首先,我使用FileOutputStream
,CipherOutputStream
& ObjectOutputStream
。
代码流就像这样Object > ObjectOutputStream > CipherOutputStream > FileOutputStream > File
。
现在,当我尝试从文件中读取该对象时,将抛出EOFException。
阅读对象的流程就像Object < ObjectInputStream < CipherInputStream < FileInputStream < File
。
我有一个类IOManager,它有方法IntIODEtail getIODetail(Path,boolean isComp)
&amp; boolean writeIntIODetail(IntIODetail obj,Path dir,boolean comp)
分别创建FileInputStream
或FileOutputStream
&amp;呼叫不同&amp;工厂类ObjectHandler加密和方法的方法将对象写入流或读取&amp;解密对象到流。
public static IntIODetail getIODetail(Path filepath,boolean isComp)throws IOException{
if(!isComp)
return getIODetail(filepath);
if(!filepath.isAbsolute())
return null;
if((!Files.exists(filepath))||Files.isDirectory(filepath))
return null;
try(FileInputStream f=new FileInputStream(filepath.toFile());
BufferedInputStream bin=new BufferedInputStream(f)){
IntIODetail obj=ObjectHandler.readCompObj(f);
if(obj!=null)
return obj;
throw new IOException();
}
}
public static boolean writeIntIODetail(IntIODetail obj,Path dir,boolean comp){
if(!comp)
return writeIntIODetail(obj,dir);
if(!dir.isAbsolute())
return false;
if(!Files.isDirectory(dir))
return false;
String name="c";
{//generate the formated file name.
name+="v";
if(obj instanceof IODetail)
name+=IODetail.getVersion();
else
name+="NA";
name=name+"p";
if(obj.programID()>=0)
name+=obj.programID();
else
name+="NA";
name+="i";
if(obj.index()>=0)
name+=obj.index();
else
name+="NA";
name+=".data";
}
Path f=dir.resolve(name);
try(FileOutputStream fout=new FileOutputStream(f.toFile());
BufferedOutputStream bout =new BufferedOutputStream(fout)){
return ObjectHandler.writeCompObj(fout, obj);
} catch(IOException ex) {
return false;
}
}
ObjectHandler的代码......
public static boolean writeObj(OutputStream out,IntIODetail o) throws IOException{
synchronized(out){
try(ObjectOutputStream objOut=new ObjectOutputStream(out)){
objOut.writeObject(o);
}catch(InvalidClassException | NotSerializableException e){
return false;
}
return true;
}
}
public static IntIODetail readObj(InputStream in) throws IOException{
synchronized(in){
try(ObjectInputStream objIn=new ObjectInputStream(in)){
IntIODetail ob=(IntIODetail)objIn.readObject();
return ob;
} catch(ClassNotFoundException | InvalidClassException
| StreamCorruptedException | OptionalDataException e) {
return null;
}
}
}
public static boolean writeCompObj(OutputStream out,IntIODetail o)
throws IOException{
try {
byte[] keyBytes = "1234123412341234".getBytes(); //example
final byte[] ivBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; //example
final SecretKey key = new SecretKeySpec(keyBytes, "AES");
final IvParameterSpec IV = new IvParameterSpec(ivBytes);
final Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key, IV);
try(CipherOutputStream cstream = new CipherOutputStream(out, cipher)){
return writeObj(cstream,o);
}
} catch(Exception ex) {
return false;
}
}
public static IntIODetail readCompObj(InputStream in)
throws IOException{
try {
byte[] keyBytes = "1234123412341234".getBytes();
final byte[] ivBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
final SecretKey secretkey = new SecretKeySpec(keyBytes, "AES");
final IvParameterSpec IV = new IvParameterSpec(ivBytes);
final Cipher decipher = Cipher.getInstance("AES/CBC/NoPadding");
decipher.init(Cipher.DECRYPT_MODE, secretkey, IV);
try(CipherInputStream cin=new CipherInputStream(in,decipher)){
return readObj(cin);
}
} catch(InvalidKeyException | InvalidAlgorithmParameterException
| NoSuchPaddingException |NoSuchAlgorithmException ex) {
return null;
}
}
要获取完整代码,请访问https://github.com/computer-developers/Program-Tester/tree/master/src/lib/runDetails。
编辑: - 因为'EJP'建议我添加了堆栈跟踪。
Exception in thread "main" java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2353)
at java.io.ObjectInputStream$BlockDataInputStream.readUTFBody(ObjectInputStream.java:3092)
at java.io.ObjectInputStream$BlockDataInputStream.readUTF(ObjectInputStream.java:2892)
at java.io.ObjectInputStream.readString(ObjectInputStream.java:1646)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1344)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373)
at java.util.ArrayList.readObject(ArrayList.java:791)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1058)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1909)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1808)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2018)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1942)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1808)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373)
at lib.runDetails.ObjectHandler.readObj(ObjectHandler.java:115)
at lib.runDetails.ObjectHandler.readCompObj(ObjectHandler.java:184)
at lib.runDetails.IOManager.getIODetail(IOManager.java:133)
at testRunDetails.IOManagerTest2.main(IOManagerTest2.java:27)
虽然对象的大小并不重要,但我必须提到该对象具有非凡的数据,例如两个字符串列表超过100000个元素。
在这里,我创建了Buffered Streams
来解决问题,但它无法正常工作。
答案 0 :(得分:0)
我已经解决了这个......
我尝试了不同的加密算法和解密。
我将AES/CBC/NoPadding
替换为AES/CBC/PKCS5Padding
&amp;在BufferedStream
&amp;之间使用FileStreams
CypherStreams
。
现在代码流程就像Object > ObjectOutputStream > CipherOutputStream > BufferedOutputStream > FileOutputStream > File
。 &安培;阅读Object < ObjectInputStream < CipherInputStream < BufferedInputStream < FileInputStream < File
。
我仍然不明白实际问题。虽然问题已经解决但我很想知道实际问题。