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