import java.io.*;
public class a extends ByteArrayOutputStream {
public byte pop() throws IOException {
if (count==0) throw new IOException( "buffer underflow") ;
count--;
return buf [ count ];
}
public static void main (String a[]) throws IOException {
int i;
a w = new a();
for(i='A';i<='Z';i++) w.write((byte)i);
while (true) {
System.out.println((char)w.pop());
}
}
}
Z
Y
X
W
V
U
T
S
R
Q
P
O
N
M
L
K
J
I
H
G
F
E
D
C
B
A
Exception in thread "main" java.io.IOException: buffer underflow
at a.pop(a.java:4)
at a.main(a.java:14)
> Maybe the wish is for someting like
> java.io.ByteArrayOutputStream (that is, an object
> which can accept byte written to it) with the
> addtiional ability to "pop" some part of the data
> back?
>
> What about extending java.io.ByteArrayOutputStream
> and providing that addtiional functionality ?
Thanks, that's more or less that (but not byte by byte!)
I've made my own:
class FIFO{
private Object[] list = new Object[0];
public void add(Object obj){
Object[] newList = new Object[list.length+1];
for(int count=0; count<=list.length-1; count++){
newList[count] = list[count];
}
newList[newList.length-1] = obj;
list = newList;
}
public Object get(){
while(list.length==0){
try{Thread.sleep(100);}catch(InterruptedException e){}
}
Object obj = list[0];
Object[] newList = new Object[list.length-1];
for(int count=1; count<=list.length-1; count++){
newList[count-1] = list[count];
}
list = newList;
return obj;
}
public Object fastGet(String nothing){
if(list.length==0){
return nothing;
}
Object obj = list[0];
Object[] newList = new Object[list.length-1];
for(int count=list.length-1; count>=1; count--){
newList[count-1] = list[count];
}
list = newList;
return obj;
}
}
> I think a Queue is being added in Java 5, which is
> what the OP wants.
And before that version, a queue is typically implemented as a LinkedList, where readers remove entries from one end and writers add entries to the other end. But I wouldn't implement a stream of bytes as a LinkedList, that's a bit of overkill. I might look at PipedInputStream and PipedOutputStream.