1 25 26 package org.jrobin.core; 27 28 import java.io.IOException ; 29 30 71 public abstract class RrdBackend { 72 private String path; 73 private static long count = 0; 74 75 81 protected RrdBackend(String path) { 82 this.path = path; 83 count++; 84 } 85 86 90 public String getPath() { 91 return path; 92 } 93 94 101 protected abstract void write(long offset, byte[] b) throws IOException ; 102 103 110 protected abstract void read(long offset, byte[] b) throws IOException ; 111 112 117 public final byte[] readAll() throws IOException { 118 byte[] b = new byte[(int) getLength()]; 119 read(0, b); 120 return b; 121 } 122 123 128 public abstract long getLength() throws IOException ; 129 130 136 protected abstract void setLength(long length) throws IOException ; 137 138 144 public void close() throws IOException { 145 sync(); 146 } 147 148 152 protected void beforeUpdate() throws IOException { 153 } 154 155 159 protected void afterUpdate() throws IOException { 160 } 161 162 166 protected void beforeFetch() throws IOException { 167 } 168 169 173 protected void afterFetch() throws IOException { 174 } 175 176 180 protected void afterCreate() throws IOException { 181 } 182 183 190 public void sync() throws IOException { 191 } 192 193 final void writeInt(long offset, int value) throws IOException { 194 write(offset, getIntBytes(value)); 195 } 196 197 final void writeLong(long offset, long value) throws IOException { 198 write(offset, getLongBytes(value)); 199 } 200 201 final void writeDouble(long offset, double value) throws IOException { 202 write(offset, getDoubleBytes(value)); 203 } 204 205 final void writeDouble(long offset, double value, int count) throws IOException { 206 byte[] b = getDoubleBytes(value); 207 byte[] image = new byte[8 * count]; 208 for(int i = 0, k = 0; i < count; i++) { 209 image[k++] = b[0]; 210 image[k++] = b[1]; 211 image[k++] = b[2]; 212 image[k++] = b[3]; 213 image[k++] = b[4]; 214 image[k++] = b[5]; 215 image[k++] = b[6]; 216 image[k++] = b[7]; 217 } 218 write(offset, image); 219 image = null; 220 } 221 222 final void writeDouble(long offset, double[] values) throws IOException { 223 int count = values.length; 224 byte[] image = new byte[8 * count]; 225 for(int i = 0, k = 0; i < count; i++) { 226 byte[] b = getDoubleBytes(values[i]); 227 image[k++] = b[0]; 228 image[k++] = b[1]; 229 image[k++] = b[2]; 230 image[k++] = b[3]; 231 image[k++] = b[4]; 232 image[k++] = b[5]; 233 image[k++] = b[6]; 234 image[k++] = b[7]; 235 } 236 write(offset, image); 237 image = null; 238 } 239 240 final void writeString(long offset, String value) throws IOException { 241 value = value.trim(); 242 byte[] b = new byte[RrdPrimitive.STRING_LENGTH * 2]; 243 for(int i = 0, k = 0; i < RrdPrimitive.STRING_LENGTH; i++) { 244 char c = (i < value.length())? value.charAt(i): ' '; 245 byte[] cb = getCharBytes(c); 246 b[k++] = cb[0]; 247 b[k++] = cb[1]; 248 } 249 write(offset, b); 250 } 251 252 final int readInt(long offset) throws IOException { 253 byte[] b = new byte[4]; 254 read(offset, b); 255 return getInt(b); 256 } 257 258 final long readLong(long offset) throws IOException { 259 byte[] b = new byte[8]; 260 read(offset, b); 261 return getLong(b); 262 } 263 264 final double readDouble(long offset) throws IOException { 265 byte[] b = new byte[8]; 266 read(offset, b); 267 return getDouble(b); 268 } 269 270 final double[] readDouble(long offset, int count) throws IOException { 271 int byteCount = 8 * count; 272 byte[] image = new byte[byteCount]; 273 read(offset, image); 274 double[] values = new double[count]; 275 for(int i = 0, k = -1; i < count; i++) { 276 byte[] b = new byte[] { 277 image[++k], image[++k], image[++k], image[++k], 278 image[++k], image[++k], image[++k], image[++k] 279 }; 280 values[i] = getDouble(b); 281 } 282 image = null; 283 return values; 284 } 285 286 final String readString(long offset) throws IOException { 287 byte[] b = new byte[RrdPrimitive.STRING_LENGTH * 2]; 288 char[] c = new char[RrdPrimitive.STRING_LENGTH]; 289 read(offset, b); 290 for(int i = 0, k = -1; i < RrdPrimitive.STRING_LENGTH; i++) { 291 byte[] cb = new byte[] { b[++k], b[++k] }; 292 c[i] = getChar(cb); 293 } 294 return new String (c).trim(); 295 } 296 297 299 private final static byte[] getIntBytes(int value) { 300 byte[] b = new byte[4]; 301 b[0] = (byte)((value >>> 24) & 0xFF); 302 b[1] = (byte)((value >>> 16) & 0xFF); 303 b[2] = (byte)((value >>> 8) & 0xFF); 304 b[3] = (byte)((value >>> 0) & 0xFF); 305 return b; 306 } 307 308 private final static byte[] getLongBytes(long value) { 309 byte[] b = new byte[8]; 310 b[0] = (byte)((int)(value >>> 56) & 0xFF); 311 b[1] = (byte)((int)(value >>> 48) & 0xFF); 312 b[2] = (byte)((int)(value >>> 40) & 0xFF); 313 b[3] = (byte)((int)(value >>> 32) & 0xFF); 314 b[4] = (byte)((int)(value >>> 24) & 0xFF); 315 b[5] = (byte)((int)(value >>> 16) & 0xFF); 316 b[6] = (byte)((int)(value >>> 8) & 0xFF); 317 b[7] = (byte)((int)(value >>> 0) & 0xFF); 318 return b; 319 } 320 321 private final static byte[] getCharBytes(char value) { 322 byte[] b = new byte[2]; 323 b[0] = (byte)((value >>> 8) & 0xFF); 324 b[1] = (byte)((value >>> 0) & 0xFF); 325 return b; 326 } 327 328 private final static byte[] getDoubleBytes(double value) { 329 byte[] bytes = getLongBytes(Double.doubleToLongBits(value)); 330 return bytes; 331 } 332 333 private final static int getInt(byte[] b) { 334 assert b.length == 4: "Invalid number of bytes for integer conversion"; 335 return ((b[0] << 24) & 0xFF000000) + ((b[1] << 16) & 0x00FF0000) + 336 ((b[2] << 8) & 0x0000FF00) + ((b[3] << 0) & 0x000000FF); 337 } 338 339 private final static long getLong(byte[] b) { 340 assert b.length == 8: "Invalid number of bytes for long conversion"; 341 int high = getInt(new byte[] { b[0], b[1], b[2], b[3] }); 342 int low = getInt(new byte[] { b[4], b[5], b[6], b[7] }); 343 long value = ((long)(high) << 32) + (low & 0xFFFFFFFFL); 344 return value; 345 } 346 347 private final static char getChar(byte[] b) { 348 assert b.length == 2: "Invalid number of bytes for char conversion"; 349 return (char)(((b[0] << 8) & 0x0000FF00) 350 + ((b[1] << 0) & 0x000000FF)); 351 } 352 353 private final static double getDouble(byte[] b) { 354 assert b.length == 8: "Invalid number of bytes for double conversion"; 355 return Double.longBitsToDouble(getLong(b)); 356 } 357 358 static long getCount() { 359 return count; 360 } 361 } 362 | Popular Tags |