1 16 package org.apache.commons.vfs.provider.ftp; 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.DataInputStream ; 23 import java.io.FilterInputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 27 class FtpRandomAccessContent extends AbstractRandomAccessContent 28 { 29 private final FtpFileObject fileObject; 30 31 protected long filePointer = 0; 32 private DataInputStream dis = null; 33 private FtpFileObject.FtpInputStream mis = null; 34 35 FtpRandomAccessContent(final FtpFileObject fileObject, RandomAccessMode mode) 36 { 37 super(mode); 38 39 this.fileObject = fileObject; 40 } 42 43 public long getFilePointer() throws IOException 44 { 45 return filePointer; 46 } 47 48 public void seek(long pos) throws IOException 49 { 50 if (pos == filePointer) 51 { 52 return; 54 } 55 56 if (pos < 0) 57 { 58 throw new FileSystemException("vfs.provider/random-access-invalid-position.error", 59 new Object [] 60 { 61 new Long (pos) 62 }); 63 } 64 if (dis != null) 65 { 66 close(); 67 } 68 69 filePointer = pos; 70 } 71 72 private void createStream() throws IOException 73 { 74 if (dis != null) 75 { 76 return; 77 } 78 79 mis = fileObject.getInputStream(filePointer); 81 dis = new DataInputStream (new FilterInputStream (mis) 82 { 83 public int read() throws IOException 84 { 85 int ret = super.read(); 86 if (ret > -1) 87 { 88 filePointer++; 89 } 90 return ret; 91 } 92 93 public int read(byte b[]) throws IOException 94 { 95 int ret = super.read(b); 96 if (ret > -1) 97 { 98 filePointer+=ret; 99 } 100 return ret; 101 } 102 103 public int read(byte b[], int off, int len) throws IOException 104 { 105 int ret = super.read(b, off, len); 106 if (ret > -1) 107 { 108 filePointer+=ret; 109 } 110 return ret; 111 } 112 113 public void close() throws IOException 114 { 115 FtpRandomAccessContent.this.close(); 116 } 117 }); 118 } 119 120 121 public void close() throws IOException 122 { 123 if (dis != null) 124 { 125 mis.abort(); 126 127 DataInputStream oldDis = dis; 129 dis = null; 130 oldDis.close(); 131 mis = null; 132 } 133 } 134 135 public long length() throws IOException 136 { 137 return fileObject.getContent().getSize(); 138 } 139 140 public byte readByte() throws IOException 141 { 142 createStream(); 143 byte data = dis.readByte(); 144 return data; 145 } 146 147 public char readChar() throws IOException 148 { 149 createStream(); 150 char data = dis.readChar(); 151 return data; 152 } 153 154 public double readDouble() throws IOException 155 { 156 createStream(); 157 double data = dis.readDouble(); 158 return data; 159 } 160 161 public float readFloat() throws IOException 162 { 163 createStream(); 164 float data = dis.readFloat(); 165 return data; 166 } 167 168 public int readInt() throws IOException 169 { 170 createStream(); 171 int data = dis.readInt(); 172 return data; 173 } 174 175 public int readUnsignedByte() throws IOException 176 { 177 createStream(); 178 int data = dis.readUnsignedByte(); 179 return data; 180 } 181 182 public int readUnsignedShort() throws IOException 183 { 184 createStream(); 185 int data = dis.readUnsignedShort(); 186 return data; 187 } 188 189 public long readLong() throws IOException 190 { 191 createStream(); 192 long data = dis.readLong(); 193 return data; 194 } 195 196 public short readShort() throws IOException 197 { 198 createStream(); 199 short data = dis.readShort(); 200 return data; 201 } 202 203 public boolean readBoolean() throws IOException 204 { 205 createStream(); 206 boolean data = dis.readBoolean(); 207 return data; 208 } 209 210 public int skipBytes(int n) throws IOException 211 { 212 createStream(); 213 int data = dis.skipBytes(n); 214 return data; 215 } 216 217 public void readFully(byte b[]) throws IOException 218 { 219 createStream(); 220 dis.readFully(b); 221 } 222 223 public void readFully(byte b[], int off, int len) throws IOException 224 { 225 createStream(); 226 dis.readFully(b, off, len); 227 } 228 229 public String readUTF() throws IOException 230 { 231 createStream(); 232 String data = dis.readUTF(); 233 return data; 234 } 235 236 public InputStream getInputStream() throws IOException 237 { 238 createStream(); 239 return dis; 240 } 241 } | Popular Tags |