Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2010 SAP AG All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: Lazar Kirchev, SAP AG - initial API and implementation /
 
 
 package org.eclipse.equinox.console.common;
 
This class wraps the actual output stream (e.g., a socket output stream) and is responsible for buffering and flushing the characters to the actual output stream.
 
 public class ConsoleOutputStream extends OutputStream {

    
A size of the used buffer.
 
     public static final int BUFFER_SIZE = 2048;
     public final static byte CR = (byte'\r';
     public final static byte LF = (byte'\n';
 
     OutputStream out;
 
     private boolean isEcho = true;
     private boolean queueing = false;
     private byte prevByte;
     private byte[] buffer;
     private int pos;

    
Initiates with instance of the output stream to which it will send data. Here it writes to a socket output stream.

Parameters:
out OutputStream for console output
 
     public ConsoleOutputStream(OutputStream out) {
         this. = out;
          = new byte[];
          = 0;
     }

    
An implementation of the corresponding abstract method in OutputStream.
 
     public synchronized void write(int ithrows IOException {
 
         if (!) {
             if () {
                 if (i == '\r' || i == '\0') {
                      = true;
                      = (bytei;
                 } else if (i == '\n') {
                     add();
                     add();
                 } else {
                     add(i);
                 }
             }
         } else { // awaiting '\n' AFTER '\r', and '\b' AFTER '\0'
             if ( == '\0' && i == '\b') {
                  = !;
             } else if () {
                 if ( == '\r' && i == '\n') {
                     add();
                     add();
                 } else {
                     add();
                     add();
                     add(i);
                 }
             }
 
              = false;
             flush();
         }
 
     }

    
Empties the buffer and sends data to the socket output stream.

 
     public synchronized void flush() throws IOException {
         if ( > 0) {
             try {
 				.write(, 0, );
 				.flush();
 			} finally {
 				 = 0;
			}
            
        }
    }

    
Adds a variable of type integer to the buffer.

Parameters:
i integer to add
Throws:
java.io.IOException if there are problems adding the integer
    private void add(int ithrows IOException {
        [] = (bytei;
        ++;
        if ( == .) {
            flush();
        }
    }

    
Closes this OutputStream.

    public void close() throws IOException {
        .close();
    }

    
Substitutes the output stream. The old one is stored so that it can be restored later.

Parameters:
newOut new output stream to use.
    public void setOutput(OutputStream newOut) {
        if (newOut != null) {
             = ;
             = newOut;
        } else {
             = ;
        }
    }
New to GrepCode? Check out our FAQ X