KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > file > ByteUtil


1 package com.quadcap.sql.file;
2
3 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 /**
42  * This class encapsulates various utilities for manipulating byte arrays
43  * which contain primitive values, such as integers, strings, etc.
44  *
45  * @author Stan Bailes
46  */

47 public class ByteUtil {
48     /**
49      * Get the two-byte short stored at the specified location in the
50      * buffer.
51      *
52      * @param buf the buffer from which the short is read.
53      * @param pos the position in the buffer.
54      */

55     public static final short getShort(byte[] buf, int pos) {
56     return (short)(((buf[pos] & 0xff) << 8) + (buf[pos+1] & 0xff));
57     }
58
59     /**
60      * Write an short value as two bytes (MSB first) into the buffer
61      *
62      * @param buf the buffer
63      * @param pos the byte offset of the first value
64      * @param val the value to write to the buffer
65      */

66     public static final void putShort(byte[] buf, int pos, short val) {
67     buf[pos++] = (byte)((val >>> 8) & 0xff);
68     buf[pos] = (byte)((val ) & 0xff);
69     }
70
71     /**
72      * Get the four-byte integer stored at the specified location in the
73      * buffer.
74      *
75      * @param buf the buffer from which the integer is read.
76      * @param pos the position in the buffer.
77      */

78     public static final int getInt(byte[] buf, int pos) {
79     return
80         ((buf[pos] & 0xff) << 24) +
81         ((buf[pos+1] & 0xff) << 16) +
82         ((buf[pos+2] & 0xff) << 8) +
83         (buf[pos+3] & 0xff);
84     }
85
86     /**
87      * Write an integer value as four bytes (MSB first) into the buffer
88      *
89      * @param buf the buffer
90      * @param pos the byte offset of the first value
91      * @param val the value to write to the buffer
92      */

93     public static final void putInt(byte[] buf, int pos, int val) {
94     buf[pos++] = (byte)((val >>> 24) & 0xff);
95     buf[pos++] = (byte)((val >>> 16) & 0xff);
96     buf[pos++] = (byte)((val >>> 8) & 0xff);
97     buf[pos] = (byte)((val ) & 0xff);
98     }
99
100     /**
101      * Get the eight-byte long stored at the specified location in the
102      * buffer.
103      *
104      * @param buf the buffer from which the long is read.
105      * @param pos the position in the buffer.
106      */

107     public static final long getLong(byte[] buf, int pos) {
108     return
109         ((long)(buf[pos] & 0xff) << 56) +
110         ((long)(buf[pos+1] & 0xff) << 48) +
111         ((long)(buf[pos+2] & 0xff) << 40) +
112         ((long)(buf[pos+3] & 0xff) << 32) +
113         ((long)(buf[pos+4] & 0xff) << 24) +
114         ((long)(buf[pos+5] & 0xff) << 16) +
115         ((long)(buf[pos+6] & 0xff) << 8) +
116         ((long)buf[pos+7] & 0xff);
117     }
118
119     /**
120      * Write an long value as eight bytes (MSB first) into the buffer
121      *
122      * @param buf the buffer
123      * @param pos the byte offset of the first value
124      * @param val the value to write to the buffer
125      */

126     public static final void putLong(byte[] buf, int pos, long val) {
127     buf[pos++] = (byte)((val >>> 56) & 0xff);
128     buf[pos++] = (byte)((val >>> 48) & 0xff);
129     buf[pos++] = (byte)((val >>> 40) & 0xff);
130     buf[pos++] = (byte)((val >>> 32) & 0xff);
131     buf[pos++] = (byte)((val >>> 24) & 0xff);
132     buf[pos++] = (byte)((val >>> 16) & 0xff);
133     buf[pos++] = (byte)((val >>> 8) & 0xff);
134     buf[pos] = (byte)((val ) & 0xff);
135     }
136
137
138 }
139
Popular Tags