import java.io.*;

/**
 * Read and write to console using Byte (8 bit) I/O  
 * This program reads "buffered" data using streams
 * Create a BufferedInputStream to buffer the input  
 * Program terminates if user enters an empty line
 */

public class IOconsole1
{
    public static void main(String[] args)
    {
        System.out.println("Read buffered 8bit array, convert to string, & write");

        byte[ ] array    = new byte[250];                   //create a byte array
        byte newlineUnix = '\n';                            //value 13
        byte newlinePC   = '\r';                            //value 10

        try 
        {
            BufferedInputStream keyboardBuf = new BufferedInputStream(System.in);

            while(true)
            {
                System.out.print("Enter something: ");

                int numBytes = keyboardBuf.read(array);                 //read from console
                                                                        //return number of bytes read
                byte firstByte = array[0];

                if (firstByte==newlineUnix || firstByte==newlinePC)     //carriage return or line feed
                    System.exit(0);

                String input = new String(array, 0, numBytes);  //create a new String from an array
                                                                //with offset 0, for number of bytes
                System.out.print("You've entered.: "+ input);
            }
        }
        catch (IOException e)
        {
            System.out.println(e);
        }
    }
}