1 29 30 package com.caucho.vfs; 31 32 import java.io.FileInputStream ; 33 import java.io.FileOutputStream ; 34 import java.io.IOException ; 35 import java.io.InputStream ; 36 import java.io.OutputStream ; 37 import java.io.RandomAccessFile ; 38 39 42 public class FileRandomAccessStream extends RandomAccessStream { 43 private RandomAccessFile _file; 44 private OutputStream _os; 45 private InputStream _is; 46 47 public FileRandomAccessStream(RandomAccessFile file) 48 { 49 _file = file; 50 } 51 52 public RandomAccessFile getRandomAccessFile() 53 { 54 return _file; 55 } 56 57 60 public long getLength() 61 throws IOException 62 { 63 return _file.length(); 64 } 65 66 69 public int read(byte []buffer, int offset, int length) 70 throws IOException 71 { 72 return _file.read(buffer, offset, length); 73 } 74 75 78 public int read(long fileOffset, byte []buffer, int offset, int length) 79 throws IOException 80 { 81 _file.seek(fileOffset); 82 83 return _file.read(buffer, offset, length); 84 } 85 86 89 public void write(byte []buffer, int offset, int length) 90 throws IOException 91 { 92 _file.write(buffer, offset, length); 93 } 94 95 98 public void write(long fileOffset, byte []buffer, int offset, int length) 99 throws IOException 100 { 101 _file.seek(fileOffset); 102 103 _file.write(buffer, offset, length); 104 } 105 106 109 public boolean seek(long position) 110 { 111 try { 112 _file.seek(position); 113 114 return true; 115 } catch (IOException e) { 116 return false; 117 } 118 } 119 120 123 public OutputStream getOutputStream() 124 throws IOException 125 { 126 if (_os == null) 127 _os = new FileOutputStream (_file.getFD()); 128 129 return _os; 130 } 131 132 135 public InputStream getInputStream() 136 throws IOException 137 { 138 if (_is == null) 139 _is = new FileInputStream (_file.getFD()); 140 141 return _is; 142 } 143 144 147 public int read() 148 throws IOException 149 { 150 return _file.read(); 151 } 152 153 156 public void write(int b) 157 throws IOException 158 { 159 _file.write(b); 160 } 161 162 165 public long getFilePointer() 166 throws IOException 167 { 168 return _file.getFilePointer(); 169 } 170 171 174 public void close() throws IOException 175 { 176 RandomAccessFile file = _file; 177 _file = null; 178 179 if (file != null) 180 file.close(); 181 } 182 } 183 | Popular Tags |