001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.tools.randomaccess;
031:
032: import java.io.File;
033: import java.io.FileNotFoundException;
034: import java.io.IOException;
035: import java.util.logging.Level;
036: import java.util.logging.Logger;
037:
038: /**
039: * Implements random access to a file.
040: */
041: public class RandomAccessFile extends AbstractRandomAccess {
042:
043: /** The logger to be used in this package */
044: private static Logger Log = PACKAGE.Log;
045:
046: /**
047: * The wrapped RandomAccessFile of the java library
048: */
049: private java.io.RandomAccessFile fileAccess;
050:
051: /**
052: * Flag if this is used read only
053: */
054: private boolean readOnly = false;
055:
056: private File file;
057:
058: /**
059: * @param file
060: * to open for random access
061: * @throws FileNotFoundException
062: * if file was not found or the file is locked by a different
063: * process
064: */
065: public RandomAccessFile(File file) throws IOException {
066: this (file, true);
067: }
068:
069: /**
070: * @param file
071: * to open for random access
072: * @throws FileNotFoundException
073: * if file was not found or the file is locked by a different
074: * process
075: */
076: public RandomAccessFile(File file, boolean create)
077: throws IOException {
078: this .file = file;
079: if (Log.isLoggable(Level.FINEST)) {
080: Log.log(Level.FINEST, "create random access "
081: + file.getAbsolutePath(), new RuntimeException());
082: }
083: if (create && !file.exists()) {
084: File dir = file.getParentFile();
085: if ((dir != null) && !dir.exists()) {
086: dir.mkdirs();
087: }
088: file.createNewFile();
089: }
090: if (!file.exists()) {
091: throw new FileNotFoundException(
092: "file does not exist or can't be created");
093: }
094: if (file.canWrite()) {
095: try {
096: fileAccess = new java.io.RandomAccessFile(file, "rw");
097: return;
098: } catch (IOException e) {
099: // canWrite() doesn't check for user permissions
100: // try again with readonly
101: }
102: }
103: fileAccess = new java.io.RandomAccessFile(file, "r");
104: readOnly = true;
105: }
106:
107: /*
108: * (non-Javadoc)
109: *
110: * @see de.intarsys.tools.randomaccess.IRandomAccessData#seek(long)
111: */
112: public void seek(long offset) throws IOException {
113: fileAccess.seek(offset);
114: }
115:
116: public void seekBy(long delta) throws IOException {
117: seek(fileAccess.getFilePointer() + delta);
118: }
119:
120: /*
121: * (non-Javadoc)
122: *
123: * @see de.intarsys.tools.randomaccess.IRandomAccessData#read()
124: */
125: public int read() throws IOException {
126: return fileAccess.read();
127: }
128:
129: /*
130: * (non-Javadoc)
131: *
132: * @see de.intarsys.tools.randomaccess.IRandomAccessData#getOffset()
133: */
134: public long getOffset() throws IOException {
135: return fileAccess.getFilePointer();
136: }
137:
138: /*
139: * (non-Javadoc)
140: *
141: * @see de.intarsys.tools.randomaccess.IRandomAccessData#getLength()
142: */
143: public long getLength() throws IOException {
144: return fileAccess.length();
145: }
146:
147: /*
148: * (non-Javadoc)
149: *
150: * @see de.intarsys.tools.randomaccess.IRandomAccessData#read(byte[])
151: */
152: public int read(byte[] buffer) throws IOException {
153: return fileAccess.read(buffer);
154: }
155:
156: /*
157: * (non-Javadoc)
158: *
159: * @see de.intarsys.tools.randomaccess.IRandomAccessData#read(byte[], int,
160: * int)
161: */
162: public int read(byte[] buffer, int start, int numBytes)
163: throws IOException {
164: return fileAccess.read(buffer, start, numBytes);
165: }
166:
167: /*
168: * (non-Javadoc)
169: *
170: * @see de.intarsys.tools.randomaccess.IRandomAccessData#close()
171: */
172: public void close() throws IOException {
173: if (Log.isLoggable(Level.FINEST)) {
174: Log.log(Level.FINEST, "close random access "
175: + file.getAbsolutePath(), new RuntimeException());
176: }
177: if (fileAccess != null) {
178: fileAccess.close();
179: }
180: }
181:
182: /*
183: * (non-Javadoc)
184: *
185: * @see de.intarsys.tools.randomaccess.IRandomAccess#flush()
186: */
187: public void flush() throws IOException {
188: fileAccess.getChannel().force(true);
189: }
190:
191: /*
192: * (non-Javadoc)
193: *
194: * @see de.intarsys.tools.randomaccess.IRandomAccessData#isReadOnly()
195: */
196: public boolean isReadOnly() {
197: return readOnly;
198: }
199:
200: /*
201: * (non-Javadoc)
202: *
203: * @see de.intarsys.tools.randomaccess.IRandomAccessData#write(int)
204: */
205: public void write(int b) throws IOException {
206: fileAccess.write(b);
207: }
208:
209: /*
210: * (non-Javadoc)
211: *
212: * @see de.intarsys.tools.randomaccess.IRandomAccessData#write(byte[])
213: */
214: public void write(byte[] buffer) throws IOException {
215: fileAccess.write(buffer);
216: }
217:
218: /*
219: * (non-Javadoc)
220: *
221: * @see de.intarsys.tools.randomaccess.IRandomAccessData#write(byte[], int,
222: * int)
223: */
224: public void write(byte[] buffer, int start, int numBytes)
225: throws IOException {
226: fileAccess.write(buffer, start, numBytes);
227: }
228:
229: /*
230: * (non-Javadoc)
231: *
232: * @see de.intarsys.tools.randomaccess.IRandomAccessData#setLength(int)
233: */
234: public void setLength(long newLength) throws IOException {
235: fileAccess.setLength(newLength);
236: }
237: }
|