~binwiederhier/syncany/newsync

« back to all changes in this revision

Viewing changes to syncany/syncany-core/src/main/java/org/syncany/experimental/chunking/cbc/ZipChunkEnumeration.java

  • Committer: Philipp Heckel
  • Date: 2011-07-24 21:15:41 UTC
  • Revision ID: philipp.heckel@gmail.com-20110724211541-sgxf25p2tijj4b07
chunking experiments

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Syncany
 
3
 * Copyright (C) 2011 Philipp C. Heckel <philipp.heckel@gmail.com> 
 
4
 *
 
5
 * This program is free software: you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation, either version 3 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
package org.syncany.experimental.chunking.cbc;
 
19
 
 
20
import org.syncany.experimental.chunking.Chunk;
 
21
import java.io.File;
 
22
import java.io.FileInputStream;
 
23
import java.io.FileNotFoundException;
 
24
import java.io.IOException;
 
25
import java.io.InputStream;
 
26
import java.security.MessageDigest;
 
27
import java.util.Enumeration;
 
28
import java.util.logging.Level;
 
29
import java.util.logging.Logger;
 
30
import org.syncany.experimental.chunking.Chunker;
 
31
import org.syncany.experimental.chunking.TTTDChunker;
 
32
import org.syncany.util.StringUtil;
 
33
 
 
34
/**
 
35
 *
 
36
 * @author pheckel
 
37
 */
 
38
public class ZipChunkEnumeration implements Enumeration<Chunk> {
 
39
    protected static final Logger logger = Logger.getLogger(ZipChunkEnumeration.class.getSimpleName());   
 
40
    
 
41
    private int Tmin = 40;//*1024;
 
42
    private int Tmax = 10000*1024;
 
43
    
 
44
    private InputStream in;           
 
45
    private byte[] c;
 
46
    private int cpos;
 
47
    private int clen;
 
48
    
 
49
    private boolean closed;
 
50
    private boolean headerdone;
 
51
    private MessageDigest digest;
 
52
    private Enumeration<Chunk> chunks;
 
53
    
 
54
    public ZipChunkEnumeration(InputStream in, MessageDigest digest) throws IOException {
 
55
        this.in = in;
 
56
        this.c = new byte[Tmax];
 
57
        this.closed = false;
 
58
        this.headerdone = false;
 
59
        this.digest = digest;
 
60
        
 
61
        this.chunks = new TTTDChunker(Tmin, Tmax, Integer.MAX_VALUE, Integer.MAX_VALUE, 4, "MD5", "Zip").createChunks(in);
 
62
                //throw new UnsupportedOperationException("no supported");
 
63
 
 
64
    }
 
65
    
 
66
    @Override
 
67
    public boolean hasMoreElements() {
 
68
        return chunks.hasMoreElements();
 
69
        /*if (closed) {
 
70
            return false;
 
71
        }
 
72
 
 
73
        try {
 
74
            //System.out.println("fis ="+fis.available());
 
75
            return in.available() > 0;
 
76
        }
 
77
        catch (IOException ex) {
 
78
            if (logger.isLoggable(Level.WARNING)) {
 
79
                logger.log(Level.WARNING, "Error while reading from file input stream.", ex);
 
80
            }
 
81
 
 
82
            return false;
 
83
        }*/
 
84
    }
 
85
 
 
86
    @Override
 
87
    public Chunk nextElement() {        
 
88
        return chunks.nextElement();
 
89
        
 
90
        /*try {
 
91
            // Break at local file header 0x50, 0x4B, 0x03, 0x04
 
92
            //new int[] { 0x50, 0x4B, 0x03, 0x04 };
 
93
            /*bufferlen = in.read(buffer);
 
94
            
 
95
            if (bufferlen == -1) {
 
96
                return null;
 
97
            }
 
98
 
 
99
            // Close if this was the last bytes
 
100
            if (in.available() == 0) {
 
101
                in.close();
 
102
                closed = true;
 
103
            }*/
 
104
/*
 
105
            byte[] buf = new byte[Tmax];
 
106
            int bufpos = -1;
 
107
                                
 
108
            while (bufpos < buf.length-1) {
 
109
                if (cpos == -1 || cpos == clen-1) {
 
110
                    cpos = -1;
 
111
                    clen = in.read(c);
 
112
 
 
113
                    if (clen == -1) {
 
114
                        break;
 
115
                    }
 
116
                    
 
117
                    //fileDigest.update(c, 0, clen);                        
 
118
                }
 
119
                        
 
120
                bufpos++; cpos++;
 
121
                buf[bufpos] = c[cpos];   
 
122
                
 
123
                if (bufpos < Tmin) {
 
124
                    continue;
 
125
                }                
 
126
 
 
127
                // Get breakpoint for entry
 
128
                int breakpoint = -1;
 
129
            
 
130
                for (int i = Tmin; i < bufferlen-4; i++) {
 
131
                    if (buffer[i] == 0x50 && buffer[i+1] == 0x4b 
 
132
                            && buffer[i+2] == 0x03 && buffer[i+3] == 0x04) {
 
133
 
 
134
                        breakpoint = i;
 
135
                        break;
 
136
                    }
 
137
                }
 
138
            }
 
139
               
 
140
            if (breakpoint != -1) {
 
141
                in.reset();
 
142
            }
 
143
            
 
144
            else {
 
145
                breakpoint = bufferlen-1;
 
146
            }
 
147
            
 
148
            /// XXXXXXXXXXXXXXXXXXXXXX
 
149
            byte[] buf = new byte[100*1024];
 
150
            int bufpos = -1;            
 
151
            
 
152
            // Create chunk
 
153
            digest.reset();
 
154
            digest.update(buffer, 0, bufferlen);
 
155
                  
 
156
            return new Chunk(digest.digest(), buffer, bufferlen, new byte[] {0});
 
157
        } 
 
158
        catch (IOException ex) {                
 
159
            logger.log(Level.SEVERE, "Error while retrieving next chunk.", ex);
 
160
            return null;
 
161
        }*/
 
162
    }
 
163
   
 
164
    
 
165
}