001: package com.quadcap.sql.file;
002:
003: /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: /**
042: * This class encapsulates various utilities for manipulating byte arrays
043: * which contain primitive values, such as integers, strings, etc.
044: *
045: * @author Stan Bailes
046: */
047: public class ByteUtil {
048: /**
049: * Get the two-byte short stored at the specified location in the
050: * buffer.
051: *
052: * @param buf the buffer from which the short is read.
053: * @param pos the position in the buffer.
054: */
055: public static final short getShort(byte[] buf, int pos) {
056: return (short) (((buf[pos] & 0xff) << 8) + (buf[pos + 1] & 0xff));
057: }
058:
059: /**
060: * Write an short value as two bytes (MSB first) into the buffer
061: *
062: * @param buf the buffer
063: * @param pos the byte offset of the first value
064: * @param val the value to write to the buffer
065: */
066: public static final void putShort(byte[] buf, int pos, short val) {
067: buf[pos++] = (byte) ((val >>> 8) & 0xff);
068: buf[pos] = (byte) ((val) & 0xff);
069: }
070:
071: /**
072: * Get the four-byte integer stored at the specified location in the
073: * buffer.
074: *
075: * @param buf the buffer from which the integer is read.
076: * @param pos the position in the buffer.
077: */
078: public static final int getInt(byte[] buf, int pos) {
079: return ((buf[pos] & 0xff) << 24)
080: + ((buf[pos + 1] & 0xff) << 16)
081: + ((buf[pos + 2] & 0xff) << 8) + (buf[pos + 3] & 0xff);
082: }
083:
084: /**
085: * Write an integer value as four bytes (MSB first) into the buffer
086: *
087: * @param buf the buffer
088: * @param pos the byte offset of the first value
089: * @param val the value to write to the buffer
090: */
091: public static final void putInt(byte[] buf, int pos, int val) {
092: buf[pos++] = (byte) ((val >>> 24) & 0xff);
093: buf[pos++] = (byte) ((val >>> 16) & 0xff);
094: buf[pos++] = (byte) ((val >>> 8) & 0xff);
095: buf[pos] = (byte) ((val) & 0xff);
096: }
097:
098: /**
099: * Get the eight-byte long stored at the specified location in the
100: * buffer.
101: *
102: * @param buf the buffer from which the long is read.
103: * @param pos the position in the buffer.
104: */
105: public static final long getLong(byte[] buf, int pos) {
106: return ((long) (buf[pos] & 0xff) << 56)
107: + ((long) (buf[pos + 1] & 0xff) << 48)
108: + ((long) (buf[pos + 2] & 0xff) << 40)
109: + ((long) (buf[pos + 3] & 0xff) << 32)
110: + ((long) (buf[pos + 4] & 0xff) << 24)
111: + ((long) (buf[pos + 5] & 0xff) << 16)
112: + ((long) (buf[pos + 6] & 0xff) << 8)
113: + ((long) buf[pos + 7] & 0xff);
114: }
115:
116: /**
117: * Write an long value as eight bytes (MSB first) into the buffer
118: *
119: * @param buf the buffer
120: * @param pos the byte offset of the first value
121: * @param val the value to write to the buffer
122: */
123: public static final void putLong(byte[] buf, int pos, long val) {
124: buf[pos++] = (byte) ((val >>> 56) & 0xff);
125: buf[pos++] = (byte) ((val >>> 48) & 0xff);
126: buf[pos++] = (byte) ((val >>> 40) & 0xff);
127: buf[pos++] = (byte) ((val >>> 32) & 0xff);
128: buf[pos++] = (byte) ((val >>> 24) & 0xff);
129: buf[pos++] = (byte) ((val >>> 16) & 0xff);
130: buf[pos++] = (byte) ((val >>> 8) & 0xff);
131: buf[pos] = (byte) ((val) & 0xff);
132: }
133:
134: }
|