Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9085307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:12:48+00:00 2026-06-16T21:12:48+00:00

I have a program which will be receiving information from an external source via

  • 0

I have a program which will be receiving information from an external source via System.in. There are two input modes: line mode and raw mode. During line mode, input is simply a series of UTF-8 strings, each terminated with a line feed character. At some point while in line mode, I will receive notification that I am about to receive N bytes of raw data. At that point the input switches to raw mode and I receive exactly N bytes of raw binary data, which are not valid UTF-8 characters. After this point, it returns to line mode.

Is there a way to easily switch between reading strings and reading raw data? My only thought is to read an InputStream byte by byte and translate to characters as I go. Are there any ways to wrap System.in with multiple types of input streams? I feel like reading from two different wrappers would cause problems.

(FIXED) Update:

I tried parsifal’s suggestion, but am running into a problem. To simulate the switching input modes, I modified my test harness. (I realized that another process I have will eventually need to output this way as well.) I don’t know if the problem is caused by the send or receive end. When I switch between output modes, it doesn’t seem to be reading in the bytes properly. Also, it’s always the same byte values that appear. Here are some code excerpts:

FIX: The problem was that apparently you can’t switch from the OutputStreamWriter to OutputStream too quickly. I added a 1ms sleep command before sending the raw bytes, and the problem is solved!

Test Harness:

Process p = processList.get(pubName); //Stored list of started Processes
OutputStream o = p.getOutputStream(); //Returns OutputStream which feeds into stdin
out = new OutputStreamWriter(runPublisher.getOutputStream());

byte[] payload = new byte[25];
out.write("\nPAYLOAD\nRAW\n"); // "RAW\n" signals raw mode
out.write(String.valueOf(payload.length) + "\n");
out.flush();
Thread.sleep(1); //This fixed the problem I was having.
System.out.println(Arrays.toString(payload));
o.write(payload);
o.flush();

Client:

InputStreamReader inReader = new InputStreamReader(System.in);

while(true){
    try{
        if((chIn = inReader.read())!= -1){
            if(chIn == (int)'\n'){
                if(rawMode){
                    if(strIn.equals("ENDRAW"))
                        rawMode = false;
                    else{
                        System.out.println(strIn);
                        //Exception on next line
                        int rawSize = Integer.parseInt(strIn);
                        payload = new byte[rawSize];
                        int t = System.in.read(payload);
                        System.out.println("Read " + t + " bytes");
                        System.out.print(Arrays.toString(payload));
                    }
                }else if(strIn.startsWith("RAW")){
                    rawMode = true;
                }else {
                    // Do other things
                }
                strIn = "";
            }else
                strIn += (char)chIn;
        }else
            break;
    }catch(IOException e){break;}
}

And the outputs (prior to adding Sleep statement) look like this:

Test Harness:
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

Client:
25
Read 9 bytes
[83, 72, 85, 84, 68, 79, 87, 78, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Exception in thread "main" java.lang.NumberFormatException: For input string: "
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:470)
    at java.lang.Integer.parseInt(Integer.java:514)
    at myClass.handleCommand(myClass.java:249)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:12:50+00:00Added an answer on June 16, 2026 at 9:12 pm

    You can wrap System.in with an InputStreamReader that specifies “utf-8” encoding, and then read character-by-character. Accumulate characters into a StringBuilder and dispatch whenever appropriate (nomially when you see '\n', but possibly based on a test of the builder).

    When you want to read binary data, just read from the underlying InputStream (System.in). The InputStreamReader performs translation as-needed, and does not buffer data.

    You do not want to use any sort of buffered stream or reader in the stack. This will eliminate any opportunity to use a readLine() method, at least if you confine yourself to the JDK classes.


    Edit based on your latest updates:

    I think that your switching between raw and cooked mode is a bit suspicious. If I were to implement this, I’d create two primitive operations, String readLine() and byte[] readData(length). The first accumulates characters up to a newline, the second reads a fixed buffer. Then your main loop looks something like this:

    InputStream in = // ...
    Reader rd = new InputStreamReader(in, "USASCII");  // or whatever encoding you use
    
    while (true) {
        String command = readLine(rd );
        if (command .equals("RAW")) {
            int length = Integer.parseInt(readLine(rd ));
            byte[] data = readData(in , length);
            if (! readLine(rd ).equals("ENDRAW")) {
                throw // an exception that indicates protocol violation
            }
        }
        else // process other commands
    }
    

    I would also wrap the whole thing up in an object, which is constructed around the stream, and perhaps uses callbacks to dispatch the data packets.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program in which I will compare hash values generated from my
Basically, I have a program which will be launched more than once. So, there
How to write a java program which will tell me whether I have internet
I have a C++ program which, during execution, will allocate about 3-8Gb of memory
I have a program which runs an external, command line utility and reads the
I have a program which doesn't once reference System.I0, coded in vb.net, yet for
I have this test program which will fetch url parallel, but when I increase
I have the string: foo$bar@baz I'm looking to write a C program which will
I have a program which will prompt the user for 9 characters. Once these
Making a simple program which will generate a multiple choice form. I have an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.