001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.vfs.provider.local;
018:
019: import org.apache.commons.vfs.FileSystemException;
020: import org.apache.commons.vfs.provider.AbstractRandomAccessContent;
021: import org.apache.commons.vfs.util.RandomAccessMode;
022:
023: import java.io.EOFException;
024: import java.io.File;
025: import java.io.FileNotFoundException;
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.io.RandomAccessFile;
029:
030: /**
031: * RandomAccess for local files
032: *
033: * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
034: * @version $Revision: 537714 $ $Date: 2007-05-13 22:51:00 -0700 (Sun, 13 May 2007) $
035: */
036: class LocalFileRandomAccessContent extends AbstractRandomAccessContent {
037: // private final LocalFile localFile;
038: final private RandomAccessFile raf;
039: final private InputStream rafis;
040:
041: LocalFileRandomAccessContent(final File localFile,
042: final RandomAccessMode mode) throws FileSystemException {
043: super (mode);
044:
045: try {
046: raf = new RandomAccessFile(localFile, mode.getModeString());
047: rafis = new InputStream() {
048: public int read() throws IOException {
049: try {
050: return raf.readByte();
051: } catch (EOFException e) {
052: return -1;
053: }
054: }
055:
056: public long skip(long n) throws IOException {
057: raf.seek(raf.getFilePointer() + n);
058: return n;
059: }
060:
061: public void close() throws IOException {
062: raf.close();
063: }
064:
065: public int read(byte b[]) throws IOException {
066: return raf.read(b);
067: }
068:
069: public int read(byte b[], int off, int len)
070: throws IOException {
071: return raf.read(b, off, len);
072: }
073:
074: public int available() throws IOException {
075: long available = raf.length()
076: - raf.getFilePointer();
077: if (available > Integer.MAX_VALUE) {
078: return Integer.MAX_VALUE;
079: }
080:
081: return (int) available;
082: }
083: };
084: } catch (FileNotFoundException e) {
085: throw new FileSystemException(
086: "vfs.provider/random-access-open-failed.error",
087: localFile);
088: }
089: }
090:
091: public long getFilePointer() throws IOException {
092: return raf.getFilePointer();
093: }
094:
095: public void seek(long pos) throws IOException {
096: raf.seek(pos);
097: }
098:
099: public long length() throws IOException {
100: return raf.length();
101: }
102:
103: public void close() throws IOException {
104: raf.close();
105: }
106:
107: public byte readByte() throws IOException {
108: return raf.readByte();
109: }
110:
111: public char readChar() throws IOException {
112: return raf.readChar();
113: }
114:
115: public double readDouble() throws IOException {
116: return raf.readDouble();
117: }
118:
119: public float readFloat() throws IOException {
120: return raf.readFloat();
121: }
122:
123: public int readInt() throws IOException {
124: return raf.readInt();
125: }
126:
127: public int readUnsignedByte() throws IOException {
128: return raf.readUnsignedByte();
129: }
130:
131: public int readUnsignedShort() throws IOException {
132: return raf.readUnsignedShort();
133: }
134:
135: public long readLong() throws IOException {
136: return raf.readLong();
137: }
138:
139: public short readShort() throws IOException {
140: return raf.readShort();
141: }
142:
143: public boolean readBoolean() throws IOException {
144: return raf.readBoolean();
145: }
146:
147: public int skipBytes(int n) throws IOException {
148: return raf.skipBytes(n);
149: }
150:
151: public void readFully(byte b[]) throws IOException {
152: raf.readFully(b);
153: }
154:
155: public void readFully(byte b[], int off, int len)
156: throws IOException {
157: raf.readFully(b, off, len);
158: }
159:
160: public String readUTF() throws IOException {
161: return raf.readUTF();
162: }
163:
164: public void writeDouble(double v) throws IOException {
165: raf.writeDouble(v);
166: }
167:
168: public void writeFloat(float v) throws IOException {
169: raf.writeFloat(v);
170: }
171:
172: public void write(int b) throws IOException {
173: raf.write(b);
174: }
175:
176: public void writeByte(int v) throws IOException {
177: raf.writeByte(v);
178: }
179:
180: public void writeChar(int v) throws IOException {
181: raf.writeChar(v);
182: }
183:
184: public void writeInt(int v) throws IOException {
185: raf.writeInt(v);
186: }
187:
188: public void writeShort(int v) throws IOException {
189: raf.writeShort(v);
190: }
191:
192: public void writeLong(long v) throws IOException {
193: raf.writeLong(v);
194: }
195:
196: public void writeBoolean(boolean v) throws IOException {
197: raf.writeBoolean(v);
198: }
199:
200: public void write(byte b[]) throws IOException {
201: raf.write(b);
202: }
203:
204: public void write(byte b[], int off, int len) throws IOException {
205: raf.write(b, off, len);
206: }
207:
208: public void writeBytes(String s) throws IOException {
209: raf.writeBytes(s);
210: }
211:
212: public void writeChars(String s) throws IOException {
213: raf.writeChars(s);
214: }
215:
216: public void writeUTF(String str) throws IOException {
217: raf.writeUTF(str);
218: }
219:
220: public InputStream getInputStream() throws IOException {
221: return rafis;
222: }
223: }
|