001: /**
002: * com.mckoi.util.ByteArrayUtil 26 Jun 2000
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.util;
024:
025: /**
026: * Static utilities for byte arrays.
027: *
028: * @author Tobias Downer
029: */
030:
031: public class ByteArrayUtil {
032:
033: /**
034: * Returns the chart at the given offset of the byte array.
035: */
036: public static final char getChar(byte[] arr, int offset) {
037: int c1 = (((int) arr[offset + 0]) & 0x0FF);
038: int c2 = (((int) arr[offset + 1]) & 0x0FF);
039: return (char) ((c1 << 8) + (c2));
040: }
041:
042: /**
043: * Sets the short at the given offset of the byte array.
044: */
045: public static final void setChar(char value, byte[] arr, int offset) {
046: arr[offset + 0] = (byte) ((value >>> 8) & 0x0FF);
047: arr[offset + 1] = (byte) ((value >>> 0) & 0x0FF);
048: }
049:
050: /**
051: * Returns the short at the given offset of the byte array.
052: */
053: public static final short getShort(byte[] arr, int offset) {
054: int c1 = (((int) arr[offset + 0]) & 0x0FF);
055: int c2 = (((int) arr[offset + 1]) & 0x0FF);
056: return (short) ((c1 << 8) + (c2));
057: }
058:
059: /**
060: * Sets the short at the given offset of the byte array.
061: */
062: public static final void setShort(short value, byte[] arr,
063: int offset) {
064: arr[offset + 0] = (byte) ((value >>> 8) & 0x0FF);
065: arr[offset + 1] = (byte) ((value >>> 0) & 0x0FF);
066: }
067:
068: /**
069: * Returns the int at the given offset of the byte array.
070: */
071: public static final int getInt(byte[] arr, int offset) {
072: int c1 = (((int) arr[offset + 0]) & 0x0FF);
073: int c2 = (((int) arr[offset + 1]) & 0x0FF);
074: int c3 = (((int) arr[offset + 2]) & 0x0FF);
075: int c4 = (((int) arr[offset + 3]) & 0x0FF);
076: return (c1 << 24) + (c2 << 16) + (c3 << 8) + (c4);
077: }
078:
079: /**
080: * Sets the int at the given offset of the byte array.
081: */
082: public static final void setInt(int value, byte[] arr, int offset) {
083: arr[offset + 0] = (byte) ((value >>> 24) & 0xFF);
084: arr[offset + 1] = (byte) ((value >>> 16) & 0xFF);
085: arr[offset + 2] = (byte) ((value >>> 8) & 0xFF);
086: arr[offset + 3] = (byte) ((value >>> 0) & 0xFF);
087: }
088:
089: /**
090: * Returns the long at the given offset of the byte array.
091: */
092: public static final long getLong(byte[] arr, int offset) {
093: long c1 = (((int) arr[offset + 0]) & 0x0FF);
094: long c2 = (((int) arr[offset + 1]) & 0x0FF);
095: long c3 = (((int) arr[offset + 2]) & 0x0FF);
096: long c4 = (((int) arr[offset + 3]) & 0x0FF);
097: long c5 = (((int) arr[offset + 4]) & 0x0FF);
098: long c6 = (((int) arr[offset + 5]) & 0x0FF);
099: long c7 = (((int) arr[offset + 6]) & 0x0FF);
100: long c8 = (((int) arr[offset + 7]) & 0x0FF);
101:
102: return (c1 << 56) + (c2 << 48) + (c3 << 40) + (c4 << 32)
103: + (c5 << 24) + (c6 << 16) + (c7 << 8) + (c8);
104: }
105:
106: /**
107: * Sets the long at the given offset of the byte array.
108: */
109: public static final void setLong(long value, byte[] arr, int offset) {
110: arr[offset + 0] = (byte) ((value >>> 56) & 0xFF);
111: arr[offset + 1] = (byte) ((value >>> 48) & 0xFF);
112: arr[offset + 2] = (byte) ((value >>> 40) & 0xFF);
113: arr[offset + 3] = (byte) ((value >>> 32) & 0xFF);
114: arr[offset + 4] = (byte) ((value >>> 24) & 0xFF);
115: arr[offset + 5] = (byte) ((value >>> 16) & 0xFF);
116: arr[offset + 6] = (byte) ((value >>> 8) & 0xFF);
117: arr[offset + 7] = (byte) ((value >>> 0) & 0xFF);
118: }
119:
120: }
|