AcmOutputStream.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
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 }


android_core
Author(s): Damon Kohler
autogenerated on Thu Jun 6 2019 21:20:07