-
Notifications
You must be signed in to change notification settings - Fork 1
/
PatchApplier.java
151 lines (131 loc) · 5.17 KB
/
PatchApplier.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.socrata.ssync;
import com.socrata.ssync.exceptions.input.ChecksumMismatch;
import com.socrata.ssync.exceptions.input.InputException;
import com.socrata.ssync.exceptions.patch.*;
import java.io.*;
import java.util.zip.Inflater;
public class PatchApplier {
public static void apply(BlockFinder blockFinder, InputStream patch, OutputStream target) throws IOException, PatchException, InputException {
new PatchApplier(blockFinder, patch, target).go();
}
private final InputStreamReadHelper in;
private final BlockFinder blockFinder;
private final OutputStream target;
private final int blockSize;
private final byte[] dataBuf;
private PatchApplier(BlockFinder blockFinder, InputStream patch, OutputStream target) throws IOException, PatchException, InputException {
this.in = new InputStreamReadHelper(patch, InputStreamReadHelper.readChecksumAlgorithm(patch).messageDigest);
this.blockFinder = blockFinder;
this.target = target;
blockSize = in.readInt();
if(blockSize <= 0 || blockSize > Patch.MaxBlockSize) throw new InvalidBlockSize(blockSize);
dataBuf = new byte[blockSize];
}
public static class PatchInputStreamIOException extends IOException {
private PatchInputStreamIOException(Exception e) {
super(e);
}
}
public static class PatchInputStream extends InputStream {
private final VisibleByteArrayOutputStream out;
private final PatchApplier patchApplier;
private final InputStream underlying;
private boolean doneReading;
public PatchInputStream(BlockFinder blockFinder, InputStream patch) throws IOException {
try {
this.out = new VisibleByteArrayOutputStream();
this.patchApplier = new PatchApplier(blockFinder, patch, out);
this.underlying = patch;
this.doneReading = false;
} catch (PatchException | InputException e) {
throw new PatchInputStreamIOException(e);
}
}
@Override
public int read() throws IOException {
try {
if(!ensureAvailable()) return -1;
return out.read();
} catch (PatchException | InputException e) {
throw new PatchInputStreamIOException(e);
}
}
@Override
public int read(byte[] bs) throws IOException {
return read(bs, 0, bs.length);
}
@Override
public int read(byte[] bs, int off, int len) throws IOException {
try {
int total = 0;
while(len > 0 && ensureAvailable()) {
int amt = out.read(bs, off, len);
total += amt;
off += amt;
len -= amt;
}
if(total == 0) return -1;
return total;
} catch (PatchException | InputException e) {
throw new PatchInputStreamIOException(e);
}
}
@Override
public void close() throws IOException {
underlying.close();
}
private boolean ensureAvailable() throws IOException, PatchException, InputException {
while(out.available() == 0 && !doneReading) {
out.reset();
if(!patchApplier.step()) {
doneReading = true;
patchApplier.checkFooter();
}
}
return out.available() != 0;
}
}
private void go() throws IOException, InputException, PatchException {
while(step()) {}
checkFooter();
}
private void checkFooter() throws IOException, InputException {
byte[] result = in.checksum();
byte[] checksumInPatch = new byte[result.length];
in.readFullyWithoutUpdatingChecksum(checksumInPatch);
if(!java.util.Arrays.equals(result, checksumInPatch)) throw new ChecksumMismatch();
}
private boolean step() throws IOException, InputException, PatchException {
int code = readOp();
switch(code) {
case Patch.Block:
processBlock();
return true;
case Patch.Data:
processData();
return true;
case Patch.End:
return false;
default:
throw new UnknownOp(code);
}
}
private void processBlock() throws IOException, PatchException, InputException {
long blockNum = in.readInt();
long blockStart = blockNum * blockSize;
if(blockNum >= 0 && blockStart + blockSize - 1 >= 0) {
blockFinder.getBlock(blockStart, blockSize, target);
} else {
throw new NoSuchBlock(blockNum);
}
}
private void processData() throws IOException, PatchException, InputException {
int len = in.readInt();
if(len <= 0 || len > dataBuf.length) throw new InvalidDataBlockLength(len);
in.readBytes(dataBuf, len);
target.write(dataBuf, 0, len);
}
private int readOp() throws IOException, InputException {
return in.readByte() & 0xff;
}
}