我在运行以下代码行时遇到问题。每当我给对象大小超过95KB,我得到无效或损坏的标头异常,代码运行正常,对象大小小于95KB。请帮忙,我无法解决问题。
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;
public class Main {
public String toStringMethod(TestObject test) {
String serializedObject = "";
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream so = new ObjectOutputStream(bo);
so.writeObject(test);
so.flush();
serializedObject = bo.toString();
} catch (Exception e) {
System.out.println(e);
}
return serializedObject;
}
public TestObject toObjectMethod(String string) {
String serializedObject = string;
TestObject obj = null;
try {
byte b[] = serializedObject.getBytes();
ByteArrayInputStream bi = new ByteArrayInputStream(b);
ObjectInputStream si = new ObjectInputStream(bi);
obj = (TestObject) si.readObject();
} catch (Exception e) {
System.out.println(e);
}
return obj;
}
public static void main(String[] args) {
Main mn = new Main();
TestObject object = new TestObject();
object.setString(10);
String str = mn.toStringMethod(object);
TestObject newObject = mn.toObjectMethod(str);
newObject.printHello();
}
}
class TestObject implements Serializable{
char chars[];
String str;
public String setString(int numberOfBytes) {
numberOfBytes = numberOfBytes/2;
chars = new char[numberOfBytes];
Arrays.fill(chars, 'a');
str = new String(chars);
return str;
}
public String getString() {
return str;
}
public void printHello() {
System.out.println("hello tester");
}
}