I need to make java BufferedInputStream Serializable. Are there any alternatives or any other way to implement it?
Do yo see any issue in this implementation
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.Serializable;
public class SerializableBufferedInputStream extends BufferedInputStream implements Serializable
{
public SerializableBufferedInputStream(InputStream in)
{
super(in);
}
public SerializableBufferedInputStream(InputStream in, int size)
{
super(in, size);
}
}
First of all, BufferedInputStream Creates a BufferedInputStream and saves its argument, the input stream in, for later use.
but you are saying Serializable which means converting the state of an object into a byte stream
so why do you need to convert it ??
This thing may help you check (Provide your Code in case you want other than this)
public class MainClass {
public static void main(String[] args) throws Exception {
Punk obj1 = new Punk("A");
Punk obj2 = new Punk("B");
Punk obj3 = new Punk("V");
ObjectOutputStream objectOut = new ObjectOutputStream(new BufferedOutputStream(
new FileOutputStream("C:/punkObjects.bin")));
objectOut.writeObject(obj1); // Write object
objectOut.writeObject(obj2); // Write object
objectOut.writeObject(obj3); // Write object
objectOut.close(); // Close the output stream
ObjectInputStream objectIn = null;
int objectCount = 0;
punk object = null;
objectIn = new ObjectInputStream(new BufferedInputStream(new FileInputStream(
"C:/punkObjects.bin")));
// Read from the stream until we hit the end
while (objectCount < 3) {
object = (punk) objectIn.readObject();
objectCount++;
System.out.println(object);
}
objectIn.close();
}
}
class Punk implements Serializable {
String str;
public Punk(String s) {
str = s;
}
}
Related
I want to read and write an object to a file. This is my attempt:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
public class SaveOpen implements Serializable
{
private static String fileName;
private ArrayList<Person> list = new ArrayList<Person>();
public SaveOpen() {
fileName = "file.txt";
}
//Reader
public static Object deserialize() throws IOException,
ClassNotFoundException {
FileInputStream fis = new FileInputStream(fileName);
BufferedInputStream bis = new BufferedInputStream(fis);
ObjectInputStream ois = new ObjectInputStream(bis);
Object obj = ois.readObject();
ois.close();
return obj;
}
//Writer
public static void serialize(Object obj)
throws IOException {
FileOutputStream fos = new FileOutputStream(fileName);
BufferedOutputStream bos = new BufferedOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.close();
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public ArrayList<Person> getListPersons() {
return list;
}
}
However, I do not know if this is the correct way nor how to implement this in a class. The object is Person and I want to save and read that object from a file. Is it supposed to be done to a .txt file? Anyone who can clearify things? Thanks!
if you want the file to be human readable i would suggest to save it as xml.
Example :
Object Class
import java.io.Serializable;
public class Person implements Serializable
{
private String username;
private int id;
public String UserName() { return username; }
public void setUserName(String str) { username = str;}
public int ID() { return id; }
public void setID(int ID) { id = ID; }
}
-Serializer/Deserializer
import Settings.Person;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class XmlSerializer
{
//File path to serialize to and deserialize from
private static final String SERIALIZED_FILE_NAME = "yourSavePath.xml";
//Insert person object and save as xml file to chosen filepath
public static void Serialize(Person person)
{
try
{
FileOutputStream os = new FileOutputStream(SERIALIZED_FILE_NAME);
XMLEncoder encoder = new XMLEncoder(os);
encoder.writeObject(person);
encoder.close();
}
catch(FileNotFoundException ex)
{
System.out.println(ex.getMessage());
}
}
//Deserialize xml file into person object
public static Person Deserialize()
{
try
{
FileInputStream os = new FileInputStream(SERIALIZED_FILE_NAME);
XMLDecoder decoder = new XMLDecoder(os);
Person p = (Person)decoder.readObject();
decoder.close();
return p;
}
catch(FileNotFoundException ex)
{
System.out.println(ex.getMessage());
}
return null;
}
}
You're doing it right already. You can safe Objects in a txt file altough it makes not much sense, I'd rather go with a binary file.
To store multiple Objects in a single File, simply pack them in a Collection and then serialize the Collection object.
When reading an Object from a File, check its Class via instanceof and cast it to whatever it is.
I have a list of Attachment type [Attachment is a class which contains some getters and setters].But due to some reasons I need to convert this list to string and after that I have to fetch this list from string.
public class Attachment{
private Integer attachmentCode;
private String attachmentDesc;
}
Attachment attach1 = new Attachment();
Attachment attach2 = new Attachment();
List<Attachment> tempList = new ArrayList<>();
tempList.add(attach1);
tempList.add(attach2);
HibernateClass record = new HibernateClass();
record.setValue(tempList .toString());
If I want to fetch Attachment object from this String value, how can I achieve my values from this list?
There are several approaches I guess. Using XML or JSON or any other textual format would also be a valid approach.
What about using object serialization and Base64 like follows:
import java.io.*;
import java.nio.charset.*;
import java.util.*;
public class Serialization {
public static void main(String[] args) throws Exception {
Attachment attach1 = new Attachment();
Attachment attach2 = new Attachment();
List<Attachment> tempList = new ArrayList<>();
tempList.add(attach1);
tempList.add(attach2);
String value = serialize(tempList);
List<Attachment> attachments = deserialize(value);
}
private static List<Attachment> deserialize(String value) throws Exception {
byte[] decode = Base64.getDecoder().decode(value);
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(decode));
return (List<Attachment>) ois.readObject();
}
private static String serialize(List<Attachment> tempList) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(baos);
os.writeObject(tempList);
byte[] encode = Base64.getEncoder().encode(baos.toByteArray());
return new String(encode, Charset.defaultCharset());
}
private static class Attachment implements Serializable {
private Integer attachmentCode;
private String attachmentDesc;
}
}
This seems to be simple but I failed to get a serialized JsonNode deserialized. Here is my test class
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Foo implements Serializable {
private String string;
private transient JsonNode jsonNode;
public Foo(String string, JsonNode jsonNode) {
this.string = string;
this.jsonNode = jsonNode;
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
if (this.jsonNode != null) out.writeObject((new ObjectMapper()).writeValueAsBytes(this.jsonNode));
// out.writeObject(this.jsonNode.textValue());
}
private void readObject(ObjectInputStream in) throws IOException,ClassNotFoundException {
in.defaultReadObject();
this.jsonNode = (new ObjectMapper()).readValue(in, JsonNode.class);
}
}
When I tried to deserialize I got this error
com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
Here is the unit test
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.testng.annotations.Test;
import java.io.*;
import static org.testng.Assert.assertEquals;
public class FooTest {
#Test
public void testSerialization() {
JsonNodeFactory nodeFactory = new JsonNodeFactory(false);
ObjectNode node = nodeFactory.objectNode();
ObjectNode child = nodeFactory.objectNode(); // the child
child.put("message", "test");
node.put("notification", child);
Foo foo = new Foo("Bar", node);
String fileName = "foo.ser";
try (
OutputStream file = new FileOutputStream(fileName);
OutputStream buffer = new BufferedOutputStream(file);
ObjectOutput output = new ObjectOutputStream(buffer);
){
output.writeObject(foo);
}
catch(IOException ex){
ex.getStackTrace();
}
Foo fooNew = null;
//deserialize the ser file
try(
InputStream file = new FileInputStream(fileName);
InputStream buffer = new BufferedInputStream(file);
ObjectInput input = new ObjectInputStream (buffer);
){
//deserialize the Object
fooNew = (Foo) input.readObject();
}
catch(ClassNotFoundException ex){
ex.printStackTrace();
}
catch(IOException ex){
ex.printStackTrace();
}
assertEquals(foo, fooNew);
}
}
Your read and write operations are not matched.
On the write side you use ObjectOutputStream.writeObject(Object) to write a byte[] containing the serialized JSON content. On the read side you try to read raw bytes off the stream with ObjectMapper.readValue(InputStream, Class) when you actually need to read a byte[] object first as that is what you wrote and then use ObjectMapper.readValue(byte[], Class).
Alternatively and probably a better solution is you could use ObjectMapper.writeValue(OutputStream, Object) instead on the write side.
Try this:
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
if(jsonNode == null){
out.writeBoolean(false);
} else {
out.writeBoolean(true);
new ObjectMapper().configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false).writeValue(out, jsonNode);
}
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
if(in.readBoolean()){
this.jsonNode = new ObjectMapper().configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false).readValue(in, JsonNode.class);
}
}
I am trying to extend InputStream class and use customized read() methods.
This is my class snapshot:
class MyClass
{
/** Input stream */
private final MyInputStream in = new MyInputStream();
/**get the InputStream
public InputStream getInputStream()
{
return in;
}
/** Inner class for MyInputStream */
class MyInputStream extends InputStream
{
//here i am keeping implementation of read methods
public synchronized int read( byte b[] ) throws IOException
{
//..................
}
}
}
Here is my client class
public class MyClient {
//InStreams
protected BufferedInputStream mBufInStream;
protected DataInputStream mInStream;
public int read(byte[] buffer)
{
MyClass obj1 = new MyClass();
mBufInStream = new BufferedInputStream(obj1.getInputStream());
mInStream = new DataInputStream(mBufInStream);
try
{
int i = mBufInStream.read(buffer);
return i;
}
catch (IOException ex)
{
return -1;
}
}
public static void main(String args[])
{
MyClient cl1 = new MyClient();
int ret = 0;
byte[] data = {};
ret = cl1.read(data);
}
}
What i wanted to do is call my read method of MyInputStream Class when cl1.read is done.
I don't know what i am missing here.
I created the DataInputStream object using MyInputStream and got it working. Here is the updated code:
public class MyClient {
//InStreams
protected DataInputStream mInStream;
public int read(byte[] buffer)
{
MyClass obj1 = new MyClass();
mInStream = new DataInputStream(obj1.getInputStream());
try
{
int i = mInStream.read(buffer);
return i;
}
catch (IOException ex)
{
return -1;
}
}
public static void main(String args[])
{
MyClient cl1 = new MyClient();
int ret = 0;
byte[] data = {};
ret = cl1.read(data);
}
}
If you are extending input stream class then you will need to give the concrete definition for the following method:
public abstract int read() throws IOException
Your class has the read method with the signature as:
public int read(byte[] b) throws IOException
So please implement read() in addition to read(byte[] b). I have made some modifications and it works now...
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
public class MyClient {
//InStreams
protected BufferedInputStream mBufInStream;
protected DataInputStream mInStream;
public int read(byte[] buffer) {
MyClass obj1 = new MyClass();
// mBufInStream = new BufferedInputStream(obj1.getInputStream());
// mInStream = new DataInputStream(mBufInStream);
try {
int i = obj1.getInputStream().read(buffer);
return i;
} catch (IOException ex) {
return -1;
}
}
public static void main(String args[]) {
MyClient cl1 = new MyClient();
int ret = 0;
byte[] data = {'a','b'};
ret = cl1.read(data);
System.out.println(ret);
}
}
import java.io.IOException;
import java.io.InputStream;
class MyClass {
/** Input stream */
private final MyInputStream in = new MyInputStream();
//get the InputStream
public InputStream getInputStream() {
return in;
}
class MyInputStream extends InputStream {
//here i am keeping implementation of read methods
public int read( byte b[] ) throws IOException {
System.out.println("Inside my read()");
return b.length;
//..................
}
#Override
public int read() throws IOException {
// TODO Auto-generated method stub
return 0;
}
}
}
I have used this code to store Object to a file:
try{
FileOutputStream saveFile=new FileOutputStream("SaveObj.sav");
ObjectOutputStream save = new ObjectOutputStream(saveFile);
save.writeObject(x);
save.close();
}
catch(Exception exc){
exc.printStackTrace();
}
}
}
How remove the single Object??
How clear the file??
Well, emptying out a file is very easy -- just open it for writing, and close it again:
new FileOutputStream("SaveObj.sav").close();
That will empty it out. If you were trying to erase one object out of many, though, that's a lot more complicated. You'd either have to read in all the objects and write out only the ones you want to keep, or you'd have to keep an index of the file offsets at which each object starts (probably in a separate file.) At that point you'd want to consider using an object database instead.
Ernest is right in that a removal of a particular object from the object-stream is slightly more complicated. He is also right that when you want to empty a file, you can simply open it for writing and close it. But if you want to remove it from the file-system, it is fine to do it using the File object (do not forget to handle the exceptions and return values correctly). The following example may not be perfect, but it should give you a hint on how to achieve your goals with pure Java. Hope this helps...
package test;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception {
String filename = "object.serialized";
{
List objects = new ArrayList();
objects.add("String1");
objects.add("String2");
objects.add("String3");
writeObjectsToFile(filename, objects);
}
{
List objects = readObjectsFromFile(filename);
objects.remove(1);
writeObjectsToFile(filename, objects);
}
{
List objects = readObjectsFromFile(filename);
for (Object object : objects) {
System.out.println(object);
}
}
emptyFile(filename);
deleteFile(filename);
}
private static void emptyFile(String filename) throws IOException {
OutputStream os = null;
try {
os = new FileOutputStream(filename);
} finally {
if (os != null) {
os.close();
}
}
}
private static void deleteFile(String filename) {
File f = new File(filename);
if (f.delete()) {
System.out.println(filename + " deleted sucessfully...");
} else {
System.out.println(filename + " deletion failed!");
}
}
private static void writeObjectsToFile(String filename, List objects) throws IOException {
OutputStream os = null;
try {
os = new FileOutputStream(filename);
ObjectOutputStream oos = new ObjectOutputStream(os);
for (Object object : objects) {
oos.writeObject(object);
}
oos.flush();
} finally {
if (os != null) {
os.close();
}
}
}
private static List readObjectsFromFile(String filename) throws IOException, ClassNotFoundException {
List objects = new ArrayList();
InputStream is = null;
try {
is = new FileInputStream(filename);
ObjectInputStream ois = new ObjectInputStream(is);
while (true) {
try {
Object object = ois.readObject();
objects.add(object);
} catch (EOFException ex) {
break;
}
}
} finally {
if (is != null) {
is.close();
}
}
return objects;
}
}
Outputs:
String1
String3
object.serialized deleted sucessfully...
I know there was a long time from this subject, but just to help future coming people, what works for me was to write the object again as a null value:
public static void writeIncidentsObjectsInCache(Object object) throws IOException {
writeObject(INCIDENTS_CACHE, object); }
public static Object readIncidentsObjectFromCache() throws IOException,
ClassNotFoundException {
return readObject(INCIDENTS_CACHE); }
public static void clearIncidents() throws IOException, ClassNotFoundException {
writeIncidentsObjectsInCache(null); }
public static void writeObject(String key, Object object) throws IOException {
FileOutputStream fos = TheAAApp.getApp().openFileOutput(key, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object);
oos.close();
fos.close();
}
public static Object readObject(String key) throws IOException,
ClassNotFoundException {
FileInputStream fis = TheAAApp.getApp().openFileInput(key);
ObjectInputStream ois = new ObjectInputStream(fis);
Object object = ois.readObject();
return object;
}