All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lejos.remote.nxt.NXTOutputStream Maven / Gradle / Ivy

Go to download

leJOS (pronounced like the Spanish word "lejos" for "far") is a tiny Java Virtual Machine. In 2013 it was ported to the LEGO EV3 brick.

The newest version!
package lejos.remote.nxt;

import java.io.IOException;
import java.io.OutputStream;

/**
 * Implements an OutputStream over NXT connections.
 *
 */
public class NXTOutputStream extends OutputStream {
	private byte[] buffer;
	private int numBytes = 0;
	private NXTConnection conn = null;
	
	NXTOutputStream(NXTConnection conn, int buffSize)
	{
		this.conn = conn;
        buffer = new byte[buffSize];
	}
	
    @Override
	public synchronized void write(int b) throws IOException {
    	if (numBytes >= buffer.length) {
    		flush();
    	}
    	buffer[numBytes] = (byte) b;
    	numBytes++;  	
    }
    
    //TODO implement write(byte[], int, int)
    
    @Override
	public synchronized void flush() throws IOException{
		if (numBytes > 0) {
			if (conn.write(buffer, numBytes) < 0) throw new IOException();
			numBytes = 0;
		}
	}
    
    @Override
    public void close() throws IOException {
    	this.flush();
    	//TODO mark stream closed
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy