3

I need to read zero-terminated strings from InputStream in Java.

Is there similar to BufferedReader.readLine() method for reading zero-terminated strings?

4 Answers 4

9
package com;

import java.io.*;
import java.util.Scanner;

public class AAA {

    private static final String ENCODING = "UTF-8";

    public static void main(String[] args) throws Exception {
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        bOut.write("the first line".getBytes(ENCODING));
        bOut.write(0);
        bOut.write("the second line\r\n (long one)".getBytes(ENCODING));
        bOut.write(0);
        bOut.write("the third line".getBytes(ENCODING));
        printLines(new ByteArrayInputStream(bOut.toByteArray()));
    }

    public static void printLines(InputStream in) {
        Scanner scanner = new Scanner(in, ENCODING);
        scanner.useDelimiter("\u0000");
        while (scanner.hasNext()) {
            System.out.println(scanner.next());
            System.out.println("--------------------------------");
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This. Scanner has got to be the most under utilized java.util.* class.
2

No. Java doesn't recognise a zero-terminated string as such. You'll have to read the InputStream and look for a 0 byte.

Note that this doesn't address the issue of character-encoding. The InputStream will give you the stream of bytes, and you'll then have to encode to characters via a Reader. If you have a multi-byte character encoding then the issue becomes more complex.

5 Comments

If that works depends on the character encoding. Actually he needs to read from a Reader and look for a 0 character, not a 0 byte.
He explicitly asks for zero-terminated strings from the InputStream. It's confusing though since he then mentions BufferedReader
Since you can't read characters and strings from an InputStream, wouldn't it been nice of you to tell him that instead of confusing him with a halfway incorrect answer?
That's why a) I explicitly highlighted the 0 byte b) have clarified this following feedback. You may want to publish an answer yourself if you can produce one you're happier with.
MacGordon already did, so I don't know why I should repeat him. Actually "no" would have been a sufficient answer to Domas' question, since he just asked if there is a method similar to readLine for reading null-terminated strings.
1

you will also need to understand what is that "zero" means . input/output streams deal with bytes whereas readers/writers deal with characters. if you want to match against a zero character then byte to char conversion encoding will come into play.

Comments

0

You could create a method similar to the one below. Create a BufferedReader from an InputStream. The BufferedReader is passed by reference so it will retain state. It could easily be stored in an instance variable as well.

public String readLine(BufferedReader buff) throws IOException{
    int c = buff.read();
    // if EOF
    if (c == -1){
        return null;
    }
    StringBuilder builder = new StringBuilder("");
    // Check if new line or EOF
    while (c != -1 && c != 0){
        builder.append((char) c);
        c = buff.read();
    }
    return builder.toString();
}

....

String line = reader.readLine(buff);
while (line != null)
{
System.out.println(line);
line = reader.readLine(someBufferedReader);

}

3 Comments

This will throw away everything after the first 0-byte to the following EOL. In case your file only contains 0-byte-delimited Strings you’ll only get a single String from it.
The BufferedReader is passed by reference for the example so will keep its state. On the second call it will start where it left off.
this solution doesn't handle multibyte encoded characters. The solution of @dennis.zhdanov does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.