Deserialize ByteBuffer to InputStream java - java

I am trying to deserialize ByteBuffer to an InputStream and then to an Object in Java. This is my usecase:
# Initially I have a list of objects List<objA> objs, I converted them to a File and then to ByteBuffer like this.
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(filePath)), "UTF-8"));
for (objA obj: objs) {
StringBuilder row = new StringBuilder();
row.append(obj.getProp1()).append(SEPARATOR);
row.append(obj.getProp2()).append(SEPARATOR);
writer.write(row.toString());
}
File file = new File(filepath);
InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
ByteBuffer buff = ByteBuffer.wrap(IOUtils.toByteArray(inputStream));
Now, I want to exactly reverse this operation and get the List back, how can I do that? I couldn't find many references on this topic. Can someone help here.
Please note that I cannot change the above code snippet, I want to know how can I get the original list back from the ByteBuffer created from the above code snippet.

Related

Java OutStreamWriter to ByteArrayInputStream

I am writing a csv file in a very old java application so i can not use all the new Java 8 streams.
Writer writer = new OutputStreamWriter(new FileOutputStream("file.csv"));
writer.append("data,");
writer.append("data,");
...
Then I need to transform the writer object into a ByteArrayInputStream.
How can i do it ?
Thanks in advance.
Best regards.
This depends on what you are trying to do.
If you are writing a bunch of data to the file and THEN reading the file you will want to use a FileInputStream in place of your ByteArrayInputStream.
If you want to write a bunch of data to a byte array then you should take a look at using a ByteArrayOutputStream. If you then need to read the byte array as a ByteArrayInputStream you can pass the ByteArrayOutputStream into the input stream like what is shown below. Keep in mind this only works for writing and THEN reading. You can not use this like a buffer.
//Create output stream
ByteArrayOutputStream out = new ByteArrayOutputStream();
//Create Writer
Writer writer = new OutputStreamWriter(out);
//Write stuff
...
//Close writer
writer.close();
//Create input stream using the byte array from out as input.
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Short answer: you can't.
A ByteArrayInputStream is just not assignable from a OutputStreamWriter.
Since you're probably after write, you can just read the file back to a byte[] and then construct a ByteArrayInputStream with it:
File file = new File("S:\\Test.java");
FileInputStream fis = new FileInputStream(file);
byte[] content = new byte[(int) file.length()];
fis.read(content,0,content.length);
ByteArrayInputStream bais = new ByteArrayInputStream(content);

read FIile content as bytes java

I have 2 java classes. Let them be class A and class B.
Class A gets String input from user and stores the input as byte into the FILE, then Class B should read the file and display the Byte as String.
CLASS A:
File file = new File("C:\\FILE.txt");
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
String fwrite = user_input_1+"\n"+user_input_2;
fos.write(fwrite.getBytes());
fos.flush();
fos.close();
In CLASS B, I wrote the code to read the file, but I don't know how to read the file content as bytes.
CLASS B:
fr = new FileReader(file);
br = new BufferedReader(fr);
arr = new ArrayList<String>();
int i = 0;
while((getF = br.readLine()) != null){
arr.add(getF);
}
String[] sarr = (String[]) arr.toArray(new String[0]);
The FILE.txt has the following lines
[B#3ce76a1
[B#36245605
I want both these lines to be converted into their respective string values and then display it. How to do it?
Are you forced to save using a String byte[] representation to save data? Take a look at object serialization (Object Serialization Tutorial), you don't have to worry about any low level line by line read or write methods.
Since you are writing a byte array through the FileOutputStream, the opposite operation would be to read the file using the FileInputStream, and construct the String from the byte array:
File file = new File("C:\\FILE.txt");
Long fileLength = file.length();
byte[] bytes = new byte[fileLength.intValue()]
try (FileInputStream fis = new FileInputStream(file)) {
fis.read(bytes);
}
String result = new String(bytes);
However, there are better ways of writing the String to a file.
You could write it using the FileWriter, and read using FileReader (possibly wrapping them by the corresponding BufferedReader/Writer), this will avoid creating intermediate byte array. Or better yet, use Apache Commons' IOUtils or Google's Guava libraries.

Reading a UTF-8 string from ZipFileInputStream

I am trying to read a UTF-8 file from a zipFile and its turning out to be a major challenge.
Here I zip the String to a bytes array to persist to my db.
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipOutputStream zo = new ZipOutputStream( bos );
zo.setLevel(9);
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(bos, Charset.forName("utf-8"))
);
ZipEntry ze = new ZipEntry("data");
zo.putNextEntry(ze);
zo.write( s.getBytes() );
zo.close();
writer.close();
return bos.toByteArray();
And this is how I read the String back:
ZipInputStream zis = new ZipInputStream( new ByteArrayInputStream(bytes) );
ZipEntry entry = zis.getNextEntry();
byte[] buffer = new byte[2048];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int size;
while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, size);
}
BufferedReader r = new BufferedReader( new InputStreamReader( new ByteArrayInputStream( bos.toByteArray() ), Charset.forName("utf-8") ) );
StringBuilder b = new StringBuilder();
while (r.ready()) {
b.append( r.readLine() ).append(" ");
}
The String that I get back here has lost the UTF8 charecters!
UPDATE 1:
I changed the code around so that I compared the byte array of the original String with the byte array I read back from the zipfile and they freaking match! So its probably how I'm building the string after i have the bytes.
Arrays.equals(converted, orgi)
Your problem is in the writing, presuming s is a String, you have:
zo.write( s.getBytes() );
But that will convert s to bytes using whatever the default encoding is. You'll want to use UTF-8 for that conversion:
zo.write( s.getBytes("utf-8") );
Your observation that the original bytes are the same as the uncompressed bytes make sense because the original written data is the source of the problem.
Note that you have the writer stream declared but you never actually use it for anything (nor should you, in this context, since writing to it will just write uncompressed string data to the same stream bos that your ZipOutputStream writes to). It looks like you may have confused yourself trying a few different things at once here, you should just get rid of writer.
For one, BufferedReader#ready() is not a good indicator for reading input. Here's a number of reasons why
Does BufferedReader.ready() method ensure that readLine() method does not return NULL?
BufferedReader not stating 'ready' when it should
Second, you are using
b.append( r.readLine() ).append(" ");
which is always adding a " " on every iteration. The resulting String value is bound to be different than the original just because of this.
Third, shout out to Jason C about your BufferedWriter not doing anything.

how to read contents of file, write to outputstream and the re-read and Store in a string variable in memory in java

I need to read contents of a file, write them to an outputstream using an API Writer (PEMWriter - which converts the format of the contents to required format) and then re-read the newly formatted content from this outputstream and store in a String variable.
Anyone know how to do this?
in the words of code:
PEMWriter writer = null;
Writer out = null;
Reader in = null;
String priv = null;
KeyStore ks = null;
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
ks.load(new FileInputStream(MyConfig.KEYTOOL_FILE),
MyConfig.PASSWORD.toCharArray());
KeyPair keyPair = getPrivateKey(ks, "myKey",
MyConfig.PASSWORD.toCharArray());
PrivateKey privateKey = keyPair.getPrivate();
out = new BufferedWriter (new OutputStreamWriter(new org.apache.commons.io.output.ByteArrayOutputStream()));
writer = new PEMWriter(out);
writer.writeObject(privateKey);
in = new BufferedReader (new org.apache.commons.io.input.NullReader( new Long(5000).longValue()));
IOUtils.copy(in, out);
priv = IOUtils.toString(in);
the above code should work as it has a Writer (out) which gets populated with contents of PEMWriter using bouncy castle api. the problem occurs when trying to re -read the contents of out using Reader (in) and storing them to String (priv). i get an IO Exception stating Read after end of file. this occurs on line IOUtils.copy(in, out);.
i'd appreciate some help on this problem. thanks in advance.
You can't read a stream or reader twice. Once it's read to the end it's at the end (Done in IOUtils.copy(in, out)). Then you try to read it again (IOUtils.toString(in)).
To write both file and string in one step you could use a TeeWriter (like this one) and a StringWriter
Or copy the input to a String and write the string to out:
priv = IOUtils.toString(in);
out.wite(priv);
i believe the answer to my solution is to use pipes - a writer and reader pipe to connect the two together. so that i can read my out stream in a reader. i will only know after i try it out, but on research that seems the most correct ans to my problem
similar to
PipedWriter pwriter = new PipedWriter();
PipedReader preader = new PipedReader(pwriter);

Java sockets and file Streaming

I have a basic question on files ... It seems that I am stuck.
I am creating a server-client socket. The client sends a random number of integers to the server using an iterative way and the methods bellow.
//BufferedWriter out = new BufferedWriter(new OutputStreamWriter (sock.getOutputStream()));
out.write(number);
out.flush();
The server accepts them like this:
//BufferedReader in = new BufferedReader (new InputStreamReader (socket.getInputStream()));
number=in.read();
All , I want is the server to store all these integers into a file (myfile.txt for example) and then I want to read this file as a string (with all integers) in order to send it back to the client.
Any ideas? I tried few methods but right now I am totally stuck and I really cant think clear... I would really appreciate it if someone could help me out a bit.
Cheers
EDIT: I tried these methods so far
FileOutputStream fos = new FileOutputStream("myfile.txt");
DataOutputStream dos = new DataOutputStream(fos);
dos.writeInt(number);
And then I tried to read this with
FileInputStream fin=new FileInputStream("myfile.txt");
DataInputStream dis = new DataInputStream(fin);
int numbers = dis.read();
But all I get is the number 0. :S
Writing the file could be achieved like this
FileOutputStream fileOutputStream = new FileOutputStream("test.txt");
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
// Make sure to write the data as a String
bufferedWriter.write("" + number);
bufferedWriter.close();
fileOutputStream.close();
Afterwards, reading can be achieved like this
FileInputStream fileInputStream = new FileInputStream("test.txt");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
String line = bufferedReader.readLine();
number = Integer.valueOf(line);
Advantage of using a BufferedWriter and a BufferedReader is that you can read / write Strings and have a human readable file. Using a DataOutputStream, you'd have a binary file, and you'll have do conversion from / to your data format yourself.
Regarding your code example:
dis.read();
will return you the number of bytes read, not the actual data. You'd do that using
dis.readInt();

Categories

Resources