2

I'm new to Java and I want to implement a buffer of byte[] which one thread can write into, and another thread can read from.
It sounds like it should have already been implemented in java, but I spent hours trying to find/understand several number of classes, and I didn't understand if it does what I want, and how to use it.
I saw BufferedInputStream, ByteBuffer, ByteChannel, BlockingQueue...

Can someone please point to a more specific direction? I use SDK 1.6

6
  • Are reads/writes always sequential? Is the byte array a fixed size? And no, I don't believe Java has such a class builtin, you'll have to write it yourself and synchronize it appropriately (note: by "sequential" here, I mean "not random", ie you don't read from the middle of the array/write into the middle of the array) Commented Mar 11, 2014 at 13:59
  • The byte array isn't a fixed size. reader and writer doesn't have to be sequential... Commented Mar 11, 2014 at 14:00
  • Then a custom class it is for you... Commented Mar 11, 2014 at 14:01
  • @fge, Sorry, I just saw your edit. It is sequential. (a reader reads the first block of bytes available in queue, a writer writes to the end of the queue) Commented Mar 11, 2014 at 14:06
  • Even then you'll need a custom class. It'd be more simple if reads/writes were of fixed size but it would be too simple, wouldn't it? ;) Commented Mar 11, 2014 at 14:37

2 Answers 2

5

If you're looking to simply stream bytes from one thread to the other, I suggest you use PipedInputStream and PipedOutputStream. Though note that it's likely you're in this position where you need a solution such as this because of a design fault.

Here is how you would do such thing, for instance:

PipedOutputStream out = new PipedOutputStream();
PipedInputStream in = new PipedInputStream(out);
new YourReadingThread(in).start();
new YourWritingThread(out).start();

Then anything you write to out will be available for reading through in.


If you're looking for a solution to make a thread safe ByteBuffer, I would suggest you use a ReentrantReadWriteLock:

ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
ByteBuffer buffer = ByteBuffer.allocate(n);

// Reading thread:
lock.readLock().lock();
buffer.get(i);
lock.readLock().unlock();

// Writing thread:
lock.writeLock().lock();
buffer.put(b,i);
lock.writeLock().unlock();
Sign up to request clarification or add additional context in comments.

Comments

3

Sorry, ByteBuffer wasn't good for me cause it involves playing with the position of the buffer.
I realized all I really needed was a simple pipe (I don't know how could I forget about pipes).

I used an example from here:

PipedOutputStream output = new PipedOutputStream();
PipedInputStream input = new PipedInputStream(output);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.