Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to extract x amount of bytes from a byte object and store into another byte obj.

Reply
Thread Tools

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

 
 
DaBeef
Guest
Posts: n/a
 
      07-21-2006
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

 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      07-21-2006

"DaBeef" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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 http://www.velocityreviews.com/forums/(E-Mail Removed) http://www.iviz.com/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to store a large amount of 3D data points in Java? James Java 29 04-15-2006 04:19 AM
Store large amount of data Kiki Javascript 2 09-26-2005 08:00 AM
Insert Byte Element into array of bytes =?Utf-8?B?UmFlZCBTYXdhbGhh?= ASP .Net 0 03-28-2005 01:45 PM
Re: receiving Bytes and sending Bytes The Old Sourdough Computer Support 0 07-23-2003 01:23 PM



Advertisments