0

I have 2 classes, the first is fed a binary string of 1s and 0s to standard in, and splits the string into sub strings of length 8 to convert to bytes, to write to standard out like so:

byte b = (byte)Integer.parseInt(byteString, 2);
System.out.write(b);
System.out.flush();

The second is piped the output of the first, and ultimately will turn the bytes back into the long binary string, but currently I have it just doing this:

int next;
    while((next = System.in.read())!=-1){
    System.out.print(next); System.out.print(" ");
    System.out.flush();
}

If I replace System.out.write(b); with System.out.print(b); System.out.print(" "); in the first class with some small sample input, the output is -80 -84 83 11 98 -116 53 -119 49 27 10 -80 -72 104 -123 0

If I pipe this output to the second class it outputs 63 63 83 11 98 63 53 63 49 27 13 10 63 63 104 63 0 13 10

Two things seem to be happening that I can't figure out why, System.in.read() is returning 63, which is ASCII for ? when reading bytes that would be signed negative, and a carriage return + new line has been added to the end. I am stumped for solutions, insight is greatly appreciated.

Note: I was running this in Windows 10 powershell, but on normal command line and in a linux terminal the behaviour was as expected, so problem is only in powershell!

user85421
  • 28,957
  • 10
  • 64
  • 87
ogilberry
  • 73
  • 7
  • Java `System.in.read()` reads raw binary. Bytes, not ASCII strings, which is what you are printing. Try wrapping System.in in a Reader of some sort. – markspace Apr 02 '17 at 03:34
  • I was running this in Windows 10 powershell, but on normal command line and in a linux terminal the behaviour was as expected, so problem is only in powershell but still not sure why – ogilberry Apr 02 '17 at 06:26
  • 1
    @ogilberry this information should be part of the question and not just a comment... maybe addind the powershell tag also helps – user85421 Apr 02 '17 at 08:20

2 Answers2

2

I do not use powershell, but a small test shows that pipes do change the output, like adding a newline and carriage return at the end, or converting the byte 0xA2 to 0x3f ('?').

Searching the net I found Understanding the Windows PowerShell Pipeline. I did not read more than the first paragraph, but that contains a hint:

Piping works virtually everywhere in Windows PowerShell. Although you see text on the screen, Windows PowerShell does not pipe text between commands. Instead, it pipes objects.

Maybe this helps: What exactly does the pipe | mean in PowerShell?

Community
  • 1
  • 1
user85421
  • 28,957
  • 10
  • 64
  • 87
0
int next;
while((next = System.in.read())!=-1){
    System.out.print(next); System.out.print(" ");
    System.out.flush();
}

Bytes are signed in Java, and so is the result of InputStream.read(). Try this:

int next;
DataInputStream in = new DataInputStream(System.in);
for (;;)
{
    try
    {
        next = in.readUnsignedByte();
        System.out.print(next); System.out.print(" ");
        System.out.flush();
    }
    catch (EOFException exc)
    {
        break;
    }
}
user207421
  • 305,947
  • 44
  • 307
  • 483
  • I believe the result of `InputStream.read()` is an `int` and always non-negative or -1 (EOF), javadoc: "...The value byte is returned as an int in the range 0 to 255... ", the question is not showing any negative number being read ("63 63 83 11 98 63 53 63 49 27 13 10 63 63 104 63 0 13 10"), and it is working on normal command line or linux shell, just not in a powershell (see question comments) - and above code should give same result as question's one... (it is `read(byte[])` that can/will return negative bytes) – user85421 Apr 02 '17 at 16:05
  • just tested your code, **it is** having the same behavior as question ones! – user85421 Apr 02 '17 at 16:10