KickJava   Java API By Example, From Geeks To Geeks.

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


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
33 package com.imagero.uio.buffer;
34
35 import com.imagero.uio.RandomAccessRO;
36
37 import java.io.IOException;
38 import java.util.Enumeration;
39
40 /**
41  * @author Andrey Kuznetsov
42  */

43 public abstract class AbstractBufferManager implements BufferManager {
44     protected static int defaultBufferSize = 1024 * 50;
45
46
47     public static int getDefaultBufferSize() {
48         return RAFBufferManager.defaultBufferSize;
49     }
50
51     public static void setDefaultBufferSize(int defaultBufferSize) {
52         MutableRAFBufferManager.defaultBufferSize = defaultBufferSize;
53     }
54
55     MemoryAccessManager accessManager;
56
57     long offset;
58     long length;
59     int bufferSize;
60     RandomAccessRO ro;
61
62     public AbstractBufferManager() {
63         accessManager = createAccessManager();
64     }
65
66     protected MemoryAccessManager createAccessManager() {
67         return MemoryAccessManager.createMemoryAccessManager();
68     }
69
70     /**
71      * get count of Buffers CURRENTLY contained in this BufferManager
72      *
73      * @return int
74      */

75     public int getCount() {
76         return accessManager.getCount();
77     }
78
79     public void clear() {
80         accessManager.clear();
81     }
82
83     public void clear(long start, long end) throws IOException {
84         //get first buffer to clear
85
int bs = getStart(start);
86         //get last buffer to clear
87
int eb = getEnd(end);
88         clearImpl(bs, eb);
89     }
90
91     protected void clearImpl(int bs, int eb) throws IOException {
92         for (int i = bs; i <= eb; i++) {
93             Integer key = new Integer(i);
94             Buffer b = accessManager.get(key);
95             if (b.isDirty() && b instanceof MutableBuffer) {
96                 MutableBuffer buffer = (MutableBuffer) b;
97                 buffer.flush();
98             }
99             accessManager.drop(key);
100         }
101     }
102
103     /**
104      * get index of buffer which end is less or equals as given position
105      * @param pos position
106      * @return buffer index
107      */

108     protected int getEnd(long pos) {
109         int eb = getIndex(pos);
110         long pe = getDataStart(eb);
111         int length = getDataLength(eb);
112         if (pe + length > pos) {
113             eb--;
114         }
115         return eb;
116     }
117
118     /**
119      * get index of buffer which start is greater or equals as given position
120      * @param pos position
121      * @return buffer index
122      */

123     protected int getStart(long pos) {
124         int bs = getIndex(pos);
125         long ps = getDataStart(bs);
126         if (ps < pos) {
127             bs++;
128         }
129         return bs;
130     }
131
132     public int getMaxCache() {
133         return accessManager.getMaxBufferCount();
134     }
135
136     public void setMaxCache(int max) {
137         accessManager.setMaxBufferCount(max);
138     }
139
140     public void flush() throws IOException {
141         Enumeration enum = accessManager.keys();
142         while (enum.hasMoreElements()) {
143             Buffer b = accessManager.get(enum.nextElement());
144             if (b.isDirty() && b instanceof MutableBuffer) {
145                 MutableBuffer buffer = (MutableBuffer) b;
146                 buffer.flush();
147             }
148         }
149     }
150
151     public void setDirty(int index) {
152         Buffer b = accessManager.get(index);
153         if (b != null && b instanceof MutableBuffer) {
154             MutableBuffer buffer = (MutableBuffer) b;
155             buffer.setDirty();
156         }
157     }
158
159     public void setDirty(long from, long to) {
160         int start = getIndex(from);
161         int end = getIndex(to);
162         for (int i = start; i <= end; i++) {
163             setDirty(i);
164         }
165     }
166 }
167
Popular Tags