1 16 package org.apache.commons.vfs.provider.local; 17 18 import org.apache.commons.vfs.FileSystemException; 19 import org.apache.commons.vfs.provider.AbstractRandomAccessContent; 20 import org.apache.commons.vfs.util.RandomAccessMode; 21 22 import java.io.EOFException ; 23 import java.io.File ; 24 import java.io.FileNotFoundException ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.RandomAccessFile ; 28 29 34 class LocalFileRandomAccessContent extends AbstractRandomAccessContent 35 { 36 final private RandomAccessFile raf; 38 final private InputStream rafis; 39 40 LocalFileRandomAccessContent(final File localFile, final RandomAccessMode mode) throws FileSystemException 41 { 42 super(mode); 43 44 StringBuffer modes = new StringBuffer (2); 45 if (mode.requestRead()) 46 { 47 modes.append('r'); 48 } 49 if (mode.requestWrite()) 50 { 51 modes.append('w'); 52 } 53 54 try 55 { 56 raf = new RandomAccessFile (localFile, modes.toString()); 57 rafis = new InputStream () 58 { 59 public int read() throws IOException 60 { 61 try 62 { 63 return raf.readByte(); 64 } 65 catch (EOFException e) 66 { 67 return -1; 68 } 69 } 70 71 public long skip(long n) throws IOException 72 { 73 raf.seek(raf.getFilePointer() + n); 74 return n; 75 } 76 77 public void close() throws IOException 78 { 79 raf.close(); 80 } 81 82 public int read(byte b[]) throws IOException 83 { 84 return raf.read(b); 85 } 86 87 public int read(byte b[], int off, int len) throws IOException 88 { 89 return raf.read(b, off, len); 90 } 91 }; 92 } 93 catch (FileNotFoundException e) 94 { 95 throw new FileSystemException("vfs.provider/random-access-open-failed.error", localFile); 96 } 97 } 98 99 public long getFilePointer() throws IOException 100 { 101 return raf.getFilePointer(); 102 } 103 104 public void seek(long pos) throws IOException 105 { 106 raf.seek(pos); 107 } 108 109 public long length() throws IOException 110 { 111 return raf.length(); 112 } 113 114 public void close() throws IOException 115 { 116 raf.close(); 117 } 118 119 public byte readByte() throws IOException 120 { 121 return raf.readByte(); 122 } 123 124 public char readChar() throws IOException 125 { 126 return raf.readChar(); 127 } 128 129 public double readDouble() throws IOException 130 { 131 return raf.readDouble(); 132 } 133 134 public float readFloat() throws IOException 135 { 136 return raf.readFloat(); 137 } 138 139 public int readInt() throws IOException 140 { 141 return raf.readInt(); 142 } 143 144 public int readUnsignedByte() throws IOException 145 { 146 return raf.readUnsignedByte(); 147 } 148 149 public int readUnsignedShort() throws IOException 150 { 151 return raf.readUnsignedShort(); 152 } 153 154 public long readLong() throws IOException 155 { 156 return raf.readLong(); 157 } 158 159 public short readShort() throws IOException 160 { 161 return raf.readShort(); 162 } 163 164 public boolean readBoolean() throws IOException 165 { 166 return raf.readBoolean(); 167 } 168 169 public int skipBytes(int n) throws IOException 170 { 171 return raf.skipBytes(n); 172 } 173 174 public void readFully(byte b[]) throws IOException 175 { 176 raf.readFully(b); 177 } 178 179 public void readFully(byte b[], int off, int len) throws IOException 180 { 181 raf.readFully(b, off, len); 182 } 183 184 public String readUTF() throws IOException 185 { 186 return raf.readUTF(); 187 } 188 189 public void writeDouble(double v) throws IOException 190 { 191 raf.writeDouble(v); 192 } 193 194 public void writeFloat(float v) throws IOException 195 { 196 raf.writeFloat(v); 197 } 198 199 public void write(int b) throws IOException 200 { 201 raf.write(b); 202 } 203 204 public void writeByte(int v) throws IOException 205 { 206 raf.writeByte(v); 207 } 208 209 public void writeChar(int v) throws IOException 210 { 211 raf.writeChar(v); 212 } 213 214 public void writeInt(int v) throws IOException 215 { 216 raf.writeInt(v); 217 } 218 219 public void writeShort(int v) throws IOException 220 { 221 raf.writeShort(v); 222 } 223 224 public void writeLong(long v) throws IOException 225 { 226 raf.writeLong(v); 227 } 228 229 public void writeBoolean(boolean v) throws IOException 230 { 231 raf.writeBoolean(v); 232 } 233 234 public void write(byte b[]) throws IOException 235 { 236 raf.write(b); 237 } 238 239 public void write(byte b[], int off, int len) throws IOException 240 { 241 raf.write(b, off, len); 242 } 243 244 public void writeBytes(String s) throws IOException 245 { 246 raf.writeBytes(s); 247 } 248 249 public void writeChars(String s) throws IOException 250 { 251 raf.writeChars(s); 252 } 253 254 public void writeUTF(String str) throws IOException 255 { 256 raf.writeUTF(str); 257 } 258 259 public InputStream getInputStream() throws IOException 260 { 261 return rafis; 262 } 263 } 264 | Popular Tags |