001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/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 14:30:32 +0100 (Do, 27 Dez 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 14:30:32 +0100 (Do, 27 Dez 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 14:30:32 +0100 (Do, 27 Dez 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 ) | ( ( b[off + 1] & 0xff ) << 16 ) | ( ( b[off + 2] & 0xff ) << 8 ) | ( ( b[off + 3] & 0xff ) ) ); 148 149 } 150 151 /** 152 * Reads a little endian integer. 153 * 154 * @param b 155 * the raw data buffer 156 * @param off 157 * the offset into the buffer where the int resides 158 * @return the int read from the buffer at the offset location 159 */ 160 public static int readLEInt( byte[] b, int off ) { 161 162 return ( ( ( b[off + 3] & 0xff ) << 24 ) | ( ( b[off + 2] & 0xff ) << 16 ) | ( ( b[off + 1] & 0xff ) << 8 ) | ( ( b[off + 0] & 0xff ) ) ); 163 164 } 165 166 /** 167 * method: readLELong(byte[] b, int off)<BR> 168 * Reads a little endian 8 byte integer. 169 * 170 * @param b 171 * the raw data buffer 172 * @param off 173 * 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 ) ) | ( ( b[off + 1] & 0xffL ) << 8 ) | ( ( b[off + 2] & 0xffL ) << 16 ) 179 | ( ( b[off + 3] & 0xffL ) << 24 ) | ( ( b[off + 4] & 0xffL ) << 32 ) 180 | ( ( b[off + 5] & 0xffL ) << 40 ) | ( ( b[off + 6] & 0xffL ) << 48 ) | ( ( b[off + 7] & 0xffL ) << 56 ) ); 181 182 } 183 184 /** 185 * method: readBELong(byte[] b, int off)<BR> 186 * Reads a little endian 8 byte integer. 187 * 188 * @param b 189 * the raw data buffer 190 * @param off 191 * the offset into the buffer where the long resides 192 * @return the long read from the buffer at the offset location 193 */ 194 public static long readBELong( byte[] b, int off ) { 195 196 return ( ( ( b[off + 7] & 0xffL ) ) | ( ( b[off + 6] & 0xffL ) << 8 ) | ( ( b[off + 5] & 0xffL ) << 16 ) 197 | ( ( b[off + 4] & 0xffL ) << 24 ) | ( ( b[off + 3] & 0xffL ) << 32 ) 198 | ( ( b[off + 2] & 0xffL ) << 40 ) | ( ( b[off + 1] & 0xffL ) << 48 ) | ( ( b[off + 0] & 0xffL ) << 56 ) ); 199 200 } 201 202 /** 203 * Reads a little endian float. 204 * 205 * @param b 206 * the raw data buffer 207 * @param off 208 * the offset into the buffer where the float resides 209 * @return the float read from the buffer at the offset location 210 */ 211 public static float readLEFloat( byte[] b, int off ) { 212 213 float result = Float.intBitsToFloat( readLEInt( b, off ) ); 214 215 return result; 216 217 } 218 219 /** 220 * Reads a big endian float. 221 * 222 * @param b 223 * the raw data buffer 224 * @param off 225 * the offset into the buffer where the float resides 226 * @return the float read from the buffer at the offset location 227 */ 228 public static float readBEFloat( byte[] b, int off ) { 229 230 float result = Float.intBitsToFloat( readBEInt( b, off ) ); 231 232 return result; 233 234 } 235 236 /** 237 * method: readLEDouble(byte[] b, int off)<BR> 238 * Reads a little endian double. 239 * 240 * @param b 241 * the raw data buffer 242 * @param off 243 * the offset into the buffer where the double resides 244 * @return the double read from the buffer at the offset location 245 */ 246 public static double readLEDouble( byte[] b, int off ) { 247 248 double result = Double.longBitsToDouble( readLELong( b, off ) ); 249 250 return result; 251 252 } 253 254 /** 255 * method: readBEDouble(byte[] b, int off)<BR> 256 * Reads a big endian double. 257 * 258 * @param b 259 * the raw data buffer 260 * @param off 261 * the offset into the buffer where the double resides 262 * @return the double read from the buffer at the offset location 263 */ 264 public static double readBEDouble( byte[] b, int off ) { 265 266 double result = Double.longBitsToDouble( readBELong( b, off ) ); 267 268 return result; 269 270 } 271 272 /** 273 * method: writeBEShort(byte[] b, int off, int val)<BR> 274 * Writes the given short to the given buffer at the given location in big endian format. 275 * 276 * @param b 277 * the data buffer 278 * @param off 279 * the offset into the buffer where writing should occur 280 * @param val 281 * the short to write 282 * @return the number of bytes written 283 * 284 * not tested! 285 */ 286 public static int writeBEShort( byte[] b, int off, int val ) { 287 288 b[off + 0] = (byte) ( ( val >> 8 ) & 0xff ); 289 b[off + 1] = (byte) ( ( val ) & 0xff ); 290 291 return 2; 292 293 } 294 295 /** 296 * method: writeLEShort(byte[] b, int off, int val)<BR> 297 * Writes the given short to the given buffer at the given location in big endian format. 298 * 299 * @param b 300 * the data buffer 301 * @param off 302 * the offset into the buffer where writing should occur 303 * @param val 304 * the short to write 305 * @return the number of bytes written 306 * 307 * not tested! 308 */ 309 public static int writeLEShort( byte[] b, int off, int val ) { 310 311 b[off + 0] = (byte) ( ( val ) & 0xff ); 312 b[off + 1] = (byte) ( ( val >> 8 ) & 0xff ); 313 314 return 2; 315 316 } 317 318 /** 319 * method: writeBEInt(byte[] b, int off, int val)<BR> 320 * Writes the given integer to the given buffer at the given location in big endian format. 321 * 322 * @param b 323 * the data buffer 324 * @param off 325 * the offset into the buffer where writing should occur 326 * @param val 327 * the integer to write 328 * @return the number of bytes written 329 */ 330 public static int writeBEInt( byte[] b, int off, int val ) { 331 332 b[off + 0] = (byte) ( ( val >> 24 ) & 0xff ); 333 b[off + 1] = (byte) ( ( val >> 16 ) & 0xff ); 334 b[off + 2] = (byte) ( ( val >> 8 ) & 0xff ); 335 b[off + 3] = (byte) ( ( val ) & 0xff ); 336 337 return 4; 338 339 } 340 341 /** 342 * method: writeLEInt(byte[] b, int off, int val)<BR> 343 * Writes the given integer to the given buffer at the given location in little endian format. 344 * 345 * @param b 346 * the data buffer 347 * @param off 348 * the offset into the buffer where writing should occur 349 * @param val 350 * the integer to write 351 * @return the number of bytes written 352 */ 353 public static int writeLEInt( byte[] b, int off, int val ) { 354 355 b[off + 0] = (byte) ( ( val ) & 0xff ); 356 b[off + 1] = (byte) ( ( val >> 8 ) & 0xff ); 357 b[off + 2] = (byte) ( ( val >> 16 ) & 0xff ); 358 b[off + 3] = (byte) ( ( val >> 24 ) & 0xff ); 359 360 return 4; 361 362 } 363 364 /** 365 * method: writeLELong(byte[] b, int off, long val)<BR> 366 * Writes the given long to the given buffer at the given location in little endian format. 367 * 368 * @param b 369 * the data buffer 370 * @param off 371 * the offset into the buffer where writing should occur 372 * @param val 373 * the long to write 374 * @return the number of bytes written 375 */ 376 public static int writeLELong( byte[] b, int off, long val ) { 377 378 b[off + 0] = (byte) ( ( val ) & 0xff ); 379 b[off + 1] = (byte) ( ( val >> 8 ) & 0xff ); 380 b[off + 2] = (byte) ( ( val >> 16 ) & 0xff ); 381 b[off + 3] = (byte) ( ( val >> 24 ) & 0xff ); 382 b[off + 4] = (byte) ( ( val >> 32 ) & 0xff ); 383 b[off + 5] = (byte) ( ( val >> 40 ) & 0xff ); 384 b[off + 6] = (byte) ( ( val >> 48 ) & 0xff ); 385 b[off + 7] = (byte) ( ( val >> 56 ) & 0xff ); 386 387 return 8; 388 389 } 390 391 /** 392 * method: writeBELong(byte[] b, int off, long val)<BR> 393 * Writes the given long to the given buffer at the given location in big endian format. 394 * 395 * @param b 396 * the data buffer 397 * @param off 398 * the offset into the buffer where writing should occur 399 * @param val 400 * the long to write 401 * @return the number of bytes written 402 */ 403 public static int writeBELong( byte[] b, int off, long val ) { 404 405 b[off + 0] = (byte) ( ( val >> 56 ) & 0xff ); 406 b[off + 1] = (byte) ( ( val >> 48 ) & 0xff ); 407 b[off + 2] = (byte) ( ( val >> 40 ) & 0xff ); 408 b[off + 3] = (byte) ( ( val >> 32 ) & 0xff ); 409 b[off + 4] = (byte) ( ( val >> 24 ) & 0xff ); 410 b[off + 5] = (byte) ( ( val >> 16 ) & 0xff ); 411 b[off + 6] = (byte) ( ( val >> 8 ) & 0xff ); 412 b[off + 7] = (byte) ( ( val ) & 0xff ); 413 414 return 8; 415 416 } 417 418 /** 419 * method: writeLEDouble(byte[] b, int off, double val)<BR> 420 * Writes the given double to the given buffer at the given location in little endian format. 421 * 422 * @param b 423 * the data buffer 424 * @param off 425 * the offset into the buffer where writing should occur 426 * @param val 427 * the double to write 428 * @return the number of bytes written 429 */ 430 public static int writeLEDouble( byte[] b, int off, double val ) { 431 432 return writeLELong( b, off, Double.doubleToLongBits( val ) ); 433 434 } 435 436 /** 437 * method: writeBEDouble(byte[] b, int off, double val)<BR> 438 * Writes the given double to the given buffer at the given location in big endian format. 439 * 440 * @param b 441 * the data buffer 442 * @param off 443 * the offset into the buffer where writing should occur 444 * @param val 445 * the double to write 446 * @return the number of bytes written 447 */ 448 public static int writeBEDouble( byte[] b, int off, double val ) { 449 450 return writeBELong( b, off, Double.doubleToLongBits( val ) ); 451 452 } 453 454 }