Can one write to a wrapped Stream in java? - 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");

Related

Why does my Printwriter not work like i want?

I have a Problem with my Printwriter in some of my Projects. I always have to close it because otherwise the send message of the Serversockets doesn´t come to the wished server.
Socket socket = serverSocket.accept();
console.writeMessageinConsole("Client"+socket.getInetAddress()+" verbindet!");
OutputStream outputStream = socket.getOutputStream();
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
PrintWriter printWriter = new PrintWriter(outputStream, true);
System.out.println("da");
printWriter.write("dadad");
printWriter.close();
Can you plase help me?
The only reason to use a BufferedWriter is to buffer the output. If you want to send immediately, don't use buffering.
The only reason to use a PrintWriter is to add println and printf. If you want to use write() you don't need a PrintWriter.
So you can either;
drop the BufferedWriter.
use println, or
flush() the output

Strange behavior of java streaming

I've the necessity to share a streaming of data between two instances as below:
// get EClasses which should be connected
final uk.man.xman.xcore.Parameter source = getParameter(sourceAnchor);
final uk.man.xman.xcore.Parameter target = getParameter(targetAnchor);
// Set data channels
//Output stream
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(outputStream));
source.setOutputStream(dataOutputStream);
//Input stream
DataInputStream inpuDataStream = new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(outputStream.toByteArray())));
target.setInputStream(inpuDataStream);
Everything works ok if I write, during those lines of code. Strangely, when I need to use the data channel to write something in another class, like here:
DataOutputStream dataOutputStream = (DataOutputStream) inputParameter.getOutputStream();
System.out.println("WRITE:" + attributes.getValue("value"));
dataOutputStream.writeUTF(attributes.getValue("value"));
dataOutputStream.flush();
I am not able to read, and I really do not know why. Am I missing something?
Thanks for your time
Not sure if that's what you're asking, but you're creating an InputStream that reads from an empty byte array. That doesn't make much sense:
// create an Output stream that will write in memory
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
...
// transform what has been written to the output stream into a byte array.
// Since othing has been written yet, outputStream.toByteArray() returns
// an empty array
DataInputStream inpuDataStream = new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(outputStream.toByteArray())));

how to write string with outputstream(Telnet client) using java

i tried to use this code but when i start the connection it seems that the server doesn't receive the string(the command):
public static void sendMessage(TelnetClient s, String myMessageString)
throws IOException {
OutputStream os = s.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(myMessageString);
}
i tried also to use only the outputstream and to do the following thing:
os.write(myMessageString.getbytes());
You definitely don't want to use ObjectOutputStream - that will use binary serialization that your telnet server won't be expecting.
It would be best to create an OutputStreamWriter:
// Adjust the encoding to whatever you want, but you need to decide...
Writer writer = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
writer.write(myMessageString);
writer.flush();
The flush call may well be what was missing before - depending on exactly what TelnetClient does, it may be buffering the data until you flush the stream.

is there any big difference of performance?? when using TCP Socket I/O code

when using TCP Socket I/O code.. Is there any big difference of performance between below two codes..?? The result of both is the same~~
// -------- 1 -------- //
OutputStream out = sock.getOutputStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
// -------- 2 -------- //
OutputStream out = sock.getOutputStream();
PrintWriter pw = new PrintWriter(out);
No, it shouldn't be. Quoting the doc:
public PrintWriter(OutputStream out, boolean autoFlush)
Creates a new PrintWriter from an existing OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will convert
characters into bytes using the default character encoding.
In other words, a fresh OutputStreamWriter object is created in both cases of yours.

How to change text file coding charset using java

As in subject. How to rewrite the file with different charset?
Where are can find available encodings - final static ints?
FileInputStream fis = new FileInputStream(inputFile);
InputStreamReader isr = new InputStreamReader(fis, inputEncoding);
BufferedReader in = new BufferedReader(isr);
FileOutputStream fos = new FileOutputStream(outputFile);
OutputStreamWriter osw = new OutputStreamWriter(fos, outputEncoding);
BufferedWriter out = new BufferedWriter(osw);
String line = in.readLine();
out.write(line);
As in subject. How to rewrite the file with different charset?
I'm not sure why you asked this question as your code seems legit, although it copies only 1 line (and swallows newlines). I wouldn't have used readLine(), but just read() in a loop, maybe with a buffer. This way you copy everything without modifying/swallowing newlines.
Where are can find available encodings - final static ints?
By Charset#availableCharsets().
SortedMap<String, Charset> availableCharsets = Charset.availableCharsets();
// ...
The supported encoding formats are specified in the JDK Documentation.
As per the conversion, you can use
Supported Encodings
Read Encoded Data
Write Encoded Data
Converting between String of different character sets
List all available character set converters

Categories