RLPark 1.0.0
Reinforcement Learning Framework in Java

SerialStreams.java

Go to the documentation of this file.
00001 package rlpark.plugin.irobot.internal.serial;
00002 
00003 import gnu.io.SerialPort;
00004 
00005 import java.io.IOException;
00006 import java.io.InputStream;
00007 import java.io.OutputStream;
00008 import java.util.Arrays;
00009 
00010 public class SerialStreams {
00011   private final InputStream input;
00012   private final OutputStream output;
00013 
00014   protected SerialStreams(SerialPort serialPort) throws IOException {
00015     input = serialPort.getInputStream();
00016     output = serialPort.getOutputStream();
00017   }
00018 
00019   synchronized public void write(int c) throws IOException {
00020     output.write(c);
00021   }
00022 
00023   synchronized public void write(byte[] c) throws IOException {
00024     output.write(c);
00025   }
00026 
00027   synchronized public int available() throws IOException {
00028     return input.available();
00029   }
00030 
00031   synchronized public int read() throws IOException {
00032     return input.read();
00033   }
00034 
00035   synchronized public byte[] read(int size) throws IOException {
00036     byte[] buffer = new byte[size];
00037     int alreadyRead = 0;
00038     while (alreadyRead < size) {
00039       int justRead = input.read(buffer, alreadyRead, size - alreadyRead);
00040       if (justRead == 0)
00041         return Arrays.copyOf(buffer, alreadyRead);
00042       alreadyRead += justRead;
00043     }
00044     return buffer;
00045   }
00046 
00047   synchronized public void close() {
00048     try {
00049       input.close();
00050     } catch (IOException e) {
00051       e.printStackTrace();
00052     }
00053     try {
00054       output.close();
00055     } catch (IOException e) {
00056       e.printStackTrace();
00057     }
00058   }
00059 }
 All Classes Namespaces Files Functions Variables Enumerations
Zephyr
RLPark