Solved

How do I create a Java byte array containing the last x bytes of an inputstream?

Posted on 2010-09-23
6
401 Views
Last Modified: 2013-11-23
In Java, I have an inputstream and want a byte array containing the last x bytes. What is the best way to do this?
0
Comment
Question by:rubixcube5
6 Comments
 
LVL 1

Expert Comment

by:mrdodger
ID: 33745684
Byte [] ba = new Byte[]
0
 

Author Comment

by:rubixcube5
ID: 33745812
hahahahaha! you've declared a byte array but that's it! it's empty! where is the inputstream?
0
 
LVL 10

Expert Comment

by:Hegemon
ID: 33745896
byte[] array = new byte[x];
inputStream.read (array, offset, x);

0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:rubixcube5
ID: 33746275
Hegemon, I could use your solution where offset = length of bytes in the inputStream - x.
But then I have to read in the inputstream twice, once to get the length and then again to get the last x bytes.
What is a more efficient solution?
0
 
LVL 10

Accepted Solution

by:
Hegemon earned 125 total points
ID: 33746371
Have another array (anotherArray) of the same size and copy the content of the first array into it before each read() call.
The border case: if your read() returns -1 or 0, anotherArray contains the last x bytes.
Otherwise, if read() returns 0 < n < x, the last n bytes of x are stored in the first array from 0 onwards, up to n-1. The first x-n elements are at the end of anotherArray.
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33746449
you can use StringWriter to convert InputStream to String first, then you can get the last x number of bytes from String into byte Array

http://stackoverflow.com/questions/309424/in-java-how-do-a-read-convert-an-inputstream-in-to-a-string
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#getBytes%28%29

but i think even if here InputStream  is read once, the solution proposed by @Hegemon #33745896 is the best possible solution
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 85
HSSFWorkbook cannot be resolved error 10 66
Should localization be done inside spring controller 5 25
Tomcat: Unable to run tomcat service. 2 18
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now