KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > imagero > uio > buffer > arrays > AbstractArrayBufferManager


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.arrays;
33
34 import com.imagero.uio.RandomAccessFactory;
35 import com.imagero.uio.Sys;
36 import com.imagero.uio.buffer.Buffer;
37 import com.imagero.uio.buffer.MemoryAccessManager;
38 import com.imagero.uio.buffer.MutableBuffer;
39 import com.imagero.uio.buffer.MutableBufferManager;
40 import com.imagero.uio.buffer.MutableByteBuffer;
41
42 import java.io.IOException JavaDoc;
43 import java.util.Enumeration JavaDoc;
44
45 /**
46  * @author Andrei Kouznetsov
47  * Date: 02.06.2004
48  * Time: 11:37:27
49  */

50 public abstract class AbstractArrayBufferManager implements MutableBufferManager {
51     int tileSize;
52     protected int unitSize;
53
54     MemoryAccessManager accessManager;
55
56     private int byteOrder = RandomAccessFactory.BIG_ENDIAN;
57     int offset;
58     int length;
59     protected static int TILE_SIZE = 4096;
60
61
62
63     /**
64      * read block of data into byte array
65      *
66      * @param b byte array
67      * @param index index of Buffer to read
68      */

69     protected void readLE(byte[] b, int index) {
70         int start = (int) getDataStart(index);
71         int c = 0;
72         long length = getLength() / getUnitSize();
73         long max = Math.min(length - start, tileSize);
74         for (int i = 0; i < max; i++) {
75             c = readUnitLE(start + i, b, c);
76         }
77     }
78
79     public long getDataStart(int i) {
80         return offset + i * tileSize * unitSize;
81     }
82
83     public int getIndex(long pos) {
84         return (int) (pos / (tileSize * unitSize));
85     }
86
87     /**
88      * mark region as dirty (changed)
89      *
90      * @param from start of changed region
91      * @param to end of changed region
92      */

93     public void setDirty(long from, long to) {
94         int start = getIndex(from);
95         int end = getIndex(to);
96         for (int i = start; i <= end; i++) {
97             MutableBuffer buffer = (MutableBuffer) accessManager.get(i);
98             if (buffer != null) {
99                 buffer.setDirty();
100             }
101         }
102     }
103
104     /**
105      * mark Buffer as dirty (changed)
106      *
107      * @param index Buffer to mark as dirty
108      */

109     public void setDirty(int index) {
110         MutableBuffer buffer = (MutableBuffer) accessManager.get(index);
111         if(buffer != null) {
112             buffer.setDirty();
113         }
114     }
115
116     public void flush() {
117         flush(false);
118     }
119
120     /**
121      * read block of data into byte array
122      *
123      * @param b byte array
124      * @param index tile index to read
125      */

126     protected void readBE(byte[] b, int index) {
127         int start = (int) getDataStart(index);
128         int c = 0;
129         long length = getLength() / getUnitSize();
130         long max = Math.min(length - start, tileSize);
131         for (int i = 0; i < max; i++) {
132             c = readUnitBE(start + i, b, c);
133         }
134     }
135
136     /**
137      * read appropriate unit (short, int or long) in BIG_ENDIAN order
138      *
139      * @param offset offset in source array
140      * @param dest byte array (destination)
141      * @param destOffset offset in destination array
142      * @return offset in destination array (updated)
143      */

144     protected abstract int readUnitBE(int offset, byte[] dest, int destOffset);
145
146     /**
147      * read appropriate unit (short, int or long) in LITTLE_ENDIAN order
148      *
149      * @param offset offset in source array
150      * @param dest byte array (destination)
151      * @param destOffset offset in destination array
152      * @return offset in destination array (updated)
153      */

154     protected abstract int readUnitLE(int offset, byte[] dest, int destOffset);
155
156     public int getCount() {
157         return accessManager.getCount();
158     }
159
160     /**
161      * get length of i'th Buffer
162      *
163      * @param i Buffer index
164      * @return length of i'th Buffer (in bytes)
165      */

166     public int getDataLength(int i) {
167         int count = accessManager.getCount();
168         if (i < count - 1 || length % tileSize == 0) {
169             return tileSize * unitSize;
170         }
171         else {
172             return (length % tileSize) * unitSize;
173         }
174     }
175
176     /**
177      * read block of data into byte array.
178      *
179      * @param b byte array
180      * @param index index of Buffer to read
181      */

182     protected void read(byte[] b, int index) {
183         if (byteOrder == RandomAccessFactory.LITTLE_ENDIAN) {
184             readLE(b, index);
185         }
186         else if (byteOrder == RandomAccessFactory.BIG_ENDIAN) {
187             readBE(b, index);
188         }
189         else {
190             throw new RuntimeException JavaDoc("unknown byte order:" + byteOrder);
191         }
192     }
193
194     public byte[] getData(int i) throws IOException JavaDoc {
195         Buffer buffer = accessManager.get(i);
196         if (buffer == null) {
197             byte[] b = new byte[getDataLength(i)];
198             read(b, i);
199             accessManager.put(new Integer JavaDoc(i), new MutableByteBuffer(b));
200         }
201         return accessManager.get(i).getData();
202     }
203
204     /**
205      * write block of data back to int array
206      *
207      * @param b data to write
208      * @param index tile index to write
209      */

210     protected void write(byte[] b, int index) {
211         if (byteOrder == RandomAccessFactory.LITTLE_ENDIAN) {
212             writeLE(b, index);
213         }
214         else if (byteOrder == RandomAccessFactory.BIG_ENDIAN) {
215             writeBE(b, index);
216         }
217         else {
218             throw new RuntimeException JavaDoc("unknown byte order:" + byteOrder);
219         }
220     }
221
222     /**
223      * write back dirty data and possibly free resources
224      *
225      * @param free if true free resources
226      */

227     public void flush(boolean free) {
228         Enumeration JavaDoc keys = accessManager.keys();
229         while (keys.hasMoreElements()) {
230             Object JavaDoc key = keys.nextElement();
231             if (accessManager.isDirty(key)) {
232                 try {
233                     write(accessManager.get(key).getData(), ((Integer JavaDoc)key).intValue());
234                 }
235                 catch (IOException JavaDoc ex) {
236                     ex.printStackTrace();
237                 }
238             }
239         }
240         if (free) {
241             accessManager.clear();
242         }
243     }
244
245     /**
246      * write back dirty data and free resources
247      */

248     public void close() {
249         Enumeration JavaDoc keys = accessManager.keys();
250         while (keys.hasMoreElements()) {
251             Object JavaDoc key = keys.nextElement();
252             if (accessManager.isDirty(key)) {
253                 try {
254                     write(accessManager.get(key).getData(), ((Integer JavaDoc)key).intValue());
255                 }
256                 catch (IOException JavaDoc ex) {
257                     ex.printStackTrace();
258                 }
259             }
260         }
261         accessManager.clear();
262     }
263
264     public void clear() {
265         flush(true);
266     }
267
268     public void clear(long start, long end) {
269         //get first buffer to clear
270
int bs = getIndex(start);
271         long ps = getDataStart(bs);
272         if(ps < start) {
273             bs++;
274         }
275         //get last buffer to clear
276
int eb = getIndex(end);
277         long pe = getDataStart(eb);
278         int length = getDataLength(eb);
279         if(pe + length > end) {
280             eb--;
281         }
282         for(int i = bs; i <= eb; i++) {
283             Integer JavaDoc key = new Integer JavaDoc(i);
284             if (accessManager.isDirty(key)) {
285                 try {
286                     write(accessManager.get(key).getData(), key.intValue());
287                 }
288                 catch (IOException JavaDoc ex) {
289                     ex.printStackTrace();
290                 }
291             }
292             accessManager.drop(key);
293         }
294     }
295
296     protected void writeLE(byte[] b, int index) {
297         int start = (int) getDataStart(index);
298         int c = 0;
299         final int len = b.length / unitSize;
300         for (int i = 0; i < len; i++) {
301             c = writeUnitLE(b, c, start + i);
302         }
303     }
304
305     protected abstract int writeUnitLE(byte[] b, int c, int offset);
306
307     protected void writeBE(byte[] b, int index) {
308         int start = (int) getDataStart(index);
309         int c = 0;
310         final int len = b.length / unitSize;
311         for (int i = 0; i < len; i++) {
312             c = writeUnitBE(b, c, start + i);
313         }
314     }
315
316     protected abstract int writeUnitBE(byte[] b, int c, int offset);
317
318     /**
319      * Get length of data
320      *
321      * @return length of data in bytes
322      */

323     public long getLength() {
324         return length * unitSize;
325     }
326
327     /**
328      * get unit size
329      *
330      * @return 2 for short, 4 for int and 8 for long data
331      */

332     public int getUnitSize() {
333         return unitSize;
334     }
335
336     public int getByteOrder() {
337         return byteOrder;
338     }
339
340     public void setByteOrder(int byteOrder) {
341         this.byteOrder = byteOrder;
342     }
343
344
345     static void printHex(long[] vl) {
346         for (int i = 0; i < vl.length; i++) {
347             Sys.out.print(Long.toHexString(vl[i]));
348             Sys.out.print(" ");
349         }
350         Sys.out.println("\n*************************");
351     }
352
353     static void printHex(int[] vi) {
354         for (int i = 0; i < vi.length; i++) {
355             Sys.out.print(Integer.toHexString(vi[i]));
356             Sys.out.print(" ");
357         }
358         Sys.out.println("\n*************************");
359     }
360
361     static void printHex(float[] vf) {
362         for (int i = 0; i < vf.length; i++) {
363             Sys.out.print(vf[i]);
364             Sys.out.print(" ");
365         }
366         Sys.out.println();
367
368         for (int i = 0; i < vf.length; i++) {
369             Sys.out.print(Integer.toHexString(Float.floatToIntBits(vf[i])));
370             Sys.out.print(" ");
371         }
372         Sys.out.println("\n*************************");
373     }
374
375     static void printHex(double[] vd) {
376         for (int i = 0; i < vd.length; i++) {
377             Sys.out.print(vd[i]);
378             Sys.out.print(" ");
379         }
380         Sys.out.println();
381         for (int i = 0; i < vd.length; i++) {
382             Sys.out.print(Long.toHexString(Double.doubleToLongBits(vd[i])));
383             Sys.out.print(" ");
384         }
385         Sys.out.println("\n*************************");
386     }
387
388     static void printHex(short[] vs) {
389         for (int i = 0; i < vs.length; i++) {
390             Sys.out.print(Integer.toHexString(vs[i] & 0xFFFF));
391             Sys.out.print(" ");
392         }
393         Sys.out.println("\n*************************");
394     }
395
396     static void printHex(char[] vs) {
397         for (int i = 0; i < vs.length; i++) {
398             Sys.out.print(Integer.toHexString(vs[i]));
399             Sys.out.print(" ");
400         }
401         Sys.out.println("\n*************************");
402     }
403
404     static void printHex(byte[] vb) {
405         for (int i = 0; i < vb.length; i++) {
406             if (vb[i] == 0) {
407                 Sys.out.print("00");
408             }
409             else {
410                 Sys.out.print(Integer.toHexString(vb[i] & 0xFF));
411             }
412             Sys.out.print(" ");
413         }
414         Sys.out.println("\n*************************");
415     }
416
417
418     public int getMaxCache() {
419         return accessManager.getMaxBufferCount();
420     }
421
422     public void setMaxCache(int max) {
423         accessManager.setMaxBufferCount(max);
424     }
425 }
426
Popular Tags