Results 1 to 3 of 3
  1. #1
    aamp is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default Simple IO problem

    Hello,

    I'm trying to read utf string from bytearray like this:
    Java Code:
    byteInput = new ByteArrayInputStream(data);
    dataInput = new DataInputStream(byteInput);
    string that lies in dataEvent data is 2048 symbols length.

    dataInput.available() gives me 2050 and it means first 2 bytes on byte array are the length of utf string. I read it with dataInput().readShort() and I get 2048 wich is correct. But when I try to read the string with readUTF() (not reading the length, i mean from beggining of stream) i get EOFException. If the string that lies in my bytearray is like 1024 or less in length I dont get EOFException, my string is read ok:

    Java Code:
    byteInput = new ByteArrayInputStream(data); // there is 2048-length UTF 
    string in data (bytearray)
    dataInput = new DataInputStream(byteInput);
    String s = dataInput.readUTF(); // throws EOFException
    //-------------------------------------------------------------
    byteInput = new ByteArrayInputStream(data); // there is 1024-length UTF string in data (bytearray)
    dataInput = new DataInputStream(byteInput);
    String s = dataInput.readUTF(); // works
    Why a length of 2048 isnt working?

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    First two bytes are normally "byte order mark" ( BOM ) which sounds like what the first two bytes are rather than length of the string.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    aamp is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default

    A quote from Java reference:
    First, two bytes are written to the output stream as if by the writeShort method giving the number of bytes to follow.

    But that doesn't change anything. Why does it throw EOFException when I try to read somewhat long string? I mean, dataInput.available() gives me an exact length of what I need to read, though it just threws up.

    P.S. I get this string throught java.nio TCP socket channel, maybe there any length limitations or the data is transferred in portions?

Similar Threads

  1. Simple JFrame Problem
    By DC1 in forum New To Java
    Replies: 4
    Last Post: 06-06-2008, 08:09 AM
  2. Simple newbie problem: import 3rd party jar
    By jodyflorian in forum Eclipse
    Replies: 5
    Last Post: 05-30-2008, 05:02 AM
  3. My Simple Refresh Problem
    By pmcastillo in forum New To Java
    Replies: 0
    Last Post: 03-26-2008, 08:59 AM
  4. New to JSF, simple problem with Eclispe and Tomcat
    By nibeck in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-16-2007, 06:53 PM
  5. problem with a simple java code
    By boy22 in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 03:46 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
  •