JDK-4235914 : EOFException in readUnsignedByte when stream is of a certain size
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_98
  • CPU: x86
  • Submitted: 1999-05-06
  • Updated: 1999-06-07
  • Resolved: 1999-06-07
Related Reports
Relates :  
Description

Name: tb29552			Date: 05/06/99


/*
Hi there,
We have a problem with ByteArrayInputStream or DataInputStream.
We don't know where the problem exactly happens, probably in
ByteArrayInputStream. In just one case we get an exception if the
stream is of a certain size, if we add/remove a blank or character to the
stream, the problem doesn't exist (no exception). We have also tried
JDK 1.1.8 but the problem persists.
Here is the test case:

program which simulates the following situation:

In the sending part, a Vector is written into a stream and then into a
String Then a int (count), the converted Vector and the CRC of the
converted Vector are again written into a stream, which is then
converted into a String.

In the receiving part, the String is attempted to read into a stream,
and normally put in a String Array.

What happens is this:
% javac testsendstream.java
% java -Djava.compiler=NONE testsendstream
to second stream: 000104FGEEEDftFFFekt00070001
                                              chosen type0002
                                                             chosen brand0003
                                                                             config name0004group00060012
to send: 2000???0001??04??FGEEEDftFFFekt??0007??0001??
                                                      chosen type??0002??
                                                                         chosen brand??0003??
             config name??0004??group??0006??00120000000000908106492
??0001??04??FGEEEDftFFFekt??0007??0001??
                                        chosen type??000
IOException: receiveString
   incrcstring contains: null
java.io.EOFException
        at java.io.DataInputStream.readUnsignedByte(DataInputStream.java:247)
        at java.io.DataInputStream.readUTF(DataInputStream.java:526)
        at java.io.DataInputStream.readUTF(DataInputStream.java:498)
        at testsendstream.main(testsendstream.java:156)
CRC not correct



*/

import java.util.*;
import java.util.zip.*;
import java.io.*;
import java.lang.reflect.*;

class testsendstream {

    public static void main(String args[]) {

        /** sending part*/

        ByteArrayOutputStream byteoutstream = new ByteArrayOutputStream();
        DataOutputStream dataoutstream = new DataOutputStream(byteoutstream);
        byteoutstream.reset();

        boolean fpfound = false;
        int count = 0;

        Vector vect = new Vector();

        vect.addElement(new Integer(4));
        vect.addElement(new String("FGEEEDftFFFekt"));
        vect.addElement(new Integer(7));
        vect.addElement(new Integer(1));
        vect.addElement(new String("chosen type"));     // <- add/remove a blank
                                                        // or character and it
                                                        // works
        vect.addElement(new Integer(2));
        vect.addElement(new String("chosen brand"));
        vect.addElement(new Integer(3));
        vect.addElement(new String("config name"));
        vect.addElement(new Integer(4));
        vect.addElement(new String("group"));
        vect.addElement(new Integer(6));
        vect.addElement(new Integer(12));

        int cmd = 0x2000;
        int cmdtest = ((Integer) vect.elementAt(0)).intValue();

        int[] posdigact = {1, 2, 3, 4, 6};

        count++;
        try {
            dataoutstream.writeUTF(convertInt(4, count));       // count
            dataoutstream.writeUTF(convertInt(2, cmdtest));     // Number
            dataoutstream.writeUTF((String) vect.elementAt(1)); // ID
            dataoutstream.writeUTF(convertInt(4, ((Integer) vect.elementAt(2)).intValue()));    // Number
            dataoutstream.writeUTF(convertInt(4, ((Integer) vect.elementAt(3)).intValue()));    // Field pos
            dataoutstream.writeUTF((String) vect.elementAt(4)); // Value
            dataoutstream.writeUTF(convertInt(4, ((Integer) vect.elementAt(5)).intValue()));    // Field pos
            dataoutstream.writeUTF((String) vect.elementAt(6)); // Value
            dataoutstream.writeUTF(convertInt(4, ((Integer) vect.elementAt(7)).intValue()));    // Field pos
            dataoutstream.writeUTF((String) vect.elementAt(8)); // Value
            dataoutstream.writeUTF(convertInt(4, ((Integer) vect.elementAt(9)).intValue()));    // Field pos
            dataoutstream.writeUTF((String) vect.elementAt(10));        // Value
            dataoutstream.writeUTF(convertInt(4, ((Integer) vect.elementAt(11)).intValue()));   // Field pos
            dataoutstream.writeUTF(convertInt(4, ((Integer) vect.elementAt(12)).intValue()));   // Value
        }
        catch(IOException e) {
            System.out.println("IOException");
            e.printStackTrace();
        }

        String framestring = byteoutstream.toString();

        System.out.println("to second stream: " + framestring);

        String command = Integer.toHexString(cmd);

        ByteArrayOutputStream byteoutstream2 = new ByteArrayOutputStream();
        DataOutputStream dataoutstream2 = new DataOutputStream(byteoutstream2);
        byteoutstream2.reset();
        byte[] crcbyte = framestring.getBytes();
        CRC32 crc = new CRC32();
        crc.reset();
        crc.update(crcbyte);
        try {
            dataoutstream2.writeUTF(command);
            dataoutstream2.writeUTF(framestring);
            String crcstringvalue = convertLong(crc.getValue());
            dataoutstream2.writeUTF(crcstringvalue);
            dataoutstream2.flush();
            dataoutstream2.close();
        }
        catch(IOException e) {
            System.out.println("IOException sendString");
            e.printStackTrace();
        }
        String sendstring = byteoutstream2.toString();

        System.out.println("to send: " + sendstring);

        String received = sendstring;

        /**receiving part */


        String[] s = new String[2];
        String incrcstring=null;
        long incrc = 0;

        byte[] inbyte = received.getBytes();
        ByteArrayInputStream byteinstream = new ByteArrayInputStream(inbyte);
        DataInputStream datainstream = new DataInputStream(byteinstream);
        byteinstream.reset();

        try {
            s[0] = datainstream.readUTF();      // command
            s[1] = datainstream.readUTF();      // instring
            System.out.println(s[1]);
            incrcstring = datainstream.readUTF();
            incrc = Long.parseLong(incrcstring);
        }
        catch(IOException e) {
            System.out.println("IOException: receiveString");
            System.out.println("   incrcstring contains: " + incrcstring);
            
            e.printStackTrace();
        }


        byte[] crcbyte2 = s[1].getBytes();
        CRC32 crc2 = new CRC32();
        crc2.reset();
        crc2.update(crcbyte2);

        if (crc2.getValue() == incrc)
            System.out.println("Frame correctly received");
        else
            System.out.println("CRC not correct");

    }

    synchronized static String convertInt(int max, int toconvert) {

        String converted = new String();
        String temp;

        temp = String.valueOf(toconvert);
        int le = temp.length();

        for (int i = 0; i < (max - le); i++)
            converted += "0";
        converted += temp;

        return converted;
    }


    synchronized static String convertLong(long longnumber) {
        String converted = new String();
        String temp;

        temp = String.valueOf(longnumber);
        int le = temp.length();

        for (int i = 0; i < (19 - le); i++)
            converted += "0";
        converted += temp;

        return converted;
    }
}

/*

The blank/character can be added/removed in any of the above strings
and the problem doesn't occur. We are about to program a
communication protocol for Internet communication any need a solution
to this problem urgently.
Any ideas?
For any questions, don't hesitate to contact us at
###@###.### (Peter) or
###@###.### (Georg)

Best regards
Peter

*/
(Review ID: 57890) 
======================================================================

Comments
WORK AROUND Name: tb29552 Date: 05/06/99 none, just add/remove a character to one of the strings as described above. ======================================================================
11-06-2004

EVALUATION Not a bug. The ByteArrayOutputStream.toString method uses the platform's default encoding to translate bytes into characters. On Win32, the default encoding (as of 1.2 and 1.1.8) is "cp1252", the standard Microsoft code page for ISO-Latin-1. This encoding does not preserve all byte values. The given code works with earlier releases becuase the default was a different encoding that did preserve most byte values. -- mr@eng 1999/6/7
06-07-0173