How to extract x amount of bytes from a byte object and store into another byte obj.

D

DaBeef

Hello

I am reading bytes from a stream but some are cmoing back corrupt.
Someonly return ex. 456.
byte[] byteobj= new byte[1024] ;
n = _fromGeneva.read(byteobj, 0 , 1024);

if(n >= 1024)
_errorMsg = new String(blah) ;
else
{
/* I want to put up to n in a new byte. How would I transfer up to n
efficienlty from byteobj into another byte so the data is not
corrupted. After say 456 i get null bytes */


}

Thanks so MUCH!!!! kinda new to dealing with bytes in java. Did it
years ago but alast i dont recall.

Thanks again
 
M

Matt Humphrey

DaBeef said:
Hello

I am reading bytes from a stream but some are cmoing back corrupt.
Someonly return ex. 456.

I'm not sure what you mean by corrupt, but I think you mean that some times
your read returns fewer than 1024 bytes. Whether that's corrupt or not
depends on your application. if _fromGeneva is a socket it may simply mean
that you havn't received all of the data yet and that you need to read more.
byte[] byteobj= new byte[1024] ;
n = _fromGeneva.read(byteobj, 0 , 1024);

if(n >= 1024)
_errorMsg = new String(blah) ;

The new String () is unnecessary here. Just say _errorMsg = blah;
else
{
/* I want to put up to n in a new byte. How would I transfer up to n
efficienlty from byteobj into another byte so the data is not
corrupted. After say 456 i get null bytes */

I think you mean you want to copy the bytes read into another array. You
can do that with:

byte [] result = new byte [n];
System.arraycopy (byteobj, 0, result, 0, n);

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top