1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package goldengate.common.file;
22
23 import org.jboss.netty.buffer.ChannelBuffer;
24
25
26
27
28
29
30
31 public class DataBlock {
32 private static final int EOR = 128;
33
34 private static final int EOF = 64;
35
36 private static final int ERROR = 32;
37
38 private static final int RESTART = 16;
39
40
41
42
43 private int descriptor = 0;
44
45
46
47
48 private int byteCount = -1;
49
50
51
52
53 private int[] markers = null;
54
55
56
57
58 private ChannelBuffer block = null;
59
60
61
62
63 private boolean isEOF = false;
64
65
66
67
68 private boolean isEOR = false;
69
70
71
72
73 private boolean isERROR = false;
74
75
76
77
78 private boolean isRESTART = false;
79
80
81
82
83 public DataBlock() {
84 }
85
86
87
88
89 public ChannelBuffer getBlock() {
90 return block;
91 }
92
93
94
95
96
97
98
99 public void setBlock(ChannelBuffer block) {
100 if (isRESTART) {
101 this.block = null;
102 markers = new int[6];
103 for (int i = 0; i < 6; i ++) {
104 markers[i] = block.readByte();
105 }
106 byteCount = 6;
107 return;
108 }
109 this.block = block;
110 if (this.block == null) {
111 byteCount = 0;
112 } else {
113 byteCount = this.block.readableBytes();
114 }
115 }
116
117
118
119
120 public int getByteCount() {
121 return byteCount;
122 }
123
124
125
126
127
128 public void setByteCount(int byteCount) {
129 this.byteCount = byteCount;
130 }
131
132
133
134
135
136
137
138 public void setByteCount(byte upper, byte lower) {
139 byteCount = upper << 8 | lower;
140 }
141
142
143
144
145
146 public byte getByteCountUpper() {
147 return (byte) (byteCount >> 8 & 0xFF);
148 }
149
150
151
152
153
154 public byte getByteCountLower() {
155 return (byte) (byteCount & 0xFF);
156 }
157
158
159
160
161 public byte getDescriptor() {
162 return (byte) (descriptor & 0xFF);
163 }
164
165
166
167
168
169 public void setDescriptor(int descriptor) {
170 this.descriptor = descriptor & 0xFF;
171 isEOF = false;
172 if ((this.descriptor & EOF) != 0) {
173 isEOF = true;
174 }
175 isEOR = false;
176 if ((this.descriptor & EOR) != 0) {
177 isEOR = true;
178 }
179 isERROR = false;
180 if ((this.descriptor & ERROR) != 0) {
181 isERROR = true;
182 }
183 isRESTART = false;
184 if ((this.descriptor & RESTART) != 0) {
185 isRESTART = true;
186 }
187 }
188
189
190
191
192 public boolean isEOF() {
193 return isEOF;
194 }
195
196
197
198
199
200 public void setEOF(boolean isEOF) {
201 this.isEOF = isEOF;
202 descriptor = descriptor | EOF;
203 }
204
205
206
207
208 public boolean isEOR() {
209 return isEOR;
210 }
211
212
213
214
215
216 public void setEOR(boolean isEOR) {
217 this.isEOR = isEOR;
218 descriptor = descriptor | EOR;
219 }
220
221
222
223
224 public boolean isERROR() {
225 return isERROR;
226 }
227
228
229
230
231
232 public void setERROR(boolean isERROR) {
233 this.isERROR = isERROR;
234 descriptor = descriptor | ERROR;
235 }
236
237
238
239
240 public boolean isRESTART() {
241 return isRESTART;
242 }
243
244
245
246
247
248 public void setRESTART(boolean isRESTART) {
249 this.isRESTART = isRESTART;
250 descriptor = descriptor | RESTART;
251 }
252
253
254
255
256 public int[] getMarkers() {
257 return markers;
258 }
259
260
261
262
263
264 public byte[] getByteMarkers() {
265 byte[] bmarkers = new byte[6];
266 if (markers == null) {
267 for (int i = 0; i < 6; i ++) {
268 bmarkers[i] = 0;
269 }
270 } else {
271 for (int i = 0; i < 6; i ++) {
272 bmarkers[i] = (byte) (markers[i] & 0xFF);
273 }
274 }
275 return bmarkers;
276 }
277
278
279
280
281
282
283
284 public void setMarkers(int[] markers) {
285 this.markers = markers;
286 byteCount = 6;
287 }
288
289
290
291
292
293 public void clear() {
294 block = null;
295 byteCount = -1;
296 descriptor = 0;
297 isEOF = false;
298 isEOR = false;
299 isERROR = false;
300 isRESTART = false;
301 markers = null;
302 }
303
304
305
306
307
308
309 public boolean isCleared() {
310 return byteCount == -1;
311 }
312
313
314
315
316
317
318
319
320
321 public static String toBinaryString(byte[] bytes, boolean cutted) {
322 StringBuilder buffer = new StringBuilder();
323 boolean first = true;
324 for (byte b: bytes) {
325 if (cutted) {
326 if (first) {
327 first = false;
328 } else {
329 buffer.append(' ');
330 }
331 }
332 String bin = Integer.toBinaryString(b & 0xFF);
333 bin = bin.substring(0, Math.min(bin.length(), 8));
334 for (int j = 0; j < 8 - bin.length(); j ++) {
335 buffer.append('0');
336 }
337 buffer.append(bin);
338 }
339 return buffer.toString();
340 }
341 }