Get Binary Attribute from a MongoDB document in Java - java

Well, I am creating dumped objects using a ByteArrayOutputStream. And then will write them to MongoDB as an attribute in my documents. When I read it back how can I convert it to a byte array or a Binary without using a toString in the middle?
This is the way I do it now, and I dont think it is safe:
BasicBSONObject temp = (BasicBSONObject) models.get("0");
model = null;
if (temp != null) {
// TODO fix this serialization!
try {
ObjectInputStream ois = new ObjectInputStream(
new ByteArrayInputStream(temp.toString().getBytes()));
} catch (IOException e) {
L.error("unable to read model from string!");
L.error(e.getStackTrace().toString());
}
}

Related

ObjectOutputStream read in other language

I used to save items in Android using Android Studio with Java. But now I want to read in exactly that data in my rewritten app in Flutter SDK (Dart language). But it seems that the files that are written are very Java specific. This is the save and load method in Java.
public void save(Item item)
{
String json = gson.toJson(item);
FileOutputStream fos = null;
try {
fos = context.openFileOutput("item_"+ item.getUid(), Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(json);
os.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private Item loadItem(String fileName)
{
FileInputStream fis = null;
try {
fis = context.openFileInput(fileName);
ObjectInputStream is = new ObjectInputStream(fis);
Object object = is.readObject();
Item item = null;
String json = (String)object;
item = gson.fromJson(json, Item.class);
is.close();
fis.close();
return item;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
And now for reading it in Dart
Uint8List bytes = f.readAsBytesSync();
var data = bytes.buffer.asByteData();
// HOW TO DETECT JAVA
if (?SOMETHING?) { }
The length of bytes is quite large. It is an array of length = 3 245 500.
The length of the json string should be between 740 and 840 bytes long (i.e. a relatively short json).
How could I read in only the json part and not the entire Java 'overhead'/'java serialization format'
What you are currently doing is:
Serialize to JSON
Serialize the Java String with JSON data using Java serialization
Instead, change your code to:
Serialize to JSON (directly to file, or first to string, then to file using OutputStreamWriter wrapping a FileOutputStream).
Trying to reimplement Java serialization in another language is extremely painful, and your current usage of Java serialization makes little sense, and it is unnecessary overhead and only makes your life harder. Remove it from the mix and write the JSON directly to file.
It will simplify your code, and removes the need to implement Java serialization in Dart.
To be able to handle current files written in this combination of Java serialization wrapping JSON data, I would recommend using some extra code in your Java application to detect if a file is Java serialization and if so do a one-off conversion to store the JSON directly.
Alternatively, you can read up on the Java serialization protocol in Java Object Serialization Specification.

How to read the multiple objects text file in java?

How to read the multiple objects in a text file. I am trying to read the text file. I am always get only the first object of the file. How to get the all objects from text file...
List<Processedfile> processfiles = new ArrayList<Processedfile>();
Processedfile processfile = new Processedfile();
processfile.setFilename(filename);
processfile.setCountrow(uploadedFileCount);
processfile.setDate(dateformat);
processfiles.add(processfile);
writeReportTextFile(processfiles);
Write the processedfile object in the text file...
Write the file
public void writeReportTextFile(List<Processedfile> processfiles) {
String processedfilereport = "D:\\PaymentGatewayFiles\\MSSConsolidate\\processedfilereport.txt";
try {
File file = new File(processedfilereport);
FileOutputStream f = new FileOutputStream(file.getAbsoluteFile(), true);
// System.out.println(file);
ObjectOutputStream s = new ObjectOutputStream(f);
// System.out.println("the write"+reportfile);
s.writeObject(processfiles);
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Read the file..
public List<Processedfile> processreportfileread() {
List<Processedfile> a1 = new ArrayList();
String processedfilereport = "D:\\PaymentGatewayFiles\\MSSConsolidate\\processedfilereport.txt";
try {
File file = new File(processedfilereport);
FileInputStream r = new FileInputStream(file);
ObjectInputStream sp = new ObjectInputStream(r);
a1 = (List) sp.readObject();
System.out.println("the list is" +a1);
Iterator i = a1.iterator();
while(i.hasNext()) {
System.out.println("the iterator report is ===="+i.next());
}
}
catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
return a1;
}
The object serialization protocol / APIs can cope with a stream containing single object or a sequence of objects. But it cannot cope with a concatenation of streams1 ... which is what your application appears to be creating by opening the output file in "append" mode)
The solution is to NOT write them like that. This:
FileOutputStream f = new FileOutputStream(file.getAbsoluteFile(), true);
is fundamentally wrong. It results in multiple object streams being concatenated. That can't be read.
The correct approach is to either:
assemble all of the Processedfile objects into a single list, and write the list ... with one writeObject call, or
use a sequence of writeObject calls to write the Processedfile objects to a single ObjectOutputStream, or
write the objects to multiple output files, and send the files as a ZIP archive or similar.
1 - To understand why, you need to read the Object Serialization specification, and in particular you need to understand the serialization protocol / format.

Code after readObject() does not run

I am working on a UI which reads serialized object from zookeeper, deserializes it and then converts it into JSON. For some reason I am not able to deseerialize the MQTopic object. But I am able to do the same with other objects.
This is the part which converts the byte[] into the MQTopic object.
if (tester != null && tester.contains("com.ibm.mq.jms.MQTopic")) {
System.out.println(getValue());
ByteArrayInputStream in = new ByteArrayInputStream(this.value);
ObjectInputStream is = new ObjectInputStream(in);
System.out.println("after deserializing..");
topic = (MQTopic) is.readObject();
System.out.println("after typecasting..");
System.out.println(topic.getTopicName());
System.out.println(topic.toString());
is.close();
in.close();
}
Here value is a byte array of the object after serialization.
Nothing runs after topic = (MQTopic) is.readObject(); . Not even the print statements. The program neither terminates nor an exception is thrown or caught.
EDIT : Whole Method
public String getStrValue() {
FtpConnectionInfo ftp = null;
MQTopic topic = null;
try {
String tester = new String(this.value, "UTF-8");
if (tester != null && tester.contains("FtpConnectionInfo")) {
ByteArrayInputStream in = new ByteArrayInputStream(this.value);
ObjectInputStream is = new ObjectInputStream(in);
ftp = (FtpConnectionInfo) is.readObject();
in.close();
is.close();
Gson gson = new Gson();
return gson.toJson(ftp);
} else if (tester != null
&& tester.contains("com.ibm.mq.jms.MQTopic")) {
ByteArrayInputStream in = new ByteArrayInputStream(this.value);
ObjectInputStream is = new ObjectInputStream(in);
System.out.println("after deserializing..");
topic = (MQTopic) is.readObject();
System.out.println("after typecasting..");
System.out.println(topic.getTopicName());
System.out.println(topic.toString());
is.close();
in.close();
} else {
return new String(this.value, "UTF-8");
}
} catch (UnsupportedEncodingException ex) {
System.out.println("unsupported error ");
ex.printStackTrace();
//logger.error(Arrays.toString(ex.getStackTrace()));
} catch (Exception e) {
System.out.println("Exception in new logic.");
e.printStackTrace();
}
System.out.println("im out of try");
return null;
}
The FTP if loop works fine, but the Topic loop does not work beyond typecasting.
EDIT 2: This how the other team stores the object into Zookeeper
public static byte[] serialize(Object obj) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(out);
os.writeObject(obj);
return out.toByteArray();
}
The byte[] is stored in Zookeeper and that is what I am retrieving in my UI.
EDIT 3: I did a debug of the process and at the point where is is called, these are the values. Can anyone tell me if the object is right ?
You're doing this wrong. You should deserialize the object first, and then use instanceof to see what type it is. Converting binary data to String is poor practice at the best of times.
Your actual symptom is not credible. An exception must be thrown, or else you are blocking earlier than stated.
ObjectInputStream's readObject is a blocking method. First check using the available method if there is something to read without blocking.
available would most probably return 0 in this case.
This might be only half the solution you are looking for, but I think this would let you know if you have anything to read or not.

Appending New Object to the existing file

Hi I have an issue when trying to append new objects to the existing file..
Once the android app has been lunched again I want to get the existing file and add a new objects then read the objects from the existing file ... Actually, when I'm trying to read the object, the code will read only the first objects .. You can find below the code .. Could you please help ? Thanks
using the following method to write an objects :
public void saveObject(Person p, File f){
try
{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f, true));
oos.writeObject(p);
oos.reset();
oos.flush();
oos.close();
}
catch(Exception ex)
{
Log.v("Serialization Save Error : ",ex.getMessage());
ex.printStackTrace();
}
}
Using the following method to read an objects :
public Object loadSerializedObject(File f)
{
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
try{
Object loadedObj = null;
while ((loadedObj = ois.readObject()) != null) {
Log.w(this.getClass().getName(), "ReadingObjects") ;
}
return objects;
}finally{
ois.close();
}
} catch (StreamCorruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
Unfortunately you can't create a new ObjectOutputStream every time you want to append to the stream and then read everything back with a single stream. The constructor adds headers to the underlying stream before you start writing objects. You are probably seeing the java.io.StreamCorruptedException: invalid type code: AC exception, that's because the first header is 0xAC.
I don't know how many objects you are dealing with, but one option might be to read all your objects and then rewriting them all using a single ObjectOutputStream. That can get pricy if there are lots of objects. Alternatively, you might want to consider managing the serialization yourself manually through Externalizable. It can get painful though.

handling EOFException in java

I have created a method in my java assignment to write into a file from a LinkedList (I used serialization) , then I have created another method to read the file into the inkedList. The following is my method's body:
try {
FileInputStream fin = new FileInputStream("c:\\Info.ser");
ObjectInputStream ois = new ObjectInputStream(fin);
Employee e = (Employee) ois.readObject();
linkP.add(e);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
but it doesnt work right. I think this part:
Employee e = (Employee) ois.readObject();
linkP.add(e);
reads only the first object of the file into the linkedlist and ignores other objects. I surrounded it for loop and while loop several times but it causes EOFException. How can I change my method to read all of the file's objects into the LinkedList?
If you used LinkedList for serialization you should expect a LinkedList to deserialize:
linkP= (LinkedList) ois.readObject();
instead of
Employee e = (Employee) ois.readObject();
linkP.add(e);
The easiest way is to include the size of the list as the first thing written to the file. When you read the file, the first thing you retrieve is the size. Then you can read the expected number of objects.
Are you sure that the serialized file contains all of the elements? It looks to me like you might only be serializing one.
Note: Please also add the code where you create the info.ser file, since you may have corrupted the ObjectOutputStream by closing/reopening it for each object.
But to answer your question, the proper way of doing it (without catching exceptions) would be:
#Test
public void testSerializingListByEntries() throws Exception {
List<Serializable> list = new ArrayList<Serializable>();
list.add(new Date());
list.add(new Date());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeInt(list.size()); // Magic
for(Serializable o : list) {
oos.writeObject(o);
}
oos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
int count = ois.readInt();
List<Object> newList = new ArrayList<Object>();
for(int i = 0; i < count;i++) {
newList.add(ois.readObject());
}
ois.close();
assertEquals(list,newList);
}
Yes, you need to close the streams yourself of course. Omitted for readability.
Would probably need to see how you're writing in the first place but generally:
ObjectInputStream is = null;
try
{
is = new ObjectInputStream(new FileInputStream("c:/Info.ser"));
Object object = null;
while ((object = is.readObject()) != null)
{
linkP.add(object);
}
}
catch(Exception e)
{
//Whatever you need to do
}
finally
{
//Never forget to close your streams or you'll run into memory leaks
try
{
if (is != null)
{
is.close();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
Also, its probably better practice for you to handle the exceptions individually but I can't really tell what the streams throw so replace the (Exception e) with everything else.
surround Employee e = (Employee) ois.readObject();
linkP.add(e);
with a for loop as you suggested and surround the .readObject call with a try/catc(EOFException)
Just catch EOFException separately inside your reading loop and process it accordingly, i.e. break out of the loop.

Categories

Resources