lejos.remote.nxt.SocketConnection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lejos-ev3-api Show documentation
Show all versions of lejos-ev3-api Show documentation
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.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class SocketConnection extends NXTConnection {
private Socket socket;
private InputStream is;
private OutputStream os;
public SocketConnection(Socket socket) {
this.socket = socket;
try {
is = socket.getInputStream();
os = socket.getOutputStream();
} catch (IOException e) {
// Ignore for the moment
}
}
@Override
public void close() throws IOException {
socket.close();
}
@Override
public int read(byte[] buf, int length) {
try {
return is.read(buf, 0, length);
} catch (IOException e) {
return -2;
}
}
@Override
public int write(byte[] buffer, int numBytes) {
try {
os.write(buffer, 0, numBytes);
return 0;
} catch (IOException e) {
return -2;
}
}
@Override
public int read(byte[] buf, int length, boolean b) {
return read(buf,length);
}
}