Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
DaBeef - 21 Jul 2006 14:48 GMT
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
Matt Humphrey - 21 Jul 2006 18:20 GMT
> 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 matth@ivizNOSPAM.com http://www.iviz.com/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.