1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 private final boolean cachingAllowed;
39
40 RrdPrimitive(RrdUpdater updater, int type, boolean isConstant) throws IOException {
41 this(updater, type, 1, isConstant);
42 }
43
44 RrdPrimitive(RrdUpdater updater, int type, int count, boolean isConstant) throws IOException {
45 this.backend = updater.getRrdBackend();
46 this.byteCount = RRD_PRIM_SIZES[type] * count;
47 this.pointer = updater.getRrdAllocator().allocate(byteCount);
48 this.cachingAllowed = isConstant || backend.isCachingAllowed();
49 }
50
51 final byte[] readBytes() throws IOException {
52 byte[] b = new byte[byteCount];
53 backend.read(pointer, b);
54 return b;
55 }
56
57 final 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 final int readInt() throws IOException {
63 return backend.readInt(pointer);
64 }
65
66 final void writeInt(int value) throws IOException {
67 backend.writeInt(pointer, value);
68 }
69
70 final long readLong() throws IOException {
71 return backend.readLong(pointer);
72 }
73
74 final void writeLong(long value) throws IOException {
75 backend.writeLong(pointer, value);
76 }
77
78 final double readDouble() throws IOException {
79 return backend.readDouble(pointer);
80 }
81
82 final double readDouble(int index) throws IOException {
83 long offset = pointer + index * RRD_PRIM_SIZES[RRD_DOUBLE];
84 return backend.readDouble(offset);
85 }
86
87 final 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 final void writeDouble(double value) throws IOException {
93 backend.writeDouble(pointer, value);
94 }
95
96 final 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 final 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 final String readString() throws IOException {
107 return backend.readString(pointer);
108 }
109
110 final void writeString(String value) throws IOException {
111 backend.writeString(pointer, value);
112 }
113
114 final boolean isCachingAllowed() {
115 return cachingAllowed;
116 }
117 }