001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/model/spatialschema/ByteUtils.java $
002: /* **********************************************************************
003: *
004: * BBN Corporation
005: * 10 Moulton St.
006: * Cambridge, MA 02138
007: * (617) 873-2000
008: *
009: * Copyright (C) 1998
010: * This software is subject to copyright protection under the laws of
011: * the United States and other countries.
012: *
013: * **********************************************************************
014: *
015: * $Source$
016: * $RCSfile$
017: * $Revision: 9343 $
018: * $Date: 2007-12-27 05:30:32 -0800 (Thu, 27 Dec 2007) $
019: * $Author: apoth $
020: *
021: * **********************************************************************
022: */
023:
024: /*---------------- FILE HEADER ------------------------------------------
025:
026: This file is part of deegree.
027: Copyright (C) 2001-2008 by:
028: EXSE, Department of Geography, University of Bonn
029: http://www.giub.uni-bonn.de/deegree/
030: lat/lon GmbH
031: http://www.lat-lon.de
032:
033: This library is free software; you can redistribute it and/or
034: modify it under the terms of the GNU Lesser General Public
035: License as published by the Free Software Foundation; either
036: version 2.1 of the License, or (at your option) any later version.
037:
038: This library is distributed in the hope that it will be useful,
039: but WITHOUT ANY WARRANTY; without even the implied warranty of
040: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
041: Lesser General Public License for more details.
042:
043: You should have received a copy of the GNU Lesser General Public
044: License along with this library; if not, write to the Free Software
045: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
046:
047: Contact:
048:
049: Andreas Poth
050: lat/lon GmbH
051: Aennchenstr. 19
052: 53115 Bonn
053: Germany
054: E-Mail: poth@lat-lon.de
055:
056: Prof. Dr. Klaus Greve
057: Department of Geography
058: University of Bonn
059: Meckenheimer Allee 166
060: 53115 Bonn
061: Germany
062: E-Mail: greve@giub.uni-bonn.de
063:
064:
065: ---------------------------------------------------------------------------*/
066:
067: package org.deegree.model.spatialschema;
068:
069: /**
070: * Utilities for reading and writing the components of binary files.
071: *
072: * @author Tom Mitchell <tmitchell@bbn.com>
073: * @version $Revision: 9343 $ $Date: 2007-12-27 05:30:32 -0800 (Thu, 27 Dec 2007) $ modified
074: *
075: * <B>Last changes<B>:<BR>
076: * 25.11.1999 ap: memory allocation dynaminized<BR>
077: * 17.01.2000 ap: method SHPPoint readPoint(byte[] b, int off) modified<BR>
078: * 17.01.2000 ap: method SHPEnvelope readBox(byte[] b, int off) modified<BR>
079: * 17.01.2000 ap: method writePoint(..) modified<BR>
080: * 25.01.2000 ap: method writeBELong(..) added<BR>
081: * 25.01.2000 ap: method writeBEDouble(..) added<BR>
082: * 25.01.2000 ap: method readBELong(..) added<BR>
083: * 25.01.2000 ap: method readBEDouble(..) added<BR>
084: * 22.04.2000 ap: method readBEShort(byte[] b, int off) added<BR>
085: * 22.04.2000 ap: method readLEShort(byte[] b, int off) added<BR>
086: * 22.04.2000 ap: method writeBEShort(byte[] b, int off) added<BR>
087: * 22.04.2000 ap: method writeLEShort(byte[] b, int off) added<BR>
088: *
089: * <p>
090: * ----------------------------------------------------------------------------
091: * </p>
092: *
093: * @author Andreas Poth
094: * @version $Revision: 9343 $ $Date: 2007-12-27 05:30:32 -0800 (Thu, 27 Dec 2007) $
095: * <p>
096: */
097:
098: public class ByteUtils {
099:
100: /**
101: * method: readBEShort(byte[] b, int off)<BR>
102: * Reads a big endian small integer.
103: *
104: * @param b
105: * the raw data buffer
106: * @param off
107: * the offset into the buffer where the int resides
108: * @return the int read from the buffer at the offset location
109: *
110: * not tested!
111: */
112: public static int readBEShort(byte[] b, int off) {
113:
114: return (((b[off + 0] & 0xff) << 8) | ((b[off + 1] & 0xff)));
115:
116: }
117:
118: /**
119: * method: readLEShort(byte[] b, int off)<BR>
120: * Reads a little endian small integer.
121: *
122: * @param b
123: * the raw data buffer
124: * @param off
125: * the offset into the buffer where the int resides
126: * @return the int read from the buffer at the offset location
127: *
128: * not tested!
129: */
130: public static int readLEShort(byte[] b, int off) {
131:
132: return (((b[off + 1] & 0xff) << 8) | ((b[off + 0] & 0xff)));
133:
134: }
135:
136: /**
137: * Reads a big endian integer.
138: *
139: * @param b
140: * the raw data buffer
141: * @param off
142: * the offset into the buffer where the int resides
143: * @return the int read from the buffer at the offset location
144: */
145: public static int readBEInt(byte[] b, int off) {
146:
147: return (((b[off + 0] & 0xff) << 24)
148: | ((b[off + 1] & 0xff) << 16)
149: | ((b[off + 2] & 0xff) << 8) | ((b[off + 3] & 0xff)));
150:
151: }
152:
153: /**
154: * Reads a little endian integer.
155: *
156: * @param b
157: * the raw data buffer
158: * @param off
159: * the offset into the buffer where the int resides
160: * @return the int read from the buffer at the offset location
161: */
162: public static int readLEInt(byte[] b, int off) {
163:
164: return (((b[off + 3] & 0xff) << 24)
165: | ((b[off + 2] & 0xff) << 16)
166: | ((b[off + 1] & 0xff) << 8) | ((b[off + 0] & 0xff)));
167:
168: }
169:
170: /**
171: * method: readLELong(byte[] b, int off)<BR>
172: * Reads a little endian 8 byte integer.
173: *
174: * @param b
175: * the raw data buffer
176: * @param off
177: * the offset into the buffer where the long resides
178: * @return the long read from the buffer at the offset location
179: */
180: public static long readLELong(byte[] b, int off) {
181:
182: return (((b[off + 0] & 0xffL)) | ((b[off + 1] & 0xffL) << 8)
183: | ((b[off + 2] & 0xffL) << 16)
184: | ((b[off + 3] & 0xffL) << 24)
185: | ((b[off + 4] & 0xffL) << 32)
186: | ((b[off + 5] & 0xffL) << 40)
187: | ((b[off + 6] & 0xffL) << 48) | ((b[off + 7] & 0xffL) << 56));
188:
189: }
190:
191: /**
192: * method: readBELong(byte[] b, int off)<BR>
193: * Reads a little endian 8 byte integer.
194: *
195: * @param b
196: * the raw data buffer
197: * @param off
198: * the offset into the buffer where the long resides
199: * @return the long read from the buffer at the offset location
200: */
201: public static long readBELong(byte[] b, int off) {
202:
203: return (((b[off + 7] & 0xffL)) | ((b[off + 6] & 0xffL) << 8)
204: | ((b[off + 5] & 0xffL) << 16)
205: | ((b[off + 4] & 0xffL) << 24)
206: | ((b[off + 3] & 0xffL) << 32)
207: | ((b[off + 2] & 0xffL) << 40)
208: | ((b[off + 1] & 0xffL) << 48) | ((b[off + 0] & 0xffL) << 56));
209:
210: }
211:
212: /**
213: * Reads a little endian float.
214: *
215: * @param b
216: * the raw data buffer
217: * @param off
218: * the offset into the buffer where the float resides
219: * @return the float read from the buffer at the offset location
220: */
221: public static float readLEFloat(byte[] b, int off) {
222:
223: float result = Float.intBitsToFloat(readLEInt(b, off));
224:
225: return result;
226:
227: }
228:
229: /**
230: * Reads a big endian float.
231: *
232: * @param b
233: * the raw data buffer
234: * @param off
235: * the offset into the buffer where the float resides
236: * @return the float read from the buffer at the offset location
237: */
238: public static float readBEFloat(byte[] b, int off) {
239:
240: float result = Float.intBitsToFloat(readBEInt(b, off));
241:
242: return result;
243:
244: }
245:
246: /**
247: * method: readLEDouble(byte[] b, int off)<BR>
248: * Reads a little endian double.
249: *
250: * @param b
251: * the raw data buffer
252: * @param off
253: * the offset into the buffer where the double resides
254: * @return the double read from the buffer at the offset location
255: */
256: public static double readLEDouble(byte[] b, int off) {
257:
258: double result = Double.longBitsToDouble(readLELong(b, off));
259:
260: return result;
261:
262: }
263:
264: /**
265: * method: readBEDouble(byte[] b, int off)<BR>
266: * Reads a big endian double.
267: *
268: * @param b
269: * the raw data buffer
270: * @param off
271: * the offset into the buffer where the double resides
272: * @return the double read from the buffer at the offset location
273: */
274: public static double readBEDouble(byte[] b, int off) {
275:
276: double result = Double.longBitsToDouble(readBELong(b, off));
277:
278: return result;
279:
280: }
281:
282: /**
283: * method: writeBEShort(byte[] b, int off, int val)<BR>
284: * Writes the given short to the given buffer at the given location in big endian format.
285: *
286: * @param b
287: * the data buffer
288: * @param off
289: * the offset into the buffer where writing should occur
290: * @param val
291: * the short to write
292: * @return the number of bytes written
293: *
294: * not tested!
295: */
296: public static int writeBEShort(byte[] b, int off, int val) {
297:
298: b[off + 0] = (byte) ((val >> 8) & 0xff);
299: b[off + 1] = (byte) ((val) & 0xff);
300:
301: return 2;
302:
303: }
304:
305: /**
306: * method: writeLEShort(byte[] b, int off, int val)<BR>
307: * Writes the given short to the given buffer at the given location in big endian format.
308: *
309: * @param b
310: * the data buffer
311: * @param off
312: * the offset into the buffer where writing should occur
313: * @param val
314: * the short to write
315: * @return the number of bytes written
316: *
317: * not tested!
318: */
319: public static int writeLEShort(byte[] b, int off, int val) {
320:
321: b[off + 0] = (byte) ((val) & 0xff);
322: b[off + 1] = (byte) ((val >> 8) & 0xff);
323:
324: return 2;
325:
326: }
327:
328: /**
329: * method: writeBEInt(byte[] b, int off, int val)<BR>
330: * Writes the given integer to the given buffer at the given location in big endian format.
331: *
332: * @param b
333: * the data buffer
334: * @param off
335: * the offset into the buffer where writing should occur
336: * @param val
337: * the integer to write
338: * @return the number of bytes written
339: */
340: public static int writeBEInt(byte[] b, int off, int val) {
341:
342: b[off + 0] = (byte) ((val >> 24) & 0xff);
343: b[off + 1] = (byte) ((val >> 16) & 0xff);
344: b[off + 2] = (byte) ((val >> 8) & 0xff);
345: b[off + 3] = (byte) ((val) & 0xff);
346:
347: return 4;
348:
349: }
350:
351: /**
352: * method: writeLEInt(byte[] b, int off, int val)<BR>
353: * Writes the given integer to the given buffer at the given location in little endian format.
354: *
355: * @param b
356: * the data buffer
357: * @param off
358: * the offset into the buffer where writing should occur
359: * @param val
360: * the integer to write
361: * @return the number of bytes written
362: */
363: public static int writeLEInt(byte[] b, int off, int val) {
364:
365: b[off + 0] = (byte) ((val) & 0xff);
366: b[off + 1] = (byte) ((val >> 8) & 0xff);
367: b[off + 2] = (byte) ((val >> 16) & 0xff);
368: b[off + 3] = (byte) ((val >> 24) & 0xff);
369:
370: return 4;
371:
372: }
373:
374: /**
375: * method: writeLELong(byte[] b, int off, long val)<BR>
376: * Writes the given long to the given buffer at the given location in little endian format.
377: *
378: * @param b
379: * the data buffer
380: * @param off
381: * the offset into the buffer where writing should occur
382: * @param val
383: * the long to write
384: * @return the number of bytes written
385: */
386: public static int writeLELong(byte[] b, int off, long val) {
387:
388: b[off + 0] = (byte) ((val) & 0xff);
389: b[off + 1] = (byte) ((val >> 8) & 0xff);
390: b[off + 2] = (byte) ((val >> 16) & 0xff);
391: b[off + 3] = (byte) ((val >> 24) & 0xff);
392: b[off + 4] = (byte) ((val >> 32) & 0xff);
393: b[off + 5] = (byte) ((val >> 40) & 0xff);
394: b[off + 6] = (byte) ((val >> 48) & 0xff);
395: b[off + 7] = (byte) ((val >> 56) & 0xff);
396:
397: return 8;
398:
399: }
400:
401: /**
402: * method: writeBELong(byte[] b, int off, long val)<BR>
403: * Writes the given long to the given buffer at the given location in big endian format.
404: *
405: * @param b
406: * the data buffer
407: * @param off
408: * the offset into the buffer where writing should occur
409: * @param val
410: * the long to write
411: * @return the number of bytes written
412: */
413: public static int writeBELong(byte[] b, int off, long val) {
414:
415: b[off + 0] = (byte) ((val >> 56) & 0xff);
416: b[off + 1] = (byte) ((val >> 48) & 0xff);
417: b[off + 2] = (byte) ((val >> 40) & 0xff);
418: b[off + 3] = (byte) ((val >> 32) & 0xff);
419: b[off + 4] = (byte) ((val >> 24) & 0xff);
420: b[off + 5] = (byte) ((val >> 16) & 0xff);
421: b[off + 6] = (byte) ((val >> 8) & 0xff);
422: b[off + 7] = (byte) ((val) & 0xff);
423:
424: return 8;
425:
426: }
427:
428: /**
429: * method: writeLEDouble(byte[] b, int off, double val)<BR>
430: * Writes the given double to the given buffer at the given location in little endian format.
431: *
432: * @param b
433: * the data buffer
434: * @param off
435: * the offset into the buffer where writing should occur
436: * @param val
437: * the double to write
438: * @return the number of bytes written
439: */
440: public static int writeLEDouble(byte[] b, int off, double val) {
441:
442: return writeLELong(b, off, Double.doubleToLongBits(val));
443:
444: }
445:
446: /**
447: * method: writeBEDouble(byte[] b, int off, double val)<BR>
448: * Writes the given double to the given buffer at the given location in big endian format.
449: *
450: * @param b
451: * the data buffer
452: * @param off
453: * the offset into the buffer where writing should occur
454: * @param val
455: * the double to write
456: * @return the number of bytes written
457: */
458: public static int writeBEDouble(byte[] b, int off, double val) {
459:
460: return writeBELong(b, off, Double.doubleToLongBits(val));
461:
462: }
463:
464: }
|