root / branches / Mobile_Compatible_Hito_1 / libFMap / src-comp / org / gvsig / datasources / impljme / ByteBufferJME.java @ 21606
History | View | Annotate | Download (12.4 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 Prodevelop and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (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, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* http://www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* Prodevelop Integraci?n de Tecnolog?as SL
|
34 |
* Conde Salvatierra de ?lava , 34-10
|
35 |
* 46004 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963 510 612
|
39 |
* +34 963 510 968
|
40 |
* gis@prodevelop.es
|
41 |
* http://www.prodevelop.es
|
42 |
*
|
43 |
* or
|
44 |
*
|
45 |
* Instituto de Rob?tica
|
46 |
* Apartado de correos 2085
|
47 |
* 46071 Valencia
|
48 |
* (Spain)
|
49 |
*
|
50 |
* +34 963 543 577
|
51 |
* jjordan@robotica.uv.es
|
52 |
* http://robotica.uv.es
|
53 |
*
|
54 |
*/
|
55 |
|
56 |
package org.gvsig.datasources.impljme; |
57 |
|
58 |
import java.nio.ByteOrder; |
59 |
|
60 |
import org.apache.log4j.Logger; |
61 |
import org.gvsig.datasources.common.IByteBuffer; |
62 |
|
63 |
|
64 |
|
65 |
/**
|
66 |
* This class replaces the byte buffer used in J2SE to store a file in memory.
|
67 |
*
|
68 |
* @see java.nio.ByteOrder
|
69 |
*
|
70 |
* @author jldominguez
|
71 |
*
|
72 |
*/
|
73 |
public class ByteBufferJME implements IByteBuffer { |
74 |
|
75 |
private static Logger logger = Logger.getLogger(ByteBufferJME.class); |
76 |
|
77 |
private byte[] bytes; |
78 |
private ByteOrder byteOrder = ByteOrder.LITTLE_ENDIAN; |
79 |
private int pos = 0; |
80 |
|
81 |
public ByteBufferJME(int size) { |
82 |
// logger.debug("Instanciamos ByteBufferJME con tamanyo: " + System.currentTimeMillis());
|
83 |
bytes = new byte[size]; |
84 |
} |
85 |
|
86 |
public ByteBufferJME(byte[] bb) { |
87 |
// logger.debug("Instanciamos ByteBufferJME con array : " + System.currentTimeMillis());
|
88 |
bytes = bb; |
89 |
} |
90 |
|
91 |
// /**
|
92 |
// * Constructor
|
93 |
// * @param bb array of bytes to be stored
|
94 |
// */
|
95 |
// public ByteBufferJME(byte[] bb) {
|
96 |
// bytes = bb;
|
97 |
// }
|
98 |
//
|
99 |
// /**
|
100 |
// * Moves file cursor to position p
|
101 |
// * @param p the new position
|
102 |
// */
|
103 |
// public void position(int p) {
|
104 |
// pos = p;
|
105 |
// }
|
106 |
//
|
107 |
// /**
|
108 |
// *
|
109 |
// * @return current cursor position
|
110 |
// */
|
111 |
// public int getPosition() {
|
112 |
// return pos;
|
113 |
// }
|
114 |
//
|
115 |
// /**
|
116 |
// * @return byte pointed by cursor then increases cursor
|
117 |
// */
|
118 |
// public byte getByte() {
|
119 |
// byte resp = bytes[pos];
|
120 |
// pos++;
|
121 |
// return resp;
|
122 |
// }
|
123 |
//
|
124 |
// /**
|
125 |
// * Gets one byte
|
126 |
// * @param ind index of the byte of interest
|
127 |
// * @return the byte of interest
|
128 |
// */
|
129 |
// public byte getByte(int ind) {
|
130 |
// return bytes[ind];
|
131 |
// }
|
132 |
//
|
133 |
// /**
|
134 |
// * @return integer pointed by cursor then increases cursor
|
135 |
// */
|
136 |
// public int getInt() {
|
137 |
// int resp = getInt(bytes, pos);
|
138 |
// pos = pos + 4;
|
139 |
// return resp;
|
140 |
// }
|
141 |
//
|
142 |
// /**
|
143 |
// * @return short pointed by cursor then increases cursor
|
144 |
// */
|
145 |
// public short getShort() {
|
146 |
// short resp = getShort(bytes, pos);
|
147 |
// pos = pos + 2;
|
148 |
// return resp;
|
149 |
// }
|
150 |
//
|
151 |
// /**
|
152 |
// * Gets the integer at a certain position
|
153 |
// * @param index the index of interest
|
154 |
// * @return the integer at a certain position
|
155 |
// */
|
156 |
// public int getInt(int index) {
|
157 |
// int resp = getInt(bytes, index);
|
158 |
// return resp;
|
159 |
// }
|
160 |
//
|
161 |
// /**
|
162 |
// * Gets some bytes.
|
163 |
// * @param out the array of bytes to be filled
|
164 |
// */
|
165 |
// public void getBytes(byte[] out) {
|
166 |
// for (int i=0; i<out.length; i++) {
|
167 |
// out[i] = getByte(pos + i);
|
168 |
// }
|
169 |
// pos = pos + out.length;
|
170 |
// }
|
171 |
//
|
172 |
// /**
|
173 |
// * @return double pointed by cursor then increases cursor
|
174 |
// */
|
175 |
// public double getDouble() {
|
176 |
// double resp = getDouble(bytes, pos);
|
177 |
// pos = pos + 8;
|
178 |
// return resp;
|
179 |
// }
|
180 |
//
|
181 |
// private int getInt(final byte[] data, final int offset) {
|
182 |
// return (int) getNumber(data, offset, 4);
|
183 |
// }
|
184 |
//
|
185 |
//
|
186 |
// private short getShort(final byte[] data, final int offset) {
|
187 |
// return (short) getNumber(data, offset, 2);
|
188 |
// }
|
189 |
//
|
190 |
//
|
191 |
// private long getNumber(final byte[] data, final int offset, final int size) {
|
192 |
// long result = 0;
|
193 |
//
|
194 |
// if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
|
195 |
//
|
196 |
// for (int j = offset + size - 1; j >= offset; j--) {
|
197 |
// result <<= 8;
|
198 |
// result |= 0xff & data[j];
|
199 |
// }
|
200 |
//
|
201 |
// } else {
|
202 |
//
|
203 |
// for (int j = offset; j <= offset + size - 1; j++) {
|
204 |
// result <<= 8;
|
205 |
// result |= 0xff & data[j];
|
206 |
// }
|
207 |
//
|
208 |
// }
|
209 |
//
|
210 |
// return result;
|
211 |
// }
|
212 |
//
|
213 |
// private double getDouble(final byte[] data, final int offset) {
|
214 |
// return Double.longBitsToDouble(getNumber(data, offset, 8));
|
215 |
// }
|
216 |
//
|
217 |
// /**
|
218 |
// * Sets byte order (big endian, little endian)
|
219 |
// * @param o byte order (big endian, little endian)
|
220 |
// */
|
221 |
// public void setByteOrder(ByteOrder o) {
|
222 |
// byteOrder = o;
|
223 |
// }
|
224 |
|
225 |
public void get(byte[] out) { |
226 |
|
227 |
// logger.debug("ByteBufferJME, get : " + System.currentTimeMillis());
|
228 |
|
229 |
int canread = bytes.length - pos;
|
230 |
// if (canread < out.length) {
|
231 |
// logger.warn("output buffer too long. Cannot read all " + out.length + " bytes.");
|
232 |
// }
|
233 |
|
234 |
canread = Math.min(canread, out.length);
|
235 |
|
236 |
// logger.debug("ByteBufferJME, get, array copy: " + System.currentTimeMillis());
|
237 |
|
238 |
System.arraycopy(bytes, pos, out, 0, canread); |
239 |
// logger.debug("ByteBufferJME, get, array copy, done: " + System.currentTimeMillis());
|
240 |
pos = pos + canread; |
241 |
} |
242 |
|
243 |
public void put(byte[] input) { |
244 |
|
245 |
// logger.debug("ByteBufferJME, put : " + System.currentTimeMillis());
|
246 |
|
247 |
int canwrite = bytes.length - pos;
|
248 |
if (canwrite < input.length) {
|
249 |
logger.error("Byte buffer too short to write " + input.length + " bytes. Nothing done."); |
250 |
return;
|
251 |
} |
252 |
|
253 |
// logger.debug("ByteBufferJME, put, array copy : " + System.currentTimeMillis());
|
254 |
|
255 |
System.arraycopy(input, 0, bytes, pos, input.length); |
256 |
// logger.debug("ByteBufferJME, put array copy, done : " + System.currentTimeMillis());
|
257 |
pos = pos + input.length; |
258 |
} |
259 |
|
260 |
public IByteBuffer flip() {
|
261 |
|
262 |
// logger.debug("ByteBufferJME, flip : " + System.currentTimeMillis());
|
263 |
|
264 |
byte[] newbytes = new byte[pos]; |
265 |
|
266 |
// logger.debug("ByteBufferJME, flip, array bytes : " + System.currentTimeMillis());
|
267 |
System.arraycopy(bytes, 0, newbytes, 0, pos); |
268 |
// logger.debug("ByteBufferJME, flip, array bytes, done : " + System.currentTimeMillis());
|
269 |
bytes = newbytes; |
270 |
pos = 0;
|
271 |
return this; |
272 |
} |
273 |
|
274 |
public void order(ByteOrder o) { |
275 |
byteOrder = o; |
276 |
} |
277 |
|
278 |
public void put(byte b) { |
279 |
|
280 |
// logger.debug("ByteBufferJME, put byte : " + System.currentTimeMillis());
|
281 |
|
282 |
if (pos == bytes.length) {
|
283 |
logger.error("Cannot put one byte, we are at the end. Nothing done.");
|
284 |
} else {
|
285 |
bytes[pos] = b; |
286 |
pos++; |
287 |
} |
288 |
} |
289 |
|
290 |
public int position() { |
291 |
return pos;
|
292 |
} |
293 |
|
294 |
public int remaining() { |
295 |
return bytes.length - pos;
|
296 |
} |
297 |
|
298 |
public void rewind() { |
299 |
pos = 0;
|
300 |
} |
301 |
|
302 |
// public void get(byte[] bb, int pos, int length) {
|
303 |
// }
|
304 |
|
305 |
public ByteOrder order() { |
306 |
return byteOrder;
|
307 |
} |
308 |
|
309 |
public byte get() { |
310 |
|
311 |
// logger.debug("ByteBufferJME, get byte : " + System.currentTimeMillis());
|
312 |
|
313 |
if (pos == bytes.length) {
|
314 |
logger.error("Cannot get one byte, we are at the end. Returned 0.");
|
315 |
return 0; |
316 |
} else {
|
317 |
pos++; |
318 |
return bytes[pos-1]; |
319 |
} |
320 |
} |
321 |
|
322 |
// public double getDouble(int offset) {
|
323 |
// return getDouble(bytes, offset);
|
324 |
// }
|
325 |
|
326 |
public int capacity() { |
327 |
return bytes.length;
|
328 |
} |
329 |
|
330 |
public IByteBuffer limit(int newlimit) { |
331 |
|
332 |
// logger.debug("ByteBufferJME, limit (" + newlimit + ") : " + System.currentTimeMillis());
|
333 |
|
334 |
if ((newlimit <= 0) || (newlimit > bytes.length)) { |
335 |
logger.error("Invalid new limit: " + newlimit + ". Nothing done."); |
336 |
return this; |
337 |
} |
338 |
|
339 |
byte[] newbytes = new byte[newlimit]; |
340 |
|
341 |
// logger.debug("ByteBufferJME, limit, array copy : " + System.currentTimeMillis());
|
342 |
|
343 |
System.arraycopy(bytes, 0, newbytes, 0, newlimit); |
344 |
|
345 |
// logger.debug("ByteBufferJME, limit, array copy, done : " + System.currentTimeMillis());
|
346 |
|
347 |
bytes = newbytes; |
348 |
if (pos > newlimit) {
|
349 |
pos = newlimit; |
350 |
} |
351 |
return this; |
352 |
} |
353 |
|
354 |
public int limit() { |
355 |
return bytes.length;
|
356 |
} |
357 |
|
358 |
public void position(int newp) { |
359 |
// logger.debug("ByteBuffer JME Vamos a la posicion " + newp);
|
360 |
pos = newp; |
361 |
} |
362 |
|
363 |
public double getDouble() { |
364 |
|
365 |
// logger.debug("ByteBufferJME, get Double : " + System.currentTimeMillis());
|
366 |
|
367 |
double resp = getDouble(bytes, pos);
|
368 |
pos = pos + 8;
|
369 |
return resp;
|
370 |
} |
371 |
|
372 |
public int getInt() { |
373 |
|
374 |
// logger.debug("ByteBufferJME, get Int : " + System.currentTimeMillis());
|
375 |
|
376 |
int resp = getInt(bytes, pos);
|
377 |
pos = pos + 4;
|
378 |
return resp;
|
379 |
} |
380 |
|
381 |
// ==============================
|
382 |
|
383 |
private long getNumber(final byte[] data, final int offset, final int size) { |
384 |
|
385 |
// logger.debug("ByteBufferJME, get Number : " + System.currentTimeMillis());
|
386 |
|
387 |
|
388 |
long result = 0; |
389 |
|
390 |
if (byteOrder == ByteOrder.LITTLE_ENDIAN) { |
391 |
|
392 |
for (int j = offset + size - 1; j >= offset; j--) { |
393 |
result <<= 8;
|
394 |
result |= 0xff & data[j];
|
395 |
} |
396 |
|
397 |
} else {
|
398 |
|
399 |
for (int j = offset; j <= offset + size - 1; j++) { |
400 |
result <<= 8;
|
401 |
result |= 0xff & data[j];
|
402 |
} |
403 |
|
404 |
} |
405 |
|
406 |
return result;
|
407 |
} |
408 |
|
409 |
private void putNumber(final byte[] number) { |
410 |
|
411 |
// logger.debug("ByteBufferJME, put Number : " + System.currentTimeMillis());
|
412 |
|
413 |
if (byteOrder == ByteOrder.LITTLE_ENDIAN) { |
414 |
|
415 |
byte[] reversed = reverse(number); |
416 |
put(reversed); |
417 |
|
418 |
} else {
|
419 |
|
420 |
put(number); |
421 |
} |
422 |
} |
423 |
|
424 |
|
425 |
private static byte[] reverse(byte[] b) { |
426 |
|
427 |
// logger.debug("ByteBufferJME, reverse : " + System.currentTimeMillis());
|
428 |
|
429 |
int l = b.length;
|
430 |
byte[] resp = new byte[l]; |
431 |
for (int i=0; i<l; i++) { |
432 |
resp[i] = b[l - 1 - i];
|
433 |
} |
434 |
return resp;
|
435 |
} |
436 |
|
437 |
private double getDouble(final byte[] data, final int offset) { |
438 |
|
439 |
// logger.debug("ByteBufferJME, getDouble, two params : " + System.currentTimeMillis());
|
440 |
|
441 |
return Double.longBitsToDouble(getNumber(data, offset, 8)); |
442 |
} |
443 |
|
444 |
private int getInt(final byte[] data, final int offset) { |
445 |
|
446 |
// logger.debug("ByteBufferJME, getInt, two params : " + System.currentTimeMillis());
|
447 |
|
448 |
return (int) getNumber(data, offset, 4); |
449 |
} |
450 |
|
451 |
private short getShort(final byte[] data, final int offset) { |
452 |
// logger.debug("ByteBufferJME, getShort, two params : " + System.currentTimeMillis());
|
453 |
|
454 |
return (short) getNumber(data, offset, 2); |
455 |
} |
456 |
|
457 |
public void putDouble(double v) { |
458 |
|
459 |
// logger.debug("ByteBufferJME, putDouble : " + System.currentTimeMillis());
|
460 |
|
461 |
long vlong = Double.doubleToLongBits(v); |
462 |
byte[] vbytes = longToByteArray(vlong, 8); |
463 |
putNumber(vbytes); |
464 |
} |
465 |
|
466 |
public void putInt(int v) { |
467 |
|
468 |
// logger.debug("ByteBufferJME, putInt : " + System.currentTimeMillis());
|
469 |
byte[] vbytes = longToByteArray(v, 4); |
470 |
putNumber(vbytes); |
471 |
} |
472 |
|
473 |
public void putShort(short v) { |
474 |
// logger.debug("ByteBufferJME, putShort : " + System.currentTimeMillis());
|
475 |
byte[] vbytes = longToByteArray(v, 2); |
476 |
putNumber(vbytes); |
477 |
} |
478 |
|
479 |
|
480 |
private static byte[] longToByteArray(long v, int size) { |
481 |
|
482 |
// logger.debug("ByteBufferJME, longToByteArray, two params : " + System.currentTimeMillis());
|
483 |
byte[] resp = new byte[size]; |
484 |
long aux = v;
|
485 |
for (int i=0; i<size; i++) { |
486 |
resp[size - 1 - i] = (byte) (0xff & aux); |
487 |
aux >>= 8;
|
488 |
} |
489 |
return resp;
|
490 |
} |
491 |
|
492 |
public byte get(int ind) { |
493 |
|
494 |
// logger.debug("ByteBufferJME, get : " + System.currentTimeMillis());
|
495 |
|
496 |
if (ind >= bytes.length) {
|
497 |
logger.error("Index too large: " + ind + ". Returned zero."); |
498 |
return 0; |
499 |
} else {
|
500 |
return bytes[ind];
|
501 |
} |
502 |
} |
503 |
|
504 |
public short getShort() { |
505 |
|
506 |
// logger.debug("ByteBufferJME, getShort : " + System.currentTimeMillis());
|
507 |
|
508 |
short resp = getShort(bytes, pos);
|
509 |
pos = pos + 2;
|
510 |
return resp;
|
511 |
} |
512 |
|
513 |
public int getInt(int index) { |
514 |
|
515 |
// logger.debug("ByteBufferJME, getint : " + System.currentTimeMillis());
|
516 |
|
517 |
int resp = getInt(bytes, index);
|
518 |
return resp;
|
519 |
} |
520 |
|
521 |
public byte[] getAllBytes() { |
522 |
|
523 |
// logger.debug("ByteBufferJME, getAllBytes : " + System.currentTimeMillis());
|
524 |
|
525 |
|
526 |
return bytes;
|
527 |
} |
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
} |