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