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 8953807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:11:00+00:00 2026-06-15T14:11:00+00:00

I wrote a Java CLI program that reads lines from stdin and outputs a

  • 0

I wrote a Java CLI program that reads lines from stdin and outputs a possible completion for each line. I’m attempting to give it a gui, so I’m trying to build a drop-in replacement for System.in, to allow users to either use the gui or the cli.

So far, I got this replacement, whose method add is called when text is inputted in a JTextArea:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.util.LinkedList;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

public class GuiIn extends InputStream {

    protected LinkedBlockingQueue<Byte> buf;
    protected boolean closed;

    public GuiIn() {
        closed = false;
        buf = new LinkedBlockingQueue<Byte>();
    }

    @Override
    public void close() {
        closed = true;
    }

    /**
     * add strings to read in the InputStream. Arguments are ignored if the
     * stream is closed.
     * 
     * @param s
     *            a string. Ignored if null
     */
    public void add(String s) {
        if (closed || s == null) {
            return;
        }
        byte[] bs = s.getBytes();
        LinkedList<Byte> lbs = new LinkedList<Byte>();
        for (byte b : bs) {
            lbs.add(b);
        }
        buf.addAll(lbs);
    }

    @Override
    public int available() {
        return buf.size();
    }

    @Override
    public synchronized int read() throws InterruptedIOException {
        if (closed && buf.isEmpty()) {
            return -1;
        }
        Byte b = 0;

        while (true) {
            try {

                if ((b = buf.poll(100, TimeUnit.MILLISECONDS)) == null) {
                    if (closed && buf.isEmpty())
                        return -1;
                } else
                    break;

            } catch (InterruptedException e) { 
                throw new InterruptedIOException("interrupted: "
                        + e.getMessage());
            }
        }
        return b;
    }
}

However, when I tried it out with new BufferedReader(new InputStreamReader(in)); and attempted to readLine() it, it seems to block until it has enough characters (many), despite the fact that the text fed is always appended with a newline by my listener.

On the other hand, if in is set to System.in, each line is read as soon as it is inputted.

So, my question comes in two parts:

  1. Where does this difference come from?
  2. How to fix it?

Please note that reading from the bare GuiIn byte per byte works correctly, and that I already tried tricks like reducing the size of BufferedReader‘s buffer.

I also searched the web beforehand: this is not about creating a mock object; and ByteArrayInputStream is not an option either: it does not support appending.

  • 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-15T14:11:01+00:00Added an answer on June 15, 2026 at 2:11 pm

    One source of your problem might be that InputStreamReader might insist on reading farther ahead so that it can be sure the byte->char decoder it uses (and which is opaque to it) has enough bytes to produce a full char while System.in might know enough about the default encoding to provide just enough bytes.

    Before you start doing interactive console stuff with Java you should become familiar with the Console class and the readline to Java port.

    Then, instead of creating InputStreamReaders without specifying the encoding, I would abstract at a higher level:

    interface CommandLineSource {
      String readLine() throws IOException;
    }
    

    and then you can create one backed by the FileDescriptor.STDIN, and another by whatever you like so that you can automate input for testing.

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

Sidebar

Related Questions

I wrote a Java application that reads and sends SMS messages from a USB
If I package a application I wrote in java that writes and reads from
I wrote a Java program to add and retrieve data from an MS Access.
I have a Java applet that streams video (MJPEG) from a server. I wrote
I recently wrote a java crawler program that finds the video links in a
I wrote a java program that should fill a sqlite database with some rows
I wrote init.d script that suppose to run java CLI proccess. The problem is
I wrote a Java program that has 3 classes. When, I use javac, I
i wrote a java app that communicates and stores data in oracle; my question
so I wrote a Java program to change the IP of a machine. public

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.