KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > imagero > uio > buffer > HTTPBufferManager


1 /*
2  * Copyright (c) Andrey Kuznetsov. All Rights Reserved.
3  *
4  * http://uio.imagero.com
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * o Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * o Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * o Neither the name of imagero Andrei Kouznetsov nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

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 JavaDoc;
38 import java.net.URL JavaDoc;
39
40 /**
41  * currently supported protocols: file and http
42  *
43  * @author Andrei Kouznetsov
44  * @see HTTPBuffer
45  * <br>
46  * Date: 12.11.2003
47  * Time: 12:45:21
48  */

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 JavaDoc url;
56
57     int lastBufferSize;
58
59     Integer JavaDoc maxKey = new Integer JavaDoc(0);
60
61     int urlType;
62
63     /**
64      * create BufferManager for InputStream (with standard length of 50k)
65      *
66      * @param url URL
67      *
68      * @see HTTPBuffer
69      */

70     public HTTPBufferManager(URL JavaDoc url) throws IOException JavaDoc {
71         this(url, defaultBufferSize);
72     }
73
74     /**
75      * create BufferManager for InputStream
76      *
77      * @param dsLength standard length of one data block of Buffer
78      * @param url URL
79      *
80      * @see HTTPBuffer
81      */

82     public HTTPBufferManager(URL JavaDoc url, int dsLength) throws IOException JavaDoc {
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 JavaDoc("unsupported protocol: " + url.getProtocol());
94         }
95
96         Buffer uds = createBuffer(url, 0, dsLength);
97         accessManager.put(new Integer JavaDoc(0), uds);
98     }
99
100     protected Buffer createBuffer(URL JavaDoc 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     /**
112      * get data (as byte array) from i'th Buffer
113      *
114      * @param i Buffer index
115      *
116      * @return byte array
117      *
118      * @throws IOException if i'th Buffer not exists and couldn't be read from InputStream
119      */

120     public byte[] getData(int i) throws IOException JavaDoc {
121         Integer JavaDoc key = new Integer JavaDoc(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     /**
134      * get length of i'th Buffer
135      *
136      * @param i Buffer index
137      *
138      * @return dsLength
139      */

140     public int getDataLength(int i) {
141         return bufferSize;
142     }
143
144     /**
145      * get index of Buffer which contains <code>pos</code>
146      *
147      * @param pos
148      *
149      * @return index of Buffer or -1
150      */

151     public int getIndex(long pos) {
152         if(pos < 0) {
153             return -1;
154         }
155         return (int) (pos / bufferSize);
156     }
157
158     /**
159      * get length of data of all already read Buffer together (may change)
160      *
161      */

162     public long getLength() {
163         return maxKey.intValue() * bufferSize;
164     }
165
166     /**
167      * get start of i'th Buffer in byte<br>
168      * I assume here that length of each Buffer (except last one) equals to <code>dsLength</code>
169      *
170      * @param i
171      *
172      */

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