1 package net.obsearch.utils.bytes; 2 3 import java.nio.ByteBuffer; 4 import net.obsearch.constants.ByteConstants; 5 import java.nio.ByteOrder; 6 /* 7 OBSearch: a distributed similarity search engine This project is to 8 similarity search what 'bit-torrent' is to downloads. 9 Copyright (C) 2008 Arnoldo Jose Muller Molina 10 11 This program is free software: you can redistribute it and/or modify 12 it under the terms of the GNU General Public License as published by 13 the Free Software Foundation, either version 3 of the License, or 14 (at your option) any later version. 15 16 This program is distributed in the hope that it will be useful, 17 but WITHOUT ANY WARRANTY; without even the implied warranty of 18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 GNU General Public License for more details. 20 21 You should have received a copy of the GNU General Public License 22 along with this program. If not, see <http://www.gnu.org/licenses/>. 23 */ 24 25 /** 26 * ByteConversion. Utilities to convert primitive types from and to byte arrays. 27 * @author Arnoldo Jose Muller Molina 28 */ 29 30 public class ByteConversion { 31 32 public static final ByteOrder ORDERING = ByteOrder.nativeOrder(); 33 /** 34 * Create a ByteBuffer of size n. 35 * @param n size of the new buffer. 36 * @return The buffer. 37 */ 38 public static ByteBuffer createByteBuffer(int n){ 39 byte [] r = new byte[n]; 40 return createByteBuffer(r); 41 } 42 43 /** 44 * Create a ByteBuffer from the given byte array. 45 * @param n size of the new buffer. 46 * @return The buffer. 47 */ 48 public static ByteBuffer createByteBuffer(byte[] data){ 49 ByteBuffer res = ByteBuffer.wrap(data); 50 res.order(ORDERING); 51 return res; 52 } 53 54 55 56 /** 57 * Reads a(n) Byte from the beginning of the given data array. 58 * No size checks are performed. 59 * @param data Data that holds the encoded byte. 60 * @return byte parsed from data. 61 */ 62 public static byte bytesToByte(byte[] data){ 63 return createByteBuffer(data).get(); 64 } 65 66 /** 67 * Convert a(n) Byte into bytes. 68 * @param i value to convert. 69 * @return The byte array that represents the value. 70 */ 71 public static byte[] byteToBytes(byte i){ 72 byte [] res = new byte[ByteConstants.Byte.getSize()]; 73 createByteBuffer(res).put(i); 74 return res; 75 } 76 77 /** 78 * Reads a(n) Byte from the beginning of the given ByteBuffer. 79 * No size checks are performed. 80 * @param data Data that holds the encoded byte. 81 * @return byte parsed from data. 82 */ 83 public static byte byteBufferToByte(ByteBuffer data){ 84 return data.get(); 85 } 86 87 /** 88 * Convert a(n) Byte into bytes. 89 * @param i value to convert. 90 * @return The byte array that represents the value. 91 */ 92 public static ByteBuffer byteToByteBuffer(byte i){ 93 ByteBuffer res = createByteBuffer(ByteConstants.Byte.getSize()); 94 res.put(i); 95 return res; 96 } 97 98 99 100 /** 101 * Reads a(n) Short from the beginning of the given data array. 102 * No size checks are performed. 103 * @param data Data that holds the encoded short. 104 * @return short parsed from data. 105 */ 106 public static short bytesToShort(byte[] data){ 107 return createByteBuffer(data).getShort(); 108 } 109 110 /** 111 * Convert a(n) Short into bytes. 112 * @param i value to convert. 113 * @return The byte array that represents the value. 114 */ 115 public static byte[] shortToBytes(short i){ 116 byte [] res = new byte[ByteConstants.Short.getSize()]; 117 createByteBuffer(res).putShort(i); 118 return res; 119 } 120 121 /** 122 * Reads a(n) Short from the beginning of the given ByteBuffer. 123 * No size checks are performed. 124 * @param data Data that holds the encoded short. 125 * @return short parsed from data. 126 */ 127 public static short byteBufferToShort(ByteBuffer data){ 128 return data.getShort(); 129 } 130 131 /** 132 * Convert a(n) Short into bytes. 133 * @param i value to convert. 134 * @return The byte array that represents the value. 135 */ 136 public static ByteBuffer shortToByteBuffer(short i){ 137 ByteBuffer res = createByteBuffer(ByteConstants.Short.getSize()); 138 res.putShort(i); 139 return res; 140 } 141 142 143 144 /** 145 * Reads a(n) Int from the beginning of the given data array. 146 * No size checks are performed. 147 * @param data Data that holds the encoded int. 148 * @return int parsed from data. 149 */ 150 public static int bytesToInt(byte[] data){ 151 return createByteBuffer(data).getInt(); 152 } 153 154 /** 155 * Convert a(n) Int into bytes. 156 * @param i value to convert. 157 * @return The byte array that represents the value. 158 */ 159 public static byte[] intToBytes(int i){ 160 byte [] res = new byte[ByteConstants.Int.getSize()]; 161 createByteBuffer(res).putInt(i); 162 return res; 163 } 164 165 /** 166 * Reads a(n) Int from the beginning of the given ByteBuffer. 167 * No size checks are performed. 168 * @param data Data that holds the encoded int. 169 * @return int parsed from data. 170 */ 171 public static int byteBufferToInt(ByteBuffer data){ 172 return data.getInt(); 173 } 174 175 /** 176 * Convert a(n) Int into bytes. 177 * @param i value to convert. 178 * @return The byte array that represents the value. 179 */ 180 public static ByteBuffer intToByteBuffer(int i){ 181 ByteBuffer res = createByteBuffer(ByteConstants.Int.getSize()); 182 res.putInt(i); 183 return res; 184 } 185 186 187 188 /** 189 * Reads a(n) Long from the beginning of the given data array. 190 * No size checks are performed. 191 * @param data Data that holds the encoded long. 192 * @return long parsed from data. 193 */ 194 public static long bytesToLong(byte[] data){ 195 return createByteBuffer(data).getLong(); 196 } 197 198 /** 199 * Convert a(n) Long into bytes. 200 * @param i value to convert. 201 * @return The byte array that represents the value. 202 */ 203 public static byte[] longToBytes(long i){ 204 byte [] res = new byte[ByteConstants.Long.getSize()]; 205 createByteBuffer(res).putLong(i); 206 return res; 207 } 208 209 /** 210 * Reads a(n) Long from the beginning of the given ByteBuffer. 211 * No size checks are performed. 212 * @param data Data that holds the encoded long. 213 * @return long parsed from data. 214 */ 215 public static long byteBufferToLong(ByteBuffer data){ 216 return data.getLong(); 217 } 218 219 /** 220 * Convert a(n) Long into bytes. 221 * @param i value to convert. 222 * @return The byte array that represents the value. 223 */ 224 public static ByteBuffer longToByteBuffer(long i){ 225 ByteBuffer res = createByteBuffer(ByteConstants.Long.getSize()); 226 res.putLong(i); 227 return res; 228 } 229 230 231 232 /** 233 * Reads a(n) Float from the beginning of the given data array. 234 * No size checks are performed. 235 * @param data Data that holds the encoded float. 236 * @return float parsed from data. 237 */ 238 public static float bytesToFloat(byte[] data){ 239 return createByteBuffer(data).getFloat(); 240 } 241 242 /** 243 * Convert a(n) Float into bytes. 244 * @param i value to convert. 245 * @return The byte array that represents the value. 246 */ 247 public static byte[] floatToBytes(float i){ 248 byte [] res = new byte[ByteConstants.Float.getSize()]; 249 createByteBuffer(res).putFloat(i); 250 return res; 251 } 252 253 /** 254 * Reads a(n) Float from the beginning of the given ByteBuffer. 255 * No size checks are performed. 256 * @param data Data that holds the encoded float. 257 * @return float parsed from data. 258 */ 259 public static float byteBufferToFloat(ByteBuffer data){ 260 return data.getFloat(); 261 } 262 263 /** 264 * Convert a(n) Float into bytes. 265 * @param i value to convert. 266 * @return The byte array that represents the value. 267 */ 268 public static ByteBuffer floatToByteBuffer(float i){ 269 ByteBuffer res = createByteBuffer(ByteConstants.Float.getSize()); 270 res.putFloat(i); 271 return res; 272 } 273 274 275 276 /** 277 * Reads a(n) Double from the beginning of the given data array. 278 * No size checks are performed. 279 * @param data Data that holds the encoded double. 280 * @return double parsed from data. 281 */ 282 public static double bytesToDouble(byte[] data){ 283 return createByteBuffer(data).getDouble(); 284 } 285 286 /** 287 * Convert a(n) Double into bytes. 288 * @param i value to convert. 289 * @return The byte array that represents the value. 290 */ 291 public static byte[] doubleToBytes(double i){ 292 byte [] res = new byte[ByteConstants.Double.getSize()]; 293 createByteBuffer(res).putDouble(i); 294 return res; 295 } 296 297 /** 298 * Reads a(n) Double from the beginning of the given ByteBuffer. 299 * No size checks are performed. 300 * @param data Data that holds the encoded double. 301 * @return double parsed from data. 302 */ 303 public static double byteBufferToDouble(ByteBuffer data){ 304 return data.getDouble(); 305 } 306 307 /** 308 * Convert a(n) Double into bytes. 309 * @param i value to convert. 310 * @return The byte array that represents the value. 311 */ 312 public static ByteBuffer doubleToByteBuffer(double i){ 313 ByteBuffer res = createByteBuffer(ByteConstants.Double.getSize()); 314 res.putDouble(i); 315 return res; 316 } 317 318 319 320 }