001 /* ============================================================
002 * JRobin : Pure java implementation of RRDTool's functionality
003 * ============================================================
004 *
005 * Project Info: http://www.jrobin.org
006 * Project Lead: Sasa Markovic (saxon@jrobin.org);
007 *
008 * (C) Copyright 2003-2005, by Sasa Markovic.
009 *
010 * Developers: Sasa Markovic (saxon@jrobin.org)
011 *
012 *
013 * This library is free software; you can redistribute it and/or modify it under the terms
014 * of the GNU Lesser General Public License as published by the Free Software Foundation;
015 * either version 2.1 of the License, or (at your option) any later version.
016 *
017 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
018 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
019 * See the GNU Lesser General Public License for more details.
020 *
021 * You should have received a copy of the GNU Lesser General Public License along with this
022 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
023 * Boston, MA 02111-1307, USA.
024 */
025
026 package org.jrobin.core;
027
028 import java.io.IOException;
029 import java.io.RandomAccessFile;
030
031 /**
032 * JRobin backend which is used to store RRD data to ordinary files on the disk. This was the
033 * default factory before 1.4.0 version<p>
034 * <p/>
035 * This backend is based on the RandomAccessFile class (java.io.* package).
036 */
037 public class RrdFileBackend extends RrdBackend {
038 /**
039 * read/write file status
040 */
041 protected boolean readOnly;
042 /**
043 * radnom access file handle
044 */
045 protected RandomAccessFile file;
046
047 /**
048 * Creates RrdFileBackend object for the given file path, backed by RandomAccessFile object.
049 *
050 * @param path Path to a file
051 * @param readOnly True, if file should be open in a read-only mode. False otherwise
052 * @throws IOException Thrown in case of I/O error
053 */
054 protected RrdFileBackend(String path, boolean readOnly) throws IOException {
055 super(path);
056 this.readOnly = readOnly;
057 this.file = new RandomAccessFile(path, readOnly ? "r" : "rw");
058 }
059
060 /**
061 * Closes the underlying RRD file.
062 *
063 * @throws IOException Thrown in case of I/O error
064 */
065 public void close() throws IOException {
066 file.close();
067 }
068
069 /**
070 * Returns canonical path to the file on the disk.
071 *
072 * @param path File path
073 * @return Canonical file path
074 * @throws IOException Thrown in case of I/O error
075 */
076 public static String getCanonicalPath(String path) throws IOException {
077 return Util.getCanonicalPath(path);
078 }
079
080 /**
081 * Returns canonical path to the file on the disk.
082 *
083 * @return Canonical file path
084 * @throws IOException Thrown in case of I/O error
085 */
086 public String getCanonicalPath() throws IOException {
087 return RrdFileBackend.getCanonicalPath(getPath());
088 }
089
090 /**
091 * Writes bytes to the underlying RRD file on the disk
092 *
093 * @param offset Starting file offset
094 * @param b Bytes to be written.
095 * @throws IOException Thrown in case of I/O error
096 */
097 protected void write(long offset, byte[] b) throws IOException {
098 file.seek(offset);
099 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 }