View Javadoc

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-2005, by Sasa Markovic.
9    *
10   * Developers:    Sasa Markovic (saxon@jrobin.org)
11   *
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;
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 }