KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
31  * Backend which is used to store all RRD bytes in memory.<p>
32  */

33 public class RrdMemoryBackend extends RrdBackend {
34     private byte[] buffer = new byte[0];
35
36     protected RrdMemoryBackend(String JavaDoc path) {
37         super(path);
38     }
39
40     protected synchronized void write(long offset, byte[] b) throws IOException JavaDoc {
41         int pos = (int) offset;
42         for(int i = 0; i < b.length; i++) {
43             buffer[pos++] = b[i];
44         }
45     }
46
47     protected synchronized void read(long offset, byte[] b) throws IOException JavaDoc {
48         int pos = (int) offset;
49         if(pos + b.length <= buffer.length) {
50             for(int i = 0; i < b.length; i++) {
51                 b[i] = buffer[pos++];
52             }
53         }
54         else {
55             throw new IOException JavaDoc("Not enough bytes available in memory " + getPath());
56         }
57     }
58
59     /**
60      * Returns the number of RRD bytes held in memory.
61      * @return Number of all RRD bytes.
62      */

63     public long getLength() {
64         return buffer.length;
65     }
66
67     /**
68      * Reserves a memory section as a RRD storage.
69      * @param newLength Number of bytes held in memory.
70      * @throws IOException Thrown in case of I/O error.
71      */

72     protected void setLength(long newLength) throws IOException JavaDoc {
73         if(newLength > Integer.MAX_VALUE) {
74             throw new IOException JavaDoc("Cannot create this big memory backed RRD");
75         }
76         buffer = new byte[(int) newLength];
77     }
78
79     /**
80      * This method is required by the base class definition, but it does not
81      * releases any memory resources at all.
82      */

83     public void close() {
84         // NOP
85
}
86 }
87
Popular Tags