Results 1 to 3 of 3
Thread: Simple IO problem
- 12-01-2008, 12:02 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 4
- Rep Power
- 0
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);
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
- 12-01-2008, 03:04 PM #2
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
- 12-01-2008, 03:27 PM #3
Member
- Join Date
- Nov 2008
- Posts
- 4
- Rep Power
- 0
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
-
Simple JFrame Problem
By DC1 in forum New To JavaReplies: 4Last Post: 06-06-2008, 08:09 AM -
Simple newbie problem: import 3rd party jar
By jodyflorian in forum EclipseReplies: 5Last Post: 05-30-2008, 05:02 AM -
My Simple Refresh Problem
By pmcastillo in forum New To JavaReplies: 0Last Post: 03-26-2008, 08:59 AM -
New to JSF, simple problem with Eclispe and Tomcat
By nibeck in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 12-16-2007, 06:53 PM -
problem with a simple java code
By boy22 in forum New To JavaReplies: 2Last Post: 08-03-2007, 03:46 AM
Bookmarks