3
* Copyright (C) 2011 Philipp C. Heckel <philipp.heckel@gmail.com>
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.
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.
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/>.
18
package org.syncany.experimental.chunking.cbc;
20
import org.syncany.experimental.chunking.Chunk;
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;
38
public class ZipChunkEnumeration implements Enumeration<Chunk> {
39
protected static final Logger logger = Logger.getLogger(ZipChunkEnumeration.class.getSimpleName());
41
private int Tmin = 40;//*1024;
42
private int Tmax = 10000*1024;
44
private InputStream in;
49
private boolean closed;
50
private boolean headerdone;
51
private MessageDigest digest;
52
private Enumeration<Chunk> chunks;
54
public ZipChunkEnumeration(InputStream in, MessageDigest digest) throws IOException {
56
this.c = new byte[Tmax];
58
this.headerdone = false;
61
this.chunks = new TTTDChunker(Tmin, Tmax, Integer.MAX_VALUE, Integer.MAX_VALUE, 4, "MD5", "Zip").createChunks(in);
62
//throw new UnsupportedOperationException("no supported");
67
public boolean hasMoreElements() {
68
return chunks.hasMoreElements();
74
//System.out.println("fis ="+fis.available());
75
return in.available() > 0;
77
catch (IOException ex) {
78
if (logger.isLoggable(Level.WARNING)) {
79
logger.log(Level.WARNING, "Error while reading from file input stream.", ex);
87
public Chunk nextElement() {
88
return chunks.nextElement();
91
// Break at local file header 0x50, 0x4B, 0x03, 0x04
92
//new int[] { 0x50, 0x4B, 0x03, 0x04 };
93
/*bufferlen = in.read(buffer);
95
if (bufferlen == -1) {
99
// Close if this was the last bytes
100
if (in.available() == 0) {
105
byte[] buf = new byte[Tmax];
108
while (bufpos < buf.length-1) {
109
if (cpos == -1 || cpos == clen-1) {
117
//fileDigest.update(c, 0, clen);
121
buf[bufpos] = c[cpos];
127
// Get breakpoint for entry
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) {
140
if (breakpoint != -1) {
145
breakpoint = bufferlen-1;
148
/// XXXXXXXXXXXXXXXXXXXXXX
149
byte[] buf = new byte[100*1024];
154
digest.update(buffer, 0, bufferlen);
156
return new Chunk(digest.digest(), buffer, bufferlen, new byte[] {0});
158
catch (IOException ex) {
159
logger.log(Level.SEVERE, "Error while retrieving next chunk.", ex);