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 import java.io.RandomAccessFile;
30
31 /**
32 * JRobin backend which is used to store RRD data to ordinary files on the disk. This was the
33 * default factory before 1.4.0 version<p>
34 * <p/>
35 * This backend is based on the RandomAccessFile class (java.io.* package).
36 */
37 public class RrdFileBackend extends RrdBackend {
38 /**
39 * read/write file status
40 */
41 protected boolean readOnly;
42 /**
43 * radnom access file handle
44 */
45 protected RandomAccessFile file;
46
47 /**
48 * Creates RrdFileBackend object for the given file path, backed by RandomAccessFile object.
49 *
50 * @param path Path to a file
51 * @param readOnly True, if file should be open in a read-only mode. False otherwise
52 * @throws IOException Thrown in case of I/O error
53 */
54 protected RrdFileBackend(String path, boolean readOnly) throws IOException {
55 super(path);
56 this.readOnly = readOnly;
57 this.file = new RandomAccessFile(path, readOnly ? "r" : "rw");
58 }
59
60 /**
61 * Closes the underlying RRD file.
62 *
63 * @throws IOException Thrown in case of I/O error
64 */
65 public void close() throws IOException {
66 file.close();
67 }
68
69 /**
70 * Returns canonical path to the file on the disk.
71 *
72 * @param path File path
73 * @return Canonical file path
74 * @throws IOException Thrown in case of I/O error
75 */
76 public static String getCanonicalPath(String path) throws IOException {
77 return Util.getCanonicalPath(path);
78 }
79
80 /**
81 * Returns canonical path to the file on the disk.
82 *
83 * @return Canonical file path
84 * @throws IOException Thrown in case of I/O error
85 */
86 public String getCanonicalPath() throws IOException {
87 return RrdFileBackend.getCanonicalPath(getPath());
88 }
89
90 /**
91 * Writes bytes to the underlying RRD file on the disk
92 *
93 * @param offset Starting file offset
94 * @param b Bytes to be written.
95 * @throws IOException Thrown in case of I/O error
96 */
97 protected void write(long offset, byte[] b) throws IOException {
98 file.seek(offset);
99 file.write(b);
100 }
101
102 /**
103 * Reads a number of bytes from the RRD file on the disk
104 *
105 * @param offset Starting file offset
106 * @param b Buffer which receives bytes read from the file.
107 * @throws IOException Thrown in case of I/O error.
108 */
109 protected void read(long offset, byte[] b) throws IOException {
110 file.seek(offset);
111 if (file.read(b) != b.length) {
112 throw new IOException("Not enough bytes available in file " + getPath());
113 }
114 }
115
116 /**
117 * Returns RRD file length.
118 *
119 * @return File length.
120 * @throws IOException Thrown in case of I/O error.
121 */
122 public long getLength() throws IOException {
123 return file.length();
124 }
125
126 /**
127 * Sets length of the underlying RRD file. This method is called only once, immediately
128 * after a new RRD file gets created.
129 *
130 * @param length Length of the RRD file
131 * @throws IOException Thrown in case of I/O error.
132 */
133 protected void setLength(long length) throws IOException {
134 file.setLength(length);
135 }
136 }
137