-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathBufferReader.java
More file actions
228 lines (178 loc) · 5.85 KB
/
BufferReader.java
File metadata and controls
228 lines (178 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
** AACDecoder - Freeware Advanced Audio (AAC) Decoder for Android
** Copyright (C) 2011 Spolecne s.r.o., http://www.spoledge.com
**
** This file is a part of AACDecoder.
**
** AACDecoder is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published
** by the Free Software Foundation; either version 3 of the License,
** or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.spoledge.aacdecoder;
import android.util.Log;
import java.io.InputStream;
import java.io.IOException;
/**
* This is a separate thread for reading data from a stream.
* The buffer creates 3 buffer instances - one is being filled by the execution thread,
* one is prepared with data, one can be processed by a consumer.
* <pre>
* InputStream is = ...;
*
* // create a new reader with capacity of 4096 bytes per buffer:
* BufferReader reader = new BufferReader( 4096, is );
*
* // start the execution thread which reads the stream and fills the buffers:
* new Thread(reader).start();
*
* // get the data
* while (...) {
* BufferReader.Buffer buf = reader.next();
*
* if (!buf.getSize() == 0) break;
*
* // process data
* ...
* }
* </pre>
*/
public class BufferReader implements Runnable {
public static class Buffer {
private byte[] data;
private int size;
Buffer( int capacity ) {
data = new byte[ capacity ];
}
public final byte[] getData() {
return data;
}
public final int getSize() {
return size;
}
}
private static String LOG = "BufferReader";
int capacity;
private Buffer[] buffers;
/**
* The buffer to be write into.
*/
private int indexMine;
/**
* The index of the buffer last returned in the next() method.
*/
private int indexBlocked;
private boolean stopped;
private InputStream is;
////////////////////////////////////////////////////////////////////////////
// Constructors
////////////////////////////////////////////////////////////////////////////
/**
* Creates a new buffer.
*
* @param capacity the capacity of one buffer in bytes
* = total allocated memory
*
* @param is the input stream
*/
public BufferReader( int capacity, InputStream is ) {
this.capacity = capacity;
this.is = is;
Log.d( LOG, "init(): capacity=" + capacity );
buffers = new Buffer[3];
for (int i=0; i < buffers.length; i++) {
buffers[i] = new Buffer( capacity );
}
indexMine = 0;
indexBlocked = buffers.length-1;
}
////////////////////////////////////////////////////////////////////////////
// Public
////////////////////////////////////////////////////////////////////////////
/**
* Changes the capacity of the buffer.
*/
public synchronized void setCapacity( int capacity ) {
Log.d( LOG, "setCapacity(): " + capacity );
this.capacity = capacity;
}
/**
* The main loop.
*/
public void run() {
Log.d( LOG, "run() started...." );
int cap = capacity;
int total = 0;
while (!stopped) {
Buffer buffer = buffers[ indexMine ];
total = 0;
if (cap != buffer.data.length) {
Log.d( LOG, "run() capacity changed: " + buffer.data.length + " -> " + cap);
buffers[ indexMine ] = buffer = null;
buffers[ indexMine ] = buffer = new Buffer( cap );
}
while (!stopped && total < cap) {
try {
int n = is.read( buffer.data, total, cap - total );
if (n == -1) stopped = true;
else total += n;
}
catch (IOException e) {
Log.e( LOG, "Exception when reading: " + e );
stopped = true;
}
}
buffer.size = total;
synchronized (this) {
notify();
int indexNew = (indexMine + 1) % buffers.length;
while (!stopped && indexNew == indexBlocked) {
//Log.d( LOG, "run() waiting...." );
try { wait(); } catch (InterruptedException e) {}
//Log.d( LOG, "run() awaken" );
}
indexMine = indexNew;
cap = capacity;
}
}
Log.d( LOG, "run() stopped." );
}
/**
* Stops the thread - the object cannot be longer used.
*/
public synchronized void stop() {
stopped = true;
notify();
}
/**
* Returns true if this thread was stopped.
*/
public boolean isStopped() {
return stopped;
}
/**
* Returns next available buffer instance.
* The returned instance can be freely used by another thread.
* Blocks the caller until a buffer is ready.
*/
public synchronized Buffer next() {
int indexNew = (indexBlocked + 1) % buffers.length;
while (!stopped && indexNew == indexMine) {
Log.d( LOG, "next() waiting...." );
try { wait(); } catch (InterruptedException e) {}
Log.d( LOG, "next() awaken" );
}
if (indexNew == indexMine) return null;
indexBlocked = indexNew;
notify();
return buffers[ indexBlocked ];
}
}