KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > util > ByteArrayUtil


1 /**
2  * com.mckoi.util.ByteArrayUtil 26 Jun 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.util;
26
27 /**
28  * Static utilities for byte arrays.
29  *
30  * @author Tobias Downer
31  */

32
33 public class ByteArrayUtil {
34
35   /**
36    * Returns the chart at the given offset of the byte array.
37    */

38   public static final char getChar(byte[] arr, int offset) {
39     int c1 = (((int) arr[offset + 0]) & 0x0FF);
40     int c2 = (((int) arr[offset + 1]) & 0x0FF);
41     return (char) ((c1 << 8) + (c2));
42   }
43
44   /**
45    * Sets the short at the given offset of the byte array.
46    */

47   public static final void setChar(char value, byte[] arr, int offset) {
48     arr[offset + 0] = (byte) ((value >>> 8) & 0x0FF);
49     arr[offset + 1] = (byte) ((value >>> 0) & 0x0FF);
50   }
51   
52   /**
53    * Returns the short at the given offset of the byte array.
54    */

55   public static final short getShort(byte[] arr, int offset) {
56     int c1 = (((int) arr[offset + 0]) & 0x0FF);
57     int c2 = (((int) arr[offset + 1]) & 0x0FF);
58     return (short) ((c1 << 8) + (c2));
59   }
60
61   /**
62    * Sets the short at the given offset of the byte array.
63    */

64   public static final void setShort(short value, byte[] arr, int offset) {
65     arr[offset + 0] = (byte) ((value >>> 8) & 0x0FF);
66     arr[offset + 1] = (byte) ((value >>> 0) & 0x0FF);
67   }
68
69   /**
70    * Returns the int at the given offset of the byte array.
71    */

72   public static final int getInt(byte[] arr, int offset) {
73     int c1 = (((int) arr[offset + 0]) & 0x0FF);
74     int c2 = (((int) arr[offset + 1]) & 0x0FF);
75     int c3 = (((int) arr[offset + 2]) & 0x0FF);
76     int c4 = (((int) arr[offset + 3]) & 0x0FF);
77     return (c1 << 24) + (c2 << 16) + (c3 << 8) + (c4);
78   }
79
80   /**
81    * Sets the int at the given offset of the byte array.
82    */

83   public static final void setInt(int value, byte[] arr, int offset) {
84     arr[offset + 0] = (byte) ((value >>> 24) & 0xFF);
85     arr[offset + 1] = (byte) ((value >>> 16) & 0xFF);
86     arr[offset + 2] = (byte) ((value >>> 8) & 0xFF);
87     arr[offset + 3] = (byte) ((value >>> 0) & 0xFF);
88   }
89
90   /**
91    * Returns the long at the given offset of the byte array.
92    */

93   public static final long getLong(byte[] arr, int offset) {
94     long c1 = (((int) arr[offset + 0]) & 0x0FF);
95     long c2 = (((int) arr[offset + 1]) & 0x0FF);
96     long c3 = (((int) arr[offset + 2]) & 0x0FF);
97     long c4 = (((int) arr[offset + 3]) & 0x0FF);
98     long c5 = (((int) arr[offset + 4]) & 0x0FF);
99     long c6 = (((int) arr[offset + 5]) & 0x0FF);
100     long c7 = (((int) arr[offset + 6]) & 0x0FF);
101     long c8 = (((int) arr[offset + 7]) & 0x0FF);
102     
103     return (c1 << 56) + (c2 << 48) + (c3 << 40) +
104            (c4 << 32) + (c5 << 24) + (c6 << 16) + (c7 << 8) + (c8);
105   }
106
107   /**
108    * Sets the long at the given offset of the byte array.
109    */

110   public static final void setLong(long value, byte[] arr, int offset) {
111     arr[offset + 0] = (byte) ((value >>> 56) & 0xFF);
112     arr[offset + 1] = (byte) ((value >>> 48) & 0xFF);
113     arr[offset + 2] = (byte) ((value >>> 40) & 0xFF);
114     arr[offset + 3] = (byte) ((value >>> 32) & 0xFF);
115     arr[offset + 4] = (byte) ((value >>> 24) & 0xFF);
116     arr[offset + 5] = (byte) ((value >>> 16) & 0xFF);
117     arr[offset + 6] = (byte) ((value >>> 8) & 0xFF);
118     arr[offset + 7] = (byte) ((value >>> 0) & 0xFF);
119   }
120
121
122 }
123
Popular Tags