summaryrefslogtreecommitdiff
path: root/songdbj/org/tritonus/share/TCircularBuffer.java
diff options
context:
space:
mode:
Diffstat (limited to 'songdbj/org/tritonus/share/TCircularBuffer.java')
-rw-r--r--songdbj/org/tritonus/share/TCircularBuffer.java268
1 files changed, 0 insertions, 268 deletions
diff --git a/songdbj/org/tritonus/share/TCircularBuffer.java b/songdbj/org/tritonus/share/TCircularBuffer.java
deleted file mode 100644
index 11ebc0af01..0000000000
--- a/songdbj/org/tritonus/share/TCircularBuffer.java
+++ /dev/null
@@ -1,268 +0,0 @@
1/*
2 * TCircularBuffer.java
3 *
4 * This file is part of Tritonus: http://www.tritonus.org/
5 */
6
7/*
8 * Copyright (c) 1999 by Matthias Pfisterer
9 *
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Library General Public License as published
13 * by the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Library General Public License for more details.
20 *
21 * You should have received a copy of the GNU Library General Public
22 * License along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 */
26
27/*
28|<--- this code is formatted to fit into 80 columns --->|
29*/
30
31package org.tritonus.share;
32
33import org.tritonus.share.TDebug;
34
35
36
37public class TCircularBuffer
38{
39 private boolean m_bBlockingRead;
40 private boolean m_bBlockingWrite;
41 private byte[] m_abData;
42 private int m_nSize;
43 private long m_lReadPos;
44 private long m_lWritePos;
45 private Trigger m_trigger;
46 private boolean m_bOpen;
47
48
49
50 public TCircularBuffer(int nSize, boolean bBlockingRead, boolean bBlockingWrite, Trigger trigger)
51 {
52 m_bBlockingRead = bBlockingRead;
53 m_bBlockingWrite = bBlockingWrite;
54 m_nSize = nSize;
55 m_abData = new byte[m_nSize];
56 m_lReadPos = 0;
57 m_lWritePos = 0;
58 m_trigger = trigger;
59 m_bOpen = true;
60 }
61
62
63
64 public void close()
65 {
66 m_bOpen = false;
67 // TODO: call notify() ?
68 }
69
70
71
72 private boolean isOpen()
73 {
74 return m_bOpen;
75 }
76
77
78 public int availableRead()
79 {
80 return (int) (m_lWritePos - m_lReadPos);
81 }
82
83
84
85 public int availableWrite()
86 {
87 return m_nSize - availableRead();
88 }
89
90
91
92 private int getReadPos()
93 {
94 return (int) (m_lReadPos % m_nSize);
95 }
96
97
98
99 private int getWritePos()
100 {
101 return (int) (m_lWritePos % m_nSize);
102 }
103
104
105
106 public int read(byte[] abData)
107 {
108 return read(abData, 0, abData.length);
109 }
110
111
112
113 public int read(byte[] abData, int nOffset, int nLength)
114 {
115 if (TDebug.TraceCircularBuffer)
116 {
117 TDebug.out(">TCircularBuffer.read(): called.");
118 dumpInternalState();
119 }
120 if (! isOpen())
121 {
122 if (availableRead() > 0)
123 {
124 nLength = Math.min(nLength, availableRead());
125 if (TDebug.TraceCircularBuffer) { TDebug.out("reading rest in closed buffer, length: " + nLength); }
126 }
127 else
128 {
129 if (TDebug.TraceCircularBuffer) { TDebug.out("< not open. returning -1."); }
130 return -1;
131 }
132 }
133 synchronized (this)
134 {
135 if (m_trigger != null && availableRead() < nLength)
136 {
137 if (TDebug.TraceCircularBuffer) { TDebug.out("executing trigger."); }
138 m_trigger.execute();
139 }
140 if (!m_bBlockingRead)
141 {
142 nLength = Math.min(availableRead(), nLength);
143 }
144 int nRemainingBytes = nLength;
145 while (nRemainingBytes > 0)
146 {
147 while (availableRead() == 0)
148 {
149 try
150 {
151 wait();
152 }
153 catch (InterruptedException e)
154 {
155 if (TDebug.TraceAllExceptions)
156 {
157 TDebug.out(e);
158 }
159 }
160 }
161 int nAvailable = Math.min(availableRead(), nRemainingBytes);
162 while (nAvailable > 0)
163 {
164 int nToRead = Math.min(nAvailable, m_nSize - getReadPos());
165 System.arraycopy(m_abData, getReadPos(), abData, nOffset, nToRead);
166 m_lReadPos += nToRead;
167 nOffset += nToRead;
168 nAvailable -= nToRead;
169 nRemainingBytes -= nToRead;
170 }
171 notifyAll();
172 }
173 if (TDebug.TraceCircularBuffer)
174 {
175 TDebug.out("After read:");
176 dumpInternalState();
177 TDebug.out("< completed. Read " + nLength + " bytes");
178 }
179 return nLength;
180 }
181 }
182
183
184 public int write(byte[] abData)
185 {
186 return write(abData, 0, abData.length);
187 }
188
189
190
191 public int write(byte[] abData, int nOffset, int nLength)
192 {
193 if (TDebug.TraceCircularBuffer)
194 {
195 TDebug.out(">TCircularBuffer.write(): called; nLength: " + nLength);
196 dumpInternalState();
197 }
198 synchronized (this)
199 {
200 if (TDebug.TraceCircularBuffer) { TDebug.out("entered synchronized block."); }
201 if (!m_bBlockingWrite)
202 {
203 nLength = Math.min(availableWrite(), nLength);
204 }
205 int nRemainingBytes = nLength;
206 while (nRemainingBytes > 0)
207 {
208 while (availableWrite() == 0)
209 {
210 try
211 {
212 wait();
213 }
214 catch (InterruptedException e)
215 {
216 if (TDebug.TraceAllExceptions)
217 {
218 TDebug.out(e);
219 }
220 }
221 }
222 int nAvailable = Math.min(availableWrite(), nRemainingBytes);
223 while (nAvailable > 0)
224 {
225 int nToWrite = Math.min(nAvailable, m_nSize - getWritePos());
226 //TDebug.out("src buf size= " + abData.length + ", offset = " + nOffset + ", dst buf size=" + m_abData.length + " write pos=" + getWritePos() + " len=" + nToWrite);
227 System.arraycopy(abData, nOffset, m_abData, getWritePos(), nToWrite);
228 m_lWritePos += nToWrite;
229 nOffset += nToWrite;
230 nAvailable -= nToWrite;
231 nRemainingBytes -= nToWrite;
232 }
233 notifyAll();
234 }
235 if (TDebug.TraceCircularBuffer)
236 {
237 TDebug.out("After write:");
238 dumpInternalState();
239 TDebug.out("< completed. Wrote "+nLength+" bytes");
240 }
241 return nLength;
242 }
243 }
244
245
246
247 private void dumpInternalState()
248 {
249 TDebug.out("m_lReadPos = " + m_lReadPos + " ^= "+getReadPos());
250 TDebug.out("m_lWritePos = " + m_lWritePos + " ^= "+getWritePos());
251 TDebug.out("availableRead() = " + availableRead());
252 TDebug.out("availableWrite() = " + availableWrite());
253 }
254
255
256
257 public static interface Trigger
258 {
259 public void execute();
260 }
261
262
263}
264
265
266
267/*** TCircularBuffer.java ***/
268