Java. Io. streamcorruptedexception: Error cause of invalid stream Header

Source: Internet
Author: User

When objectinputstream is used to input a stream, an error occurs because a byte [] array is directly used as a parameter.

The Code is as follows:

Package org. test;

Import java. Io. bytearrayinputstream;
Import java. Io. bytearrayoutputstream;
Import java. Io. objectinputstream;
Import java. Io. objectoutputstream;
Import java. util. arraylist;
Import java. util. date;

Public class javatest {

Public static void main (string ARGs []) {
Javatest Jt = new javatest ();

String S = "helloworld! ";
Byte [] bt = NULL;
Bt = S. getbytes ();
Arraylist <string> List = new arraylist <string> ();
List = JT. getarraylist (BT); // This row will have an error.

}

 
Public arraylist <string> getarraylist (byte [] BT ){
Arraylist <string> List = new arraylist <string> ();
Objectinputstream objips;

// Note that objectinputstream deserializes the basic data and objects previously written using objectoutputstream. The problem is that you directly pass the byte [] array, and this byte array is not written using the objectoutputstream class. The solution is to use the output stream to obtain the byte [] array.
Try {
Objips = new objectinputstream (New bytearrayinputstream (BT ));
List = (arraylist <string>) objips. readobject ();
} Catch (exception e ){
E. printstacktrace ();
}
Return list;
}

Public byte [] getbyte (arraylist <string> List ){
Byte [] bt = NULL;
Bytearrayoutputstream baos = new bytearrayoutputstream ();

Try {
If (list! = NULL)
{
Objectoutputstream objos = new objectoutputstream (baos );
Objos. writeobject (list );
Bt = baos. tobytearray ();
}
} Catch (exception E)
{
Bt = (byte []) NULL;
E. printstacktrace ();

}
Return Bt;
}
 

}

Modify:

Get byte [] byte array through objectoutputstream. As follows:

Public static void main (string ARGs []) {
Javatest Jt = new javatest ();

String S = "helloworld! ";
Byte [] bt = NULL;

Arraylist <string> List = new arraylist <string> ();

Arraylist <string> byte_list = new arraylist <string> ();

Byte_list.add (s );

Bt = JT. getbyte (byte_list); // call the getbyte () method to obtain the BT [] array.
List = JT. getarraylist (BT );

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.