Something like a virtual Stream

Can I have something like a virtual stream, were i can read and write in a FIFO law?Thanks--david
[118 byte] By [David04a] at [2007-10-1 0:39:39]
# 1
Huh?Another poorly written, fuzzy-thinking question, David. Can't you do better than this?I know what a stream is. I know what FIFO means. What is it you want to do? %
duffymoa at 2007-7-7 16:26:33 > top of Java-index,Security,Event Handling...
# 2
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
BIJ001a at 2007-7-7 16:26:33 > top of Java-index,Security,Event Handling...
# 3

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)

BIJ001a at 2007-7-7 16:26:33 > top of Java-index,Security,Event Handling...
# 4

> 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;

}

}

David04a at 2007-7-7 16:26:33 > top of Java-index,Security,Event Handling...
# 5
Let us reinvent the java.util.Stack wheel.
BIJ001a at 2007-7-7 16:26:33 > top of Java-index,Security,Event Handling...
# 6
> Let us reinvent the java.util.Stack wheel. I believe a Stack is LIFO, not FIFO.I think a Queue is being added in Java 5, which is what the OP wants.
camickra at 2007-7-7 16:26:33 > top of Java-index,Security,Event Handling...
# 7

> 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.

DrClapa at 2007-7-7 16:26:33 > top of Java-index,Security,Event Handling...
# 8
> Let us reinvent the java.util.Stack wheel.If you had told-me that earlier it would have been nice ;)
David04a at 2007-7-7 16:26:33 > top of Java-index,Security,Event Handling...
# 9
I was enlighted during the discussion ;-)
BIJ001a at 2007-7-7 16:26:33 > top of Java-index,Security,Event Handling...
# 10
First I assumed the question was in the sense of input-output-streams. Using a Stack for every input byte wrapped in a Byte would have been a possibility but very ineffective and therefore not to recommend. But it is OK on the object level.
BIJ001a at 2007-7-7 16:26:33 > top of Java-index,Security,Event Handling...