fist of all I have written one object in file using FileOutputStream
,CipherOutputStream
& ObjectOutputStream
.
the flow of code is something like this Object > ObjectOutputStream > CipherOutputStream > FileOutputStream > File
.
now when I tried to read that object from file, the EOFException is being thrown.
the flow of reading object is like Object < ObjectInputStream < CipherInputStream < FileInputStream < File
.
I have one class IOManager which have methods IntIODEtail getIODetail(Path,boolean isComp)
& boolean writeIntIODetail(IntIODetail obj,Path dir,boolean comp)
which creates FileInputStream
or FileOutputStream
respectively & call Different & methods of factory class ObjectHandler to encrypt & write object to the stream or read & decrypt Object to the stream.
code for IOManager...
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;
}
}
code for 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;
}
}
for complete code please visit https://github.com/computer-developers/Program-Tester/tree/master/src/lib/runDetails.
Edit:- as 'EJP' suggested i have added stack trace.
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)
although size of the object does't matters but I have to mention that the object have remarkable data like two Lists of Strings having more than 100000 elements.
here I have created Buffered Streams
to resolve the problem, but it's not working.