Is there any way to convert PrintWriter into ByteArrayOutputStream? - java

I want to convert PrintWriter object into ByteArrayOutputStream. and this I am displaying as a PDF.

I don't know how you are using PrintWriter exactly (please post your code), but instead of converting objects you can write lines directly to ByteArrayOutputStream like this:
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(byteStream);
pw.write("example");
pw.flush();
or (flush after closing PrintWriter):
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(byteStream);
pw.write("example");
pw.close();
or (auto-flushable):
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(byteStream, true);
pw.println("example");
Let me know whether you prefer other solution and add some more details then(your code).

Related

Can one write to a wrapped Stream in java?

I'm writing a java server for an assignment, and I have observed some strange behaviour when I write both into a wrapped stream and a wrapper stream, can this cause any problems ? As far as I see, it can, but how ? Pls enlighten me.
as an example:
OutputStream os = new OutputStream(...);
PrintWriter pw = new PrintWriter(os);
And I want to write both in the PrintWriter, and the OutputStream.
To transfer from byte-based stream to character-based stream, you need to use OutputStreamWriter:
An OutputStreamWriter is a bridge from character streams to byte streams.
So that would be:
OutputStream os = ...
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
PrintWriter pw = new PrintWriter(osw);
I think the problem is that you need to specify the encoding, since the constructor PrintWriter(OutputStream out) uses the default encoding which might not be correct for your data input:
OutputStream os = ...
PrintWriter pw = new PrintWriter(os, "UTF-8");

what about one BufferedOutputStream wrap another BufferedOutputStream?

what about one BufferedOutputStream wrap another BufferedOutputStream? this question is simple. but confused.
As the following code,
OutputStream file = new FileOutputStream("test.txt");
OutputStream buffer = new BufferedOutputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(buffer); //wrap buffer twice
OutputStream outputStream = new ObjectOutputStream(bufferedOutputStream); // wrap as ObjectOutputStream
BufferedOutputStream bufferedOutputStream1 = new BufferedOutputStream(outputStream); //wrap back as BufferedOutputStream
ObjectOutput output = new ObjectOutputStream(bufferedOutputStream1);
What about it? Question?
If you're asking whether an inefficiency is introduced, the answer is 'no'. The code is optimised to handle that case, or rather the case where the transfer size >= the buffer size.

How to make the PrintWriter to write UTF-8?

How to make the PrintWriter to write UTF-8?
pstream = new PrintWriter(csocket.getOutputStream(), true);
String res = "some string";
pstream.println(res); // here I want to output string as UTF-8
Use an OutputStreamWriter:
pstream = new PrintWriter(new OutputStreamWriter(
csocket.getOutputStream(), StandardCharsets.UTF_8), true)
OutputStream os = new FileOutputStream("file.txt");
PrintWriter pw = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));
Look at Java: Difference between PrintStream and PrintWriter discussion.
To be quick: you can use -Dfile.encoding=utf8 JVM parameter or method suggested in the discussion (see second answer).
PrintWriter out1 = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
out1 = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8),true);
} else {
out1 = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"), true);
}
Don't user PrintWriter. If you need UTF-8 encoding, just write direct to the OutputStream.
csocket.getOutputStream().write(res.getBytes("UTF-8"));
you can only write to file with any charset, otherwise platform default charset used see doc

Pass serilaized object as parameter to HTTPServer in Java

How can I pass a serilaized object as a param in a GET request to my HTTPServer? Please let me know as I have been unable to find a way to do the same.
You can try something like this
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.close();
String prm = URLEncoder.encode(new String(bos.toByteArray(), "ISO-8859-1"), "ISO-8859-1");
On the server side:
servletRequest.setCharacterEncoding("ISO-8859-1");
String s = servletRequeset.getParameter("obj");
byte[] bytes = s.getBytes("ISO-8859-1");
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
Object obj = ois.readObject();
It's also possible to use Base64 instead of URL Encoding, the main idea is to pass serialized bytes in URL

Java text file I/O

Which is the fast way to write text file in java?
At the moment i use this way to write a text file:
FileOutputStream fos = new FileOutputStream('FileName');
DataOutputStream dos = new DataOutputStream(fos);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(dos, Charset.forName(this.config.getCharset())));
My file size will be up 3 GB.
Flush the buffer after significant chunk of data is written. FileOutputStream should be just enough for text files. There is no need for using DataOutputStream.
how about
FileOutputStream fos = new FileOutputStream('FileName');
BufferedOutputStream bof = new BufferedOutputStream(fos);
bof.write("some text".getBytes()); // or just byte array
or
FileWriter fstream = new FileWriter("out.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write("Some text");
You do not need to use DataOutputStream here.

Categories