1 32 package com.imagero.uio.buffer; 33 34 import com.imagero.uio.RandomAccessFactory; 35 import com.imagero.uio.io.IOutils; 36 37 import java.io.IOException ; 38 import java.net.URL ; 39 40 49 public class HTTPBufferManager extends AbstractBufferManager { 50 51 public static final int URL_TYPE_UNKNOWN = 0; 52 public static final int URL_TYPE_HTTP = 1; 53 public static final int URL_TYPE_FILE = 2; 54 55 URL url; 56 57 int lastBufferSize; 58 59 Integer maxKey = new Integer (0); 60 61 int urlType; 62 63 70 public HTTPBufferManager(URL url) throws IOException { 71 this(url, defaultBufferSize); 72 } 73 74 82 public HTTPBufferManager(URL url, int dsLength) throws IOException { 83 this.bufferSize = dsLength; 84 this.url = url; 85 if("http".equals(url.getProtocol())) { 86 urlType = URL_TYPE_HTTP; 87 } 88 else if("file".equals(url.getProtocol())) { 89 urlType = URL_TYPE_FILE; 90 ro = RandomAccessFactory.createRO(url.getFile()); 91 } 92 else { 93 throw new IOException ("unsupported protocol: " + url.getProtocol()); 94 } 95 96 Buffer uds = createBuffer(url, 0, dsLength); 97 accessManager.put(new Integer (0), uds); 98 } 99 100 protected Buffer createBuffer(URL url, int offset, int dsLength) { 101 switch(urlType) { 102 case URL_TYPE_HTTP: 103 return new HTTPBuffer(url, offset, dsLength); 104 case URL_TYPE_FILE: 105 return new RABufferRO(ro, offset, dsLength); 106 default: 107 return null; 108 } 109 } 110 111 120 public byte[] getData(int i) throws IOException { 121 Integer key = new Integer (i); 122 Buffer b = (accessManager.get(key)); 123 if(b == null) { 124 b = createBuffer(url, bufferSize * i, bufferSize); 125 accessManager.put(key, b); 126 if(key.intValue() > maxKey.intValue()) { 127 maxKey = key; 128 } 129 } 130 return b.getData(); 131 } 132 133 140 public int getDataLength(int i) { 141 return bufferSize; 142 } 143 144 151 public int getIndex(long pos) { 152 if(pos < 0) { 153 return -1; 154 } 155 return (int) (pos / bufferSize); 156 } 157 158 162 public long getLength() { 163 return maxKey.intValue() * bufferSize; 164 } 165 166 173 public long getDataStart(int i) { 174 return bufferSize * i; 175 } 176 177 public void close() { 178 IOutils.closeStream(ro); 179 } 180 } 181 | Popular Tags |