KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
35
36 /**
37  * DefaultBufferManager.java
38  *
39  * @author Andrei Kouznetsov
40  */

41 public class DefaultBufferManager implements MutableBufferManager {
42
43     MemoryAccessManager accessManager;
44
45     int[] startIndex;
46     int length;
47
48     public DefaultBufferManager(Buffer[] ds) {
49         accessManager = MemoryAccessManager.createMemoryAccessManager(MemoryAccessManager.DROP_NEVER);
50
51         for (int i = 0; i < ds.length; i++) {
52             accessManager.put(new Integer JavaDoc(i), ds[i]);
53         }
54
55         startIndex = new int[ds.length + 1];
56
57         for (int i = 1; i < startIndex.length; i++) {
58             startIndex[i] = startIndex[i - 1] + ds[i - 1].length();
59         }
60
61         if (ds.length > 0) {
62             length = startIndex[startIndex.length - 1];
63         }
64     }
65
66     public long getDataStart(int i) {
67         return startIndex[i];
68     }
69
70     /**
71      * get data (as byte array) from i'th Buffer
72      *
73      * @param i Buffer index
74      *
75      * @return byte array
76      */

77     public byte[] getData(int i) throws IOException JavaDoc {
78         try {
79             Buffer buffer = accessManager.get(new Integer JavaDoc(i));
80             if (buffer == null) {
81                 return empty;
82             }
83             return buffer.getData();
84         }
85         catch (IOException JavaDoc ex) {
86             ex.printStackTrace();
87             return empty;
88         }
89     }
90
91     /**
92      * count of Buffers contained in this BufferManager
93      *
94      * @return int
95      */

96     public int getCount() {
97         return accessManager.getCount();
98     }
99
100     /**
101      * get length of i'th Buffer in byte
102      *
103      * @param i Buffer index
104      * @return int
105      */

106     public int getDataLength(int i) {
107         return startIndex[i + 1] - startIndex[i];
108     }
109
110     /**
111      * get index of Buffer which contains index <code>pos</code>
112      *
113      * @param pos
114      *
115      * @return index or -1 if <code>pos</code> is negative or greater then or equal to <code>getLength()</code>
116      */

117     public int getIndex(long pos) {
118         if (pos < 0) {
119             return -1;
120         }
121         for (int i = 0; i < startIndex.length; i++) {
122             if (startIndex[i] > pos) {
123                 return i - 1;
124             }
125         }
126         return -1;
127     }
128
129     /**
130      * length of all data
131      *
132      * @return int
133      */

134     public long getLength() {
135         return length;
136     }
137
138     /**
139      * releases all buffers
140      */

141     public void close() {
142         accessManager.clear();
143     }
144
145     /**
146      * does currently nothing
147      *
148      * @param from
149      * @param to
150      */

151     public void setDirty(long from, long to) {
152
153     }
154
155     /**
156      * does currently nothing
157      *
158      * @param index
159      */

160     public void setDirty(int index) {
161
162     }
163
164     /**
165      * does currently nothing
166      *
167      * @throws IOException
168      */

169     public void flush() throws IOException JavaDoc {
170
171     }
172
173     /**
174      * clears all buffers (same as close)
175      */

176     public void clear() {
177         accessManager.clear();
178     }
179
180     /**
181      *
182      * @param start
183      * @param end
184      */

185     public void clear(long start, long end) {
186         //get first buffer to clear
187
int bs = getIndex(start);
188         long ps = getDataStart(bs);
189         if(ps < start) {
190             bs++;
191         }
192         //get last buffer to clear
193
int eb = getIndex(end);
194         long pe = getDataStart(eb);
195         int length = getDataLength(eb);
196         if(pe + length > end) {
197             eb--;
198         }
199         for(int i = bs; i <= eb; i++) {
200             Integer JavaDoc key = new Integer JavaDoc(i);
201             accessManager.drop(key);
202         }
203     }
204
205     /**
206      * @return current buffer count
207      */

208     public int getMaxCache() {
209         return getCount();
210     }
211
212     /**
213      * does nothing, because Buffer is never dropped
214      */

215     public void setMaxCache(int max) {
216     }
217 }
218
Popular Tags