Skip to content

Commit

Permalink
Implements Input/OutputStream helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Cerruti committed Jan 16, 2014
1 parent 25693d1 commit 913d051
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.hoho.android.usbserial.stream;

import com.hoho.android.usbserial.driver.UsbSerialDriver;
import java.io.IOException;
import java.io.InputStream;

/**
* @author lucas@creativa77.com.ar (Lucas Chiesa)
*/
class UsbSerialInputStream extends InputStream{

private final UsbSerialDriver device;
private final UsbSerialStreamFactory factory;

UsbSerialInputStream(UsbSerialDriver device, UsbSerialStreamFactory factory) {
this.device = device;
this.factory = factory;
}

@Override
public void close() throws IOException {
device.close();
}

@Override
public int read() throws IOException {
throw new UnsupportedOperationException();
}

@Override
public int read(byte[] buffer) throws IOException {
return read(buffer, 0, buffer.length);
}

@Override
public int read(byte[] buffer, int offset, int count) throws IOException {
byte[] local_buffer = new byte[count];
if (offset < 0 || count < 0 || offset + count > buffer.length) {
throw new IndexOutOfBoundsException();
}
int byteCount;
byteCount = device.read(local_buffer, count);
System.arraycopy(local_buffer, 0, buffer, offset, byteCount);
return byteCount;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.hoho.android.usbserial.stream;

import com.hoho.android.usbserial.driver.UsbSerialDriver;

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

/**
* @author lucas@creativa77.com.ar (Lucas Chiesa)
*/
class UsbSerialOutputStream extends OutputStream {

private final UsbSerialDriver driver;
private final UsbSerialStreamFactory factory;

UsbSerialOutputStream(UsbSerialDriver driver, UsbSerialStreamFactory factory) {
this.driver = driver;
this.factory = factory;
}

@Override
public void close() throws IOException {
driver.close();
}

@Override
public void flush() throws IOException {
}

@Override
public void write(byte[] buffer, int offset, int count) throws java.io.IOException {
if (offset < 0 || count < 0 || offset + count > buffer.length) {
throw new IndexOutOfBoundsException();
}

byte[] local = new byte[count];
System.arraycopy(buffer, offset, local, 0, count);
this.driver.write(local, 1000);
}

@Override
public void write(int oneByte) throws IOException {
write(new byte[] { (byte) oneByte }, 0, 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.hoho.android.usbserial.stream;

import com.hoho.android.usbserial.driver.UsbSerialDriver;

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

/**
* This helper class encapsulates a given <code>UsbSerialDriver</code> with implementations of
* <code>java.io.InputStream</code> and <code>java.io.OutputStream</code>.
*
* @author jcerruti@creativa77.com.ar (Julian Cerruti)
*/
public class UsbSerialStreamFactory {
private final UsbSerialDriver driver;

private final int baudRate;
private final int dataBits;
private final int stopBits;
private final int parity;

public UsbSerialStreamFactory(UsbSerialDriver driver, int baudRate, int dataBits, int stopBits, int parity) throws IOException {
this.driver = driver;
this.baudRate = baudRate;
this.dataBits = dataBits;
this.stopBits = stopBits;
this.parity = parity;

driver.open();
driver.setParameters(baudRate, dataBits, stopBits, parity);
}

/**
* Uses the following default values to open the underlying serial connection:
* - baud rante: 115200
* - data bits: 8
* - stop bits: 1
* - parity: none
*/
public UsbSerialStreamFactory(UsbSerialDriver driver) throws IOException {
this(driver, 115200, UsbSerialDriver.DATABITS_8, UsbSerialDriver.STOPBITS_1, UsbSerialDriver.PARITY_NONE);
}

/**
* Closes the underlying driver's serial connection
*/
public void close() throws IOException {
driver.close();
}

public InputStream getInputStream() {
return new UsbSerialInputStream(this.driver, this);
}
public OutputStream getOutputStream() {
return new UsbSerialOutputStream(this.driver, this);
}

public int getStopBits() {
return stopBits;
}

public int getParity() {
return parity;
}

public int getDataBits() {
return dataBits;
}

public int getBaudRate() {
return baudRate;
}
}

0 comments on commit 913d051

Please sign in to comment.