1 /**
2 This file is part of GoldenGate Project (named also GoldenGate or GG).
3
4 Copyright 2009, Frederic Bregier, and individual contributors by the @author
5 tags. See the COPYRIGHT.txt in the distribution for a full listing of
6 individual contributors.
7
8 All GoldenGate Project is free software: you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as published
10 by the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GoldenGate is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GoldenGate . If not, see <http://www.gnu.org/licenses/>.
20 */
21 package goldengate.common.file;
22
23 import org.jboss.netty.buffer.ChannelBuffer;
24
25 /**
26 * Main object implementing Data Block whaveter the mode, type, structure used.
27 *
28 * @author Frederic Bregier
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 * Descriptor
42 */
43 private int descriptor = 0;
44
45 /**
46 * Byte Count
47 */
48 private int byteCount = -1;
49
50 /**
51 * Markers
52 */
53 private int[] markers = null;
54
55 /**
56 * Byte Array
57 */
58 private ChannelBuffer block = null;
59
60 /**
61 * is EOF
62 */
63 private boolean isEOF = false;
64
65 /**
66 * is EOR
67 */
68 private boolean isEOR = false;
69
70 /**
71 * is in ERROR (should not be used)
72 */
73 private boolean isERROR = false;
74
75 /**
76 * is a MARKER RESTART
77 */
78 private boolean isRESTART = false;
79
80 /**
81 * Create a simple and empty DataBlock
82 */
83 public DataBlock() {
84 }
85
86 /**
87 * @return the block
88 */
89 public ChannelBuffer getBlock() {
90 return block;
91 }
92
93 /**
94 * Set the block and the byte count according to the block
95 *
96 * @param block
97 * the block to set
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 * @return the byteCount
119 */
120 public int getByteCount() {
121 return byteCount;
122 }
123
124 /**
125 * @param byteCount
126 * the byteCount to set
127 */
128 public void setByteCount(int byteCount) {
129 this.byteCount = byteCount;
130 }
131
132 /**
133 * @param upper
134 * upper byte of the 2 bytes length
135 * @param lower
136 * lower byte of the 2 bytes length
137 */
138 public void setByteCount(byte upper, byte lower) {
139 byteCount = upper << 8 | lower;
140 }
141
142 /**
143 *
144 * @return the Upper byte of the byte count
145 */
146 public byte getByteCountUpper() {
147 return (byte) (byteCount >> 8 & 0xFF);
148 }
149
150 /**
151 *
152 * @return the Lower byte of the byte count
153 */
154 public byte getByteCountLower() {
155 return (byte) (byteCount & 0xFF);
156 }
157
158 /**
159 * @return the descriptor
160 */
161 public byte getDescriptor() {
162 return (byte) (descriptor & 0xFF);
163 }
164
165 /**
166 * @param descriptor
167 * the descriptor to set
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 * @return the isEOF
191 */
192 public boolean isEOF() {
193 return isEOF;
194 }
195
196 /**
197 * @param isEOF
198 * the isEOF to set
199 */
200 public void setEOF(boolean isEOF) {
201 this.isEOF = isEOF;
202 descriptor = descriptor | EOF;
203 }
204
205 /**
206 * @return the isEOR
207 */
208 public boolean isEOR() {
209 return isEOR;
210 }
211
212 /**
213 * @param isEOR
214 * the isEOR to set
215 */
216 public void setEOR(boolean isEOR) {
217 this.isEOR = isEOR;
218 descriptor = descriptor | EOR;
219 }
220
221 /**
222 * @return the isERROR
223 */
224 public boolean isERROR() {
225 return isERROR;
226 }
227
228 /**
229 * @param isERROR
230 * the isERROR to set
231 */
232 public void setERROR(boolean isERROR) {
233 this.isERROR = isERROR;
234 descriptor = descriptor | ERROR;
235 }
236
237 /**
238 * @return the isRESTART
239 */
240 public boolean isRESTART() {
241 return isRESTART;
242 }
243
244 /**
245 * @param isRESTART
246 * the isRESTART to set
247 */
248 public void setRESTART(boolean isRESTART) {
249 this.isRESTART = isRESTART;
250 descriptor = descriptor | RESTART;
251 }
252
253 /**
254 * @return the markers
255 */
256 public int[] getMarkers() {
257 return markers;
258 }
259
260 /**
261 *
262 * @return the 6 bytes representation of the markers
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 * Set the markers and the byte count
280 *
281 * @param markers
282 * the markers to set
283 */
284 public void setMarkers(int[] markers) {
285 this.markers = markers;
286 byteCount = 6;
287 }
288
289 /**
290 * Clear the object
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 * Is this Block cleared
306 *
307 * @return True if this Block is cleared
308 */
309 public boolean isCleared() {
310 return byteCount == -1;
311 }
312
313 /**
314 * Translate the given array of byte into a string in binary format
315 *
316 * @param bytes
317 * @param cutted
318 * True if each Byte should be 'blank' separated or not
319 * @return the string
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 }