KickJava   Java API By Example, From Geeks To Geeks.

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


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

44 public class RABufferManager extends AbstractBufferManager {
45
46     Integer JavaDoc maxKey = new Integer JavaDoc(0);
47
48     /**
49      * create BufferManager for RandomAccess (with standard buffer length of 50k)
50      *
51      * @see RABufferRO
52      */

53     public RABufferManager(RandomAccessRO ro, long offset, int length) {
54         this(ro, offset, length, defaultBufferSize);
55     }
56
57     /**
58      * create BufferManager for RandomAccess
59      *
60      * @param dsLength standard length of one data block of Buffer
61      *
62      * @see RABuffer
63      */

64     public RABufferManager(RandomAccessRO ro, long offset, int length, int dsLength) {
65         this.bufferSize = dsLength;
66         this.ro = ro;
67         this.offset = offset;
68         this.length = length;
69
70         RABufferRO rads = createBuffer(ro, offset, dsLength);
71         accessManager.add(rads);
72     }
73
74     protected RABufferRO createBuffer(RandomAccessRO ro, long offset, int dsLength) {
75         return new RABufferRO(ro, offset, dsLength);
76     }
77
78     /**
79      * get data (as byte array) from i'th Buffer
80      *
81      * @param i Buffer index
82      *
83      * @return byte array
84      *
85      * @throws IOException if i'th Buffer not exists and couldn't be read
86      */

87     public byte[] getData(int i) throws IOException JavaDoc {
88         try {
89             return getDataImpl(i);
90         }
91         catch(ArrayIndexOutOfBoundsException JavaDoc ex) {
92             throw ex;
93         }
94     }
95
96     /**
97      * read i'th Buffer
98      *
99      * @param i Buffer index
100      *
101      * @return byte array
102      *
103      * @throws IOException
104      */

105     protected byte[] getDataImpl(int i) throws IOException JavaDoc {
106         Integer JavaDoc key = new Integer JavaDoc(i);
107         RABufferRO uds = ((RABufferRO) (accessManager.get(key)));
108         if(uds == null) {
109             uds = createBuffer(ro, offset + bufferSize * i, bufferSize);
110             accessManager.put(key, uds);
111             if(key.intValue() > maxKey.intValue()) {
112                 maxKey = key;
113             }
114         }
115         return uds.getData();
116     }
117
118     /**
119      * get length of i'th Buffer
120      *
121      * @param i Buffer index
122      *
123      * @return dsLength
124      */

125     public int getDataLength(int i) {
126         return bufferSize;
127     }
128
129     /**
130      * get index of Buffer which contains <code>pos</code>
131      *
132      * @param pos
133      *
134      * @return index of Buffer or -1
135      */

136     public int getIndex(long pos) {
137         if(pos < 0) {
138             return -1;
139         }
140         return (int) (pos / bufferSize);
141     }
142
143     /**
144      * get length of data
145      */

146     public long getLength() {
147         return length;
148     }
149
150     /**
151      * get start of i'th Buffer in byte<br>
152      * I assume here that length of each Buffer (except last one) equals to <code>dsLength</code>
153      *
154      * @param i
155      */

156     public long getDataStart(int i) {
157 // if(i >= accessManager.getCount()) {
158
// throw new ArrayIndexOutOfBoundsException(i + " >= " + ds.size());
159
// }
160
return bufferSize * i;
161     }
162
163     public void close() {
164         IOutils.closeStream(ro);
165         accessManager.clear();
166         maxKey = new Integer JavaDoc(0);
167     }
168
169     public void clear() {
170         accessManager.clear();
171         maxKey = new Integer JavaDoc(0);
172     }
173
174     public int getByteOrder() {
175         return ro.getByteOrder();
176     }
177 }
178
Popular Tags