Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seeking a ByteArrayInputStream using java.io

Tags:

java

io

stream

nio

How can I seek (change the position) of a ByteArrayInputStream (java.io)? It is something so obvious, but I can't seem to find a method for this anywhere (mark/reset is not enough, I need to set the position to anywhere on the InputStream).

If it can't be done using java.io and I must switch to java.nio and use a ByteBuffer, how can I get something similar to a DataOutputStream wrapping a ByteArrayOutputStream using java.nio? I'm not finding any kind of auto-resizable buffer.

EDIT: I've found one way to achieve what I'm attempting to do, but it's a bit messy. ImageIO.createImageInputStream creates a ImageInputStream, which is exactly what I want (can seek and read primitives). However, using a ByteArrayInputStream returns a FileCacheImageInputStream, which basically means it copies the byte array to a file just to seek.

This is my first time trying to use the Java IO classes and it has been completely negative. It's missing some fundamental (IMO) features, and it has lots of ways to do the same thing (e.g. to read primitives from a file you can either use RandomAccessFile, DataInputStream + FileInputStream, FileImageInputStream, FileChannel + ByteBuffer, and maybe even more).

like image 604
asdfghjkl Avatar asked Sep 25 '10 06:09

asdfghjkl


People also ask

How can I get data from ByteArrayInputStream?

1. The read() method of ByteArrayInputStream class in Java is used to read the next byte of the ByteArrayInputStream. This read() method returns the byte that is read int the form of an integer and if the input stream is ended this method return -1. This method reads one byte at a time from the stream.

How do you create a ByteArrayInputStream?

Create a ByteArrayInputStream byte[] bytes = ... //get byte array from somewhere. ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); This example creates a ByteArrayInputStream which can read all bytes in the byte array passed to its constructor.

What is ByteArrayInputStream in Java?

All Implemented Interfaces: Closeable, AutoCloseable. public class ByteArrayInputStream extends InputStream. A ByteArrayInputStream contains an internal buffer that contains bytes that may be read from the stream.

Do we need to close ByteArrayInputStream in Java?

You don't have to close ByteArrayInputStream , the moment it is not referenced by any variable, garbage collector will release the stream and somebytes (of course assuming they aren't referenced somewhere else).


2 Answers

If you are creating the ByteArrayInputStream to pass elsewhere, extend the class and manipulate pos (a protected member of ByteArrayInputStream) as you wish.

like image 120
erickson Avatar answered Sep 19 '22 09:09

erickson


You'd use reset()/skip(). I can't say it's the nicest API in the world, but it should work:

public void seek(ByteArrayInputStream input, int position)
    throws IOException
{
    input.reset();
    input.skip(position);
}

Of course, that assumes that no-one has called mark().

like image 33
Jon Skeet Avatar answered Sep 19 '22 09:09

Jon Skeet