Results 1 to 2 of 2
  1. #1
    sakshi.13agg is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default byte to string conversion creates problem in streams

    I am reading bytes from InputStream and converting these bytes read to the string .After that I am writing bytes to the Output Stream.
    Following is the code-

    Java Code:
    byte[] b = new byte [16384];
    int len = inputStream.read(b);
    String s = new String( b );
    outputStream.write(s.getBytes(), 0 , len);
    However I am not getting the expected output.
    May I know if I am doing any mistake in these string to byte conversion?

  2. #2
    Potato is offline Member
    Join Date
    Dec 2011
    Posts
    25
    Rep Power
    0

    Default Re: byte to string conversion creates problem in streams

    When you say
    Java Code:
    String s = new String(b);
    you are creating a string of length 16384, regardless of how many bytes you read. I'm guessing you want to have a String that's only as long as the number of bytes, 'len', you've read, in which case you only want to use 'len' bytes from the byte array 'b.' Like so:

    Java Code:
    String s = new String(b, 0, len);

Similar Threads

  1. String to byte conversion
    By YellowSun in forum New To Java
    Replies: 1
    Last Post: 02-02-2012, 08:19 PM
  2. Replies: 2
    Last Post: 11-21-2011, 12:08 AM
  3. Byte vs. Character Streams
    By Lil_Aziz1 in forum New To Java
    Replies: 5
    Last Post: 08-06-2010, 03:16 PM
  4. image to byte code file conversion
    By nupurashi in forum New To Java
    Replies: 0
    Last Post: 01-28-2009, 10:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •