![]() |
Quadcap Embeddable Database |
00001 package com.quadcap.sql.file; 00002 00003 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 /** 00042 * This class encapsulates various utilities for manipulating byte arrays 00043 * which contain primitive values, such as integers, strings, etc. 00044 * 00045 * @author Stan Bailes 00046 */ 00047 public class ByteUtil { 00048 /** 00049 * Get the two-byte short stored at the specified location in the 00050 * buffer. 00051 * 00052 * @param buf the buffer from which the short is read. 00053 * @param pos the position in the buffer. 00054 */ 00055 public static final short getShort(byte[] buf, int pos) { 00056 return (short)(((buf[pos] & 0xff) << 8) + (buf[pos+1] & 0xff)); 00057 } 00058 00059 /** 00060 * Write an short value as two bytes (MSB first) into the buffer 00061 * 00062 * @param buf the buffer 00063 * @param pos the byte offset of the first value 00064 * @param val the value to write to the buffer 00065 */ 00066 public static final void putShort(byte[] buf, int pos, short val) { 00067 buf[pos++] = (byte)((val >>> 8) & 0xff); 00068 buf[pos] = (byte)((val ) & 0xff); 00069 } 00070 00071 /** 00072 * Get the four-byte integer stored at the specified location in the 00073 * buffer. 00074 * 00075 * @param buf the buffer from which the integer is read. 00076 * @param pos the position in the buffer. 00077 */ 00078 public static final int getInt(byte[] buf, int pos) { 00079 return 00080 ((buf[pos] & 0xff) << 24) + 00081 ((buf[pos+1] & 0xff) << 16) + 00082 ((buf[pos+2] & 0xff) << 8) + 00083 (buf[pos+3] & 0xff); 00084 } 00085 00086 /** 00087 * Write an integer value as four bytes (MSB first) into the buffer 00088 * 00089 * @param buf the buffer 00090 * @param pos the byte offset of the first value 00091 * @param val the value to write to the buffer 00092 */ 00093 public static final void putInt(byte[] buf, int pos, int val) { 00094 buf[pos++] = (byte)((val >>> 24) & 0xff); 00095 buf[pos++] = (byte)((val >>> 16) & 0xff); 00096 buf[pos++] = (byte)((val >>> 8) & 0xff); 00097 buf[pos] = (byte)((val ) & 0xff); 00098 } 00099 00100 /** 00101 * Get the eight-byte long stored at the specified location in the 00102 * buffer. 00103 * 00104 * @param buf the buffer from which the long is read. 00105 * @param pos the position in the buffer. 00106 */ 00107 public static final long getLong(byte[] buf, int pos) { 00108 return 00109 ((long)(buf[pos] & 0xff) << 56) + 00110 ((long)(buf[pos+1] & 0xff) << 48) + 00111 ((long)(buf[pos+2] & 0xff) << 40) + 00112 ((long)(buf[pos+3] & 0xff) << 32) + 00113 ((long)(buf[pos+4] & 0xff) << 24) + 00114 ((long)(buf[pos+5] & 0xff) << 16) + 00115 ((long)(buf[pos+6] & 0xff) << 8) + 00116 ((long)buf[pos+7] & 0xff); 00117 } 00118 00119 /** 00120 * Write an long value as eight bytes (MSB first) into the buffer 00121 * 00122 * @param buf the buffer 00123 * @param pos the byte offset of the first value 00124 * @param val the value to write to the buffer 00125 */ 00126 public static final void putLong(byte[] buf, int pos, long val) { 00127 buf[pos++] = (byte)((val >>> 56) & 0xff); 00128 buf[pos++] = (byte)((val >>> 48) & 0xff); 00129 buf[pos++] = (byte)((val >>> 40) & 0xff); 00130 buf[pos++] = (byte)((val >>> 32) & 0xff); 00131 buf[pos++] = (byte)((val >>> 24) & 0xff); 00132 buf[pos++] = (byte)((val >>> 16) & 0xff); 00133 buf[pos++] = (byte)((val >>> 8) & 0xff); 00134 buf[pos] = (byte)((val ) & 0xff); 00135 } 00136 00137 00138 }