001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/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: 6259 $
018     * $Date: 2007-03-20 10:15:15 +0100 (Di, 20 Mär 2007) $
019     * $Author: bezema $
020     * 
021     * **********************************************************************
022     */
023    
024    /*----------------    FILE HEADER  ------------------------------------------
025    
026    This file is part of deegree.
027    Copyright (C) 2001-2006 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    /**
071     * Utilities for reading and writing the components of binary files.
072     *
073     * @author Tom Mitchell <tmitchell@bbn.com>
074     * @version $Revision: 6259 $ $Date: 2007-03-20 10:15:15 +0100 (Di, 20 Mär 2007) $
075     * modified
076     *
077     * <B>Last changes<B>:<BR>
078     * 25.11.1999  ap: memory allocation dynaminized<BR>
079     * 17.01.2000  ap: method SHPPoint readPoint(byte[] b, int off) modified<BR>
080     * 17.01.2000  ap: method SHPEnvelope readBox(byte[] b, int off) modified<BR>
081     * 17.01.2000  ap: method writePoint(..) modified<BR>
082     * 25.01.2000  ap: method writeBELong(..) added<BR>
083     * 25.01.2000  ap: method writeBEDouble(..) added<BR>
084     * 25.01.2000  ap: method readBELong(..) added<BR>
085     * 25.01.2000  ap: method readBEDouble(..) added<BR>
086     * 22.04.2000  ap: method readBEShort(byte[] b, int off) added<BR>
087     * 22.04.2000  ap: method readLEShort(byte[] b, int off) added<BR>
088     * 22.04.2000  ap: method writeBEShort(byte[] b, int off) added<BR>
089     * 22.04.2000  ap: method writeLEShort(byte[] b, int off) added<BR>
090     *
091     * <p>----------------------------------------------------------------------------</p>
092     *
093     * @author Andreas Poth
094     * @version $Revision: 6259 $ $Date: 2007-03-20 10:15:15 +0100 (Di, 20 Mär 2007) $
095     * <p>
096     */
097    
098    public class ByteUtils {
099    
100    
101       /**
102        * method: readBEShort(byte[] b, int off)<BR>
103        * Reads a big endian small integer.
104        *
105        * @param b the raw data buffer
106        * @param off the offset into the buffer where the int resides
107        * @return the int read from the buffer at the offset location
108        *
109        * not tested!
110        */
111        public static int readBEShort(byte[] b, int off) {
112    
113            return (((b[off + 0] & 0xff) <<  8) |
114                    ((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 the raw data buffer
123        * @param off the offset into the buffer where the int resides
124        * @return the int read from the buffer at the offset location
125        *
126        * not tested!
127        */
128        public static int readLEShort(byte[] b, int off) {
129    
130            return (((b[off + 1] & 0xff) <<  8) |
131                    ((b[off + 0] & 0xff)      ));
132    
133        }
134    
135       /**
136        * Reads a big endian integer.
137        *
138        * @param b the raw data buffer
139        * @param off the offset into the buffer where the int resides
140        * @return the int read from the buffer at the offset location
141        */
142        public static int readBEInt(byte[] b, int off) {
143    
144            return (((b[off + 0] & 0xff) << 24) |
145                    ((b[off + 1] & 0xff) << 16) |
146                    ((b[off + 2] & 0xff) <<  8) |
147                    ((b[off + 3] & 0xff)      ));
148    
149        }
150    
151       
152       /**
153        * Reads a little endian integer.
154        *
155        * @param b the raw data buffer
156        * @param off the offset into the buffer where the int resides
157        * @return the int read from the buffer at the offset location
158        */
159        public static int readLEInt(byte[] b, int off) {
160            
161            return (((b[off + 3] & 0xff) << 24) |
162                    ((b[off + 2] & 0xff) << 16) |
163                    ((b[off + 1] & 0xff) <<  8) |
164                    ((b[off + 0] & 0xff)      ));
165    
166        }
167    
168        /**
169         * method: readLELong(byte[] b, int off)<BR>
170         * Reads a little endian 8 byte integer.
171         *
172         * @param b the raw data buffer
173         * @param off the offset into the buffer where the long resides
174         * @return the long read from the buffer at the offset location
175         */
176        public static long readLELong(byte[] b, int off) {
177    
178            return (((b[off + 0] & 0xffL)      ) |
179                    ((b[off + 1] & 0xffL) <<  8) |
180                    ((b[off + 2] & 0xffL) << 16) |
181                    ((b[off + 3] & 0xffL) << 24) |
182                    ((b[off + 4] & 0xffL) << 32) |
183                    ((b[off + 5] & 0xffL) << 40) |
184                    ((b[off + 6] & 0xffL) << 48) |
185                    ((b[off + 7] & 0xffL) << 56));
186    
187        }
188    
189        /**
190         * method: readBELong(byte[] b, int off)<BR>
191         * Reads a little endian 8 byte integer.
192         *
193         * @param b the raw data buffer
194         * @param off the offset into the buffer where the long resides
195         * @return the long read from the buffer at the offset location
196         */
197        public static long readBELong(byte[] b, int off) {
198    
199            return (((b[off + 7] & 0xffL)      ) |
200                    ((b[off + 6] & 0xffL) <<  8) |
201                    ((b[off + 5] & 0xffL) << 16) |
202                    ((b[off + 4] & 0xffL) << 24) |
203                    ((b[off + 3] & 0xffL) << 32) |
204                    ((b[off + 2] & 0xffL) << 40) |
205                    ((b[off + 1] & 0xffL) << 48) |
206                    ((b[off + 0] & 0xffL) << 56));
207    
208        }
209        
210        /**    
211        * Reads a little endian float.
212        *
213        * @param b the raw data buffer
214        * @param off the offset into the buffer where the float resides
215        * @return the float read from the buffer at the offset location
216        */
217        public static float readLEFloat(byte[] b, int off) {
218    
219            float result = Float.intBitsToFloat( readLEInt(b, off) );
220    
221            return result;
222    
223        }
224    
225       /**
226        * Reads a big endian float.
227        *
228        * @param b the raw data buffer
229        * @param off the offset into the buffer where the float resides
230        * @return the float read from the buffer at the offset location
231        */
232        public static float readBEFloat(byte[] b, int off) {
233    
234            float result = Float.intBitsToFloat( readBEInt(b, off) );
235    
236            return result;
237    
238        }
239    
240    
241       /**
242        * method: readLEDouble(byte[] b, int off)<BR>
243        * Reads a little endian double.
244        *
245        * @param b the raw data buffer
246        * @param off the offset into the buffer where the double resides
247        * @return the double read from the buffer at the offset location
248        */
249        public static double readLEDouble(byte[] b, int off) {
250    
251            double result = Double.longBitsToDouble(readLELong(b, off));
252    
253            return result;
254    
255        }
256    
257       /**
258        * method: readBEDouble(byte[] b, int off)<BR>
259        * Reads a big endian double.
260        *
261        * @param b the raw data buffer
262        * @param off the offset into the buffer where the double resides
263        * @return the double read from the buffer at the offset location
264        */
265        public static double readBEDouble(byte[] b, int off) {
266    
267            double result = Double.longBitsToDouble(readBELong(b, off));
268    
269            return result;
270    
271        }
272    
273       /**
274        * method: writeBEShort(byte[] b, int off, int val)<BR>
275        * Writes the given short to the given buffer at the given location
276        * in big endian format.
277        *
278        * @param b the data buffer
279        * @param off the offset into the buffer where writing should occur
280        * @param val the short to write
281        * @return the number of bytes written
282        *
283        * not tested!
284        */
285        public static int writeBEShort(byte[] b, int off, int val) {
286    
287            b[off + 0] = (byte) ((val >> 8) & 0xff);
288            b[off + 1] = (byte) ((val     ) & 0xff);
289    
290            return 2;
291    
292        }
293    
294       /**
295        * method: writeLEShort(byte[] b, int off, int val)<BR>
296        * Writes the given short to the given buffer at the given location
297        * in big endian format.
298        *
299        * @param b the data buffer
300        * @param off the offset into the buffer where writing should occur
301        * @param val the short to write
302        * @return the number of bytes written
303        *
304        * not tested!
305        */
306        public static int writeLEShort(byte[] b, int off, int val) {
307            
308            b[off + 0] = (byte) ((val     ) & 0xff);
309            b[off + 1] = (byte) ((val >> 8) & 0xff);
310    
311            return 2;
312    
313        }
314    
315    
316       /**
317        * method: writeBEInt(byte[] b, int off, int val)<BR>
318        * Writes the given integer to the given buffer at the given location
319        * in big endian format.
320        *
321        * @param b the data buffer
322        * @param off the offset into the buffer where writing should occur
323        * @param val the integer to write
324        * @return the number of bytes written
325        */
326        public static int writeBEInt(byte[] b, int off, int val) {
327    
328            b[off + 0] = (byte) ((val >> 24) & 0xff);
329            b[off + 1] = (byte) ((val >> 16) & 0xff);
330            b[off + 2] = (byte) ((val >>  8) & 0xff);
331            b[off + 3] = (byte) ((val      ) & 0xff);
332    
333            return 4;
334    
335        }
336    
337       /**
338        * method: writeLEInt(byte[] b, int off, int val)<BR>
339        * Writes the given integer to the given buffer at the given location
340        * in little endian format.
341        *
342        * @param b the data buffer
343        * @param off the offset into the buffer where writing should occur
344        * @param val the integer to write
345        * @return the number of bytes written
346        */
347        public static int writeLEInt(byte[] b, int off, int val) {
348    
349            b[off + 0] = (byte) ((val      ) & 0xff);
350            b[off + 1] = (byte) ((val >>  8) & 0xff);
351            b[off + 2] = (byte) ((val >> 16) & 0xff);
352            b[off + 3] = (byte) ((val >> 24) & 0xff);
353    
354            return 4;
355    
356        }
357    
358       /**
359        * method: writeLELong(byte[]  b, int off, long val)<BR>
360        * Writes the given long to the given buffer at the given location
361        * in little endian format.
362        *
363        * @param b the data buffer
364        * @param off the offset into the buffer where writing should occur
365        * @param val the long to write
366        * @return the number of bytes written
367        */
368        public static int writeLELong(byte[]  b, int off, long val) {
369    
370            b[off + 0] = (byte) ((val      ) & 0xff);
371            b[off + 1] = (byte) ((val >>  8) & 0xff);
372            b[off + 2] = (byte) ((val >> 16) & 0xff);
373            b[off + 3] = (byte) ((val >> 24) & 0xff);
374            b[off + 4] = (byte) ((val >> 32) & 0xff);
375            b[off + 5] = (byte) ((val >> 40) & 0xff);
376            b[off + 6] = (byte) ((val >> 48) & 0xff);
377            b[off + 7] = (byte) ((val >> 56) & 0xff);
378    
379            return 8;
380    
381        }
382    
383       /**
384        * method: writeBELong(byte[]  b, int off, long val)<BR>
385        * Writes the given long to the given buffer at the given location
386        * in big endian format.
387        *
388        * @param b the data buffer
389        * @param off the offset into the buffer where writing should occur
390        * @param val the long to write
391        * @return the number of bytes written
392        */
393        public static int writeBELong(byte[]  b, int off, long val) {
394    
395            b[off + 0] = (byte) ((val >> 56) & 0xff);
396            b[off + 1] = (byte) ((val >> 48) & 0xff);
397            b[off + 2] = (byte) ((val >> 40) & 0xff);
398            b[off + 3] = (byte) ((val >> 32) & 0xff);
399            b[off + 4] = (byte) ((val >> 24) & 0xff);
400            b[off + 5] = (byte) ((val >> 16) & 0xff);
401            b[off + 6] = (byte) ((val >>  8) & 0xff);
402            b[off + 7] = (byte) ((val      ) & 0xff);
403    
404            return 8;
405    
406        }
407    
408    
409       /**
410        * method: writeLEDouble(byte[] b, int off, double val)<BR>
411        * Writes the given double to the given buffer at the given location
412        * in little endian format.
413        *
414        * @param b the data buffer
415        * @param off the offset into the buffer where writing should occur
416        * @param val the double to write
417        * @return the number of bytes written
418        */
419        public static int writeLEDouble(byte[] b, int off, double val) {
420    
421            return writeLELong(b, off, Double.doubleToLongBits(val));
422    
423        }
424    
425       /**
426        * method: writeBEDouble(byte[] b, int off, double val)<BR>
427        * Writes the given double to the given buffer at the given location
428        * in big endian format.
429        *
430        * @param b the data buffer
431        * @param off the offset into the buffer where writing should occur
432        * @param val the double to write
433        * @return the number of bytes written
434        */
435        public static int writeBEDouble(byte[] b, int off, double val) {
436    
437            return writeBELong(b, off, Double.doubleToLongBits(val));
438    
439        }
440    
441    }
442    /* ********************************************************************
443    Changes to this class. What the people have been up to:
444    $Log$
445    Revision 1.5  2006/07/12 14:46:15  poth
446    comment footer added
447    
448    ********************************************************************** */