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

Discussion in 'Java' started by DaBeef, Jul 21, 2006.

  1. DaBeef

    DaBeef Guest

    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
     
    DaBeef, Jul 21, 2006
    #1
    1. Advertisements

  2. 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.
    The new String () is unnecessary here. Just say _errorMsg = blah;
    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.iviz.com/
     
    Matt Humphrey, Jul 21, 2006
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.