I have created the following class to read Serializable object from file and write to a file.
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.channels.FileLock;
/**
* Class FileSerializeStreamer is used to read and write Serialise object to
* file.
*
* @author TapasB
*/
public class FileSerializeStreamer {
/**
* Constructor FileSerializeStreamer
*
* @author TapasB
*/
public FileSerializeStreamer() {
}
/**
* Method readSerializable reads the Serialised object from file and
* returns. If the file is null or doesn't exist then it returns null
* without throwing any error.
*
* @author TapasB
* @param file
* - the instance of the file to read from.
* @param clazz
* - the class type of the object.
* @return the instance of the object.
*/
public <T extends Serializable> T readSerializable(File file, Class<T> clazz) {
if (file == null || !file.exists()) {
return null;
}
RandomAccessFile serFile = null;
FileChannel fileChannel = null;
byte[] bytes = null;
try {
serFile = new RandomAccessFile(file, "rw");
fileChannel = serFile.getChannel();
ByteBuffer buffer = ByteBuffer.allocate((int) serFile.length());
FileLock fileLock = fileChannel.lock();
fileChannel.read(buffer);
fileLock.release();
bytes = buffer.array();
buffer.clear();
} catch (Exception cause) {
cause.printStackTrace();
} finally {
closeStream(fileChannel);
closeStream(serFile);
}
ByteArrayInputStream byteArrayInputStream = null;
ObjectInputStream objectInputStream = null;
try {
byteArrayInputStream = new ByteArrayInputStream(bytes);
objectInputStream = new ObjectInputStream(byteArrayInputStream);
return clazz.cast(objectInputStream.readObject());
} catch (Exception cause) {
cause.printStackTrace();
} finally {
closeStream(objectInputStream);
closeStream(byteArrayInputStream);
}
return null;
}
/**
* Method writeSerializable writes the Serialised object to the given file.
* If the file is null or doesn't exist then it returns without throwing any
* error.
*
* @author TapasB
* @param file
* - the instance of the file to write to.
* @param serialized
* - the instance to be written.
*/
public <T extends Serializable> void writeSerializable(File file, T serialized) {
if (file == null || !file.exists()) {
return;
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(serialized);
} catch (Exception cause) {
cause.printStackTrace();
} finally {
closeStream(objectOutputStream);
}
RandomAccessFile serFile = null;
FileChannel fileChannel = null;
try {
byte[] source = byteArrayOutputStream.toByteArray();
serFile = new RandomAccessFile(file, "rw");
fileChannel = serFile.getChannel();
ByteBuffer buffer = fileChannel.map(MapMode.READ_WRITE, 0, source.length);
FileLock fileLock = fileChannel.lock();
buffer.put(source);
fileLock.release();
buffer.clear();
} catch (Exception cause) {
cause.printStackTrace();
} finally {
closeStream(byteArrayOutputStream);
closeStream(fileChannel);
closeStream(serFile);
}
}
// Method closeStream
private void closeStream(Closeable closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (Exception cause) {
cause.printStackTrace();
}
}
}
I would like to know
- If there is any drawback present in the code?
- Is the right way to do it?
- What happens if the size of the data that I want to read or write exceeds the
Integer.MAX_VALUE
? How to handle that scenario?