KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOutils;
35
36 import java.io.IOException JavaDoc;
37 import java.io.RandomAccessFile JavaDoc;
38
39 /**
40  * @author Andrei Kouznetsov
41  * Date: 12.11.2003
42  * Time: 12:45:21
43  */

44 public class RAFBufferManager extends AbstractBufferManager {
45
46     protected RandomAccessFile JavaDoc raf;
47
48     Integer JavaDoc maxKey = new Integer JavaDoc(0);
49
50
51     public RAFBufferManager(RandomAccessFile JavaDoc raf) throws IOException JavaDoc {
52         this(raf, 0);
53     }
54
55     public RAFBufferManager(RandomAccessFile JavaDoc raf, long offset) throws IOException JavaDoc {
56         this(raf, offset, raf.length());
57     }
58
59     /**
60      * create MutableBuffer for RandomAccessRO (with standard Buffer length of 50k)
61      *
62      * @see RABufferRO
63      */

64     public RAFBufferManager(RandomAccessFile JavaDoc raf, long offset, long length) {
65         this(raf, offset, length, defaultBufferSize);
66     }
67
68     /**
69      * create MutableBuffer for RandomAccessRO
70      *
71      * @param dsLength standard length of one data block of Buffer
72      *
73      * @see RABufferRO
74      */

75     public RAFBufferManager(RandomAccessFile JavaDoc raf, long offset, long length, int dsLength) {
76         this.bufferSize = dsLength;
77         this.raf = raf;
78         this.offset = offset;
79         this.length = length;
80
81         Buffer buffer = createBuffer(raf, offset, dsLength);
82         accessManager.put(new Integer JavaDoc(0), buffer);
83     }
84
85     protected Buffer createBuffer(RandomAccessFile JavaDoc raf, long offset, int dsLength) {
86         long maxLength = 0;
87         try {
88             maxLength = raf.length() - offset;
89         }
90         catch (IOException JavaDoc ex) {
91             ex.printStackTrace();
92         }
93         if(maxLength < 0) {
94             return new RAFBufferRO(raf, offset, 0);
95         }
96         return new RAFBufferRO(raf, offset, (int) Math.min(maxLength, dsLength));
97     }
98
99     /**
100      * get data (as byte array) from i'th Buffer
101      *
102      * @param i Buffer index
103      *
104      * @return byte array
105      *
106      * @throws java.io.IOException if i'th Buffer not exists and couldn't be read
107      */

108     public byte[] getData(int i) throws IOException JavaDoc {
109         try {
110             return getDataImpl(i);
111         } catch (ArrayIndexOutOfBoundsException JavaDoc ex) {
112             throw ex;
113         }
114     }
115
116     /**
117      * read i'th Buffer
118      *
119      * @param i Buffer index
120      *
121      * @return byte array
122      *
123      * @throws java.io.IOException
124      */

125     protected byte[] getDataImpl(int i) throws IOException JavaDoc {
126         Integer JavaDoc key = new Integer JavaDoc(i);
127         Buffer uds = (accessManager.get(key));
128         if (uds == null) {
129             uds = createBuffer(raf, offset + bufferSize * i, bufferSize);
130             if(uds == null) {
131                 return null;
132             }
133             accessManager.put(key, uds);
134             if (key.intValue() > maxKey.intValue()) {
135                 maxKey = key;
136             }
137         }
138         return uds.getData();
139     }
140
141     /**
142      * get length of i'th Buffer
143      *
144      * @param i Buffer index
145      *
146      * @return dsLength
147      */

148     public int getDataLength(int i) {
149         return bufferSize;
150     }
151
152     /**
153      * get index of Buffer which contains <code>pos</code>
154      *
155      * @param pos
156      *
157      * @return index of Buffer or -1
158      */

159     public int getIndex(long pos) {
160         if (pos < 0) {
161             return -1;
162         }
163         return (int) (pos / bufferSize);
164     }
165
166     /**
167      * get length of data
168      */

169     public long getLength() {
170         return (int) length;
171     }
172
173     /**
174      * get start of i'th Buffer in byte<br>
175      * I assume here that length of each Buffer (except last one) equals to <code>dsLength</code>
176      *
177      * @param i
178      */

179     public long getDataStart(int i) {
180 // if (i >= accessManager.getCount()) {
181
// throw new ArrayIndexOutOfBoundsException(i + " >= " + ds.size());
182
// }
183
return bufferSize * i;
184     }
185
186     public void close() {
187         IOutils.closeStream(raf);
188         clear();
189     }
190
191     public void clear() {
192         accessManager.clear();
193         maxKey = new Integer JavaDoc(0);
194     }
195 }
196
Popular Tags