Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package org.ros.android.android_acm_serial;
00018
00019 import com.google.common.base.Preconditions;
00020
00021 import android.hardware.usb.UsbConstants;
00022 import android.hardware.usb.UsbEndpoint;
00023 import android.hardware.usb.UsbRequest;
00024 import android.util.Log;
00025
00026 import java.io.IOException;
00027 import java.io.OutputStream;
00028 import java.nio.ByteBuffer;
00029
00030 public class AcmOutputStream extends OutputStream {
00031
00032 private static final boolean DEBUG = false;
00033 private static final String TAG = "AcmOutputStream";
00034
00035 private final UsbRequestPool usbRequestPool;
00036 private final UsbEndpoint endpoint;
00037
00038 public AcmOutputStream(UsbRequestPool usbRequestPool, UsbEndpoint endpoint) {
00039 Preconditions.checkArgument(endpoint.getDirection() == UsbConstants.USB_DIR_OUT);
00040 this.endpoint = endpoint;
00041 this.usbRequestPool = usbRequestPool;
00042 }
00043
00044 @Override
00045 public void close() throws IOException {
00046 usbRequestPool.shutdown();
00047 }
00048
00049 @Override
00050 public void flush() throws IOException {
00051 }
00052
00053 @Override
00054 public void write(byte[] buffer, int offset, int count) {
00055 Preconditions.checkNotNull(buffer);
00056 if (offset < 0 || count < 0 || offset + count > buffer.length) {
00057 throw new IndexOutOfBoundsException();
00058 }
00059 if (DEBUG) {
00060 Log.i(TAG, "Writing " + count + " bytes from offset " + offset + ".");
00061 }
00062 UsbRequest request = usbRequestPool.poll(endpoint);
00063 if (!request.queue(ByteBuffer.wrap(buffer, offset, count), count)) {
00064 Log.e(TAG, "IO error while queuing " + count + " bytes to be written.");
00065 }
00066 }
00067
00068 @Override
00069 public void write(int oneByte) throws IOException {
00070 write(new byte[] { (byte) oneByte }, 0, 1);
00071 }
00072 }