KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > core > RrdPrimitive


1 /* ============================================================
2  * JRobin : Pure java implementation of RRDTool's functionality
3  * ============================================================
4  *
5  * Project Info: http://www.jrobin.org
6  * Project Lead: Sasa Markovic (saxon@jrobin.org);
7  *
8  * (C) Copyright 2003, by Sasa Markovic.
9  *
10  * Developers: Sasa Markovic (saxon@jrobin.org)
11  * Arne Vandamme (cobralord@jrobin.org)
12  *
13  * This library is free software; you can redistribute it and/or modify it under the terms
14  * of the GNU Lesser General Public License as published by the Free Software Foundation;
15  * either version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License along with this
22  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */

25
26 package org.jrobin.core;
27
28 import java.io.IOException JavaDoc;
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 JavaDoc {
42         this(updater, type, 1);
43     }
44
45     RrdPrimitive(RrdUpdater updater, int type, int count) throws IOException JavaDoc {
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 JavaDoc {
52         byte[] b = new byte[byteCount];
53         backend.read(pointer, b);
54         return b;
55     }
56
57     void writeBytes(byte[] b) throws IOException JavaDoc {
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 JavaDoc {
63         return backend.readInt(pointer);
64     }
65
66     void writeInt(int value) throws IOException JavaDoc {
67         backend.writeInt(pointer, value);
68     }
69
70     long readLong() throws IOException JavaDoc {
71         return backend.readLong(pointer);
72     }
73
74     void writeLong(long value) throws IOException JavaDoc {
75         backend.writeLong(pointer, value);
76     }
77
78     double readDouble() throws IOException JavaDoc {
79         return backend.readDouble(pointer);
80     }
81
82     double readDouble(int index) throws IOException JavaDoc {
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 JavaDoc {
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 JavaDoc {
93         backend.writeDouble(pointer, value);
94     }
95
96     void writeDouble(int index, double value, int count) throws IOException JavaDoc {
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 JavaDoc {
102         long offset = pointer + index * RRD_PRIM_SIZES[RRD_DOUBLE];
103         backend.writeDouble(offset, values);
104     }
105
106     String JavaDoc readString() throws IOException JavaDoc {
107         return backend.readString(pointer);
108     }
109
110     void writeString(String JavaDoc value) throws IOException JavaDoc {
111         backend.writeString(pointer, value);
112     }
113
114     void clearCache() {
115         cache.clearCache();
116     }
117 }
118
Popular Tags