EOFExcpetion при десериализации байтовым массивом
У меня есть EOFException при распаковке и десериализации байтового массива.
Трассировки стека:
java.io.EOFException в java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2324) в java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2793) в java.putreamInHeader.ject. 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;
}
Кто-то идея, чтобы это исправить?
1 ответ
Решение
В 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(..)
является пустым байтовым массивом, если метод завершается.