BowlerKernel
DyIOInputStream.java
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright 2010 Neuron Robotics, LLC
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  ******************************************************************************/
15 package com.neuronrobotics.sdk.dyio;
16 
17 import java.io.IOException;
18 import java.io.InputStream;
19 
20 import com.neuronrobotics.sdk.common.ByteList;
21 
22 // TODO: Auto-generated Javadoc
26 public class DyIOInputStream extends InputStream {
27 
29  private ByteList buffer = new ByteList();
30 
32  DyIOChannel chan;
33 
39  public DyIOInputStream(DyIOChannel channel) {
40  chan = channel;
41  }
42 
48  public void write(ByteList data) {
49  synchronized (buffer) {
50  buffer.add(data);
51  }
52  }
53 
54  /* (non-Javadoc)
55  * @see java.io.InputStream#read()
56  */
57  @Override
58  public int read() throws IOException {
59  while(chan.getMode() == DyIOChannelMode.USART_RX) {
60  if(available() > 0) {
61  // cast the poped byte as an int to make sure its positive
62  Byte val = buffer.pop();
63 
64  if(val == null) {
65  continue;
66  }
67 
68  return (int) val;
69  }
70  }
71 
72  return -1;
73  }
74 
75  /* (non-Javadoc)
76  * @see java.io.InputStream#available()
77  */
78  @Override
79  public int available() {
80  return buffer.size();
81  }
82 }
synchronized boolean add(byte data)
Definition: ByteList.java:149
DyIOChannelMode getMode(boolean resync)