在对字节数组进行解压缩和反序列化时,我遇到了EOFException 堆栈跟踪:
java.io.EOFException的 at java.io.ObjectInputStream $ PeekInputStream.readFully(ObjectInputStream.java:2324) at java.io.ObjectInputStream $ BlockDataInputStream.readShort(ObjectInputStream.java:2793) at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:799) 在java.io.ObjectInputStream。(ObjectInputStream.java:299) 在com.chuck.pack.ResourcePacket.load(ResourcePacket.java:44)
// Functions related to load
public static ResourcePacket load(String packetName) {
try {
byte[] bytes = uncompress(fileToByteArray(new File(packetName)));
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
ObjectInputStream objIn = new ObjectInputStream(in); // Error occured here
return (ResourcePacket) objIn.readObject();
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
private static byte[] uncompress(byte[] bytes) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Inflater ifl = new Inflater();
ifl.setInput(bytes);
byte[] buffer = new byte[4*1024];
while(ifl.finished()) {
int size = ifl.inflate(buffer);
out.write(buffer, 0, size);
}
out.close();
return out.toByteArray();
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("Error while load");
}
return null;
}
private static byte[] fileToByteArray(File file) {
byte[] bytes = new byte[(int) file.length()];
try {
FileInputStream fileInputStream = new FileInputStream(file);
fileInputStream.read(bytes);
fileInputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return bytes;
}
// Functions related to save
public void save() {
try {
byte[] bytes = compress(toByteArray());
//byte[] bytes = toByteArray();
FileOutputStream fileOutputStream = new FileOutputStream(packetName);
fileOutputStream.write(bytes);
fileOutputStream.close();
System.out.println("Packet saved (" + resourceFiles.length + " Files)");
} catch (IOException ex) {
ex.printStackTrace();
System.exit(-1);
}
}
private byte[] compress(byte[] bytes) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Deflater dfl = new Deflater();
dfl.setLevel(Deflater.BEST_SPEED);
dfl.setInput(bytes);
dfl.finish();
byte[] tmp = new byte[4*1024];
while(!dfl.finished()) {
int size = dfl.deflate(tmp);
out.write(tmp, 0, size);
}
out.close();
return out.toByteArray();
} catch (IOException ex) {
ex.printStackTrace();
System.out.println("Error while save");
}
System.exit(-1);
return null;
}
private byte[] toByteArray() {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(this);
byte[] bytes = bos.toByteArray();
return bytes;
} catch(IOException ex) {
ex.printStackTrace();
System.out.println("Error while create byte array");
}
System.exit(-1);
return null;
}
有人想要解决这个问题吗?
答案 0 :(得分:0)
在uncompress(..)
方法中,while循环:
while(ifl.finished()) {
int size = ifl.inflate(buffer);
out.write(buffer, 0, size);
}
应该是
while(!ifl.finished()) {
int size = ifl.inflate(buffer);
out.write(buffer, 0, size);
}
否则,如果方法终止,uncompress(..)
的结果为空字节数组。