1 25 26 package org.jrobin.core; 27 28 import java.io.IOException ; 29 30 abstract class RrdPrimitive { 31 static final int STRING_LENGTH = 20; 32 static final int RRD_INT = 0, RRD_LONG = 1, RRD_DOUBLE = 2, RRD_STRING = 3; 33 static final int[] RRD_PRIM_SIZES = { 4, 8, 8, 2 * STRING_LENGTH }; 34 35 private RrdBackend backend; 36 private int byteCount; 37 private final long pointer; 38 39 protected RrdCacher cache = new RrdCacher(); 40 41 RrdPrimitive(RrdUpdater updater, int type) throws IOException { 42 this(updater, type, 1); 43 } 44 45 RrdPrimitive(RrdUpdater updater, int type, int count) throws IOException { 46 this.backend = updater.getRrdBackend(); 47 this.byteCount = RRD_PRIM_SIZES[type] * count; 48 this.pointer = updater.getRrdAllocator().allocate(byteCount); 49 } 50 51 byte[] readBytes() throws IOException { 52 byte[] b = new byte[byteCount]; 53 backend.read(pointer, b); 54 return b; 55 } 56 57 void writeBytes(byte[] b) throws IOException { 58 assert b.length == byteCount: "Invalid number of bytes supplied to RrdPrimitive.write method"; 59 backend.write(pointer, b); 60 } 61 62 int readInt() throws IOException { 63 return backend.readInt(pointer); 64 } 65 66 void writeInt(int value) throws IOException { 67 backend.writeInt(pointer, value); 68 } 69 70 long readLong() throws IOException { 71 return backend.readLong(pointer); 72 } 73 74 void writeLong(long value) throws IOException { 75 backend.writeLong(pointer, value); 76 } 77 78 double readDouble() throws IOException { 79 return backend.readDouble(pointer); 80 } 81 82 double readDouble(int index) throws IOException { 83 long offset = pointer + index * RRD_PRIM_SIZES[RRD_DOUBLE]; 84 return backend.readDouble(offset); 85 } 86 87 double[] readDouble(int index, int count) throws IOException { 88 long offset = pointer + index * RRD_PRIM_SIZES[RRD_DOUBLE]; 89 return backend.readDouble(offset, count); 90 } 91 92 void writeDouble(double value) throws IOException { 93 backend.writeDouble(pointer, value); 94 } 95 96 void writeDouble(int index, double value, int count) throws IOException { 97 long offset = pointer + index * RRD_PRIM_SIZES[RRD_DOUBLE]; 98 backend.writeDouble(offset, value, count); 99 } 100 101 void writeDouble(int index, double[] values) throws IOException { 102 long offset = pointer + index * RRD_PRIM_SIZES[RRD_DOUBLE]; 103 backend.writeDouble(offset, values); 104 } 105 106 String readString() throws IOException { 107 return backend.readString(pointer); 108 } 109 110 void writeString(String value) throws IOException { 111 backend.writeString(pointer, value); 112 } 113 114 void clearCache() { 115 cache.clearCache(); 116 } 117 } 118 | Popular Tags |