Solved

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

Posted on 2010-09-23
6
408 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 75
web application structure 18 128
ejb on wildfly 5 61
Tomcat 9 + java 8 error while trying to deploy a war file 2 841
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

739 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