How do you merge two input streams in Java? - java

Having two InputStreams in Java, is there a way to merge them so you end with one InputStream that gives you the output of both streams? How?

As commented, it's not clear what you mean by merge.
Taking available input "randomly" from either is complicated by InputStream.available not necessarily giving you a useful answer and blocking behaviour of streams. You would need two threads to be reading from the streams and then passing back data through, say, java.io.Piped(In|Out)putStream (although those classes have issues). Alternatively for some types of stream it may be possible to use a different interface, for instance java.nio non-blocking channels.
If you want the full contents of the first input stream followed by the second: new java.io.SequenceInputStream(s1, s2).

java.io.SequenceInputStream might be what you need. It accepts an enumeration of streams, and will output the contents of the first stream, then the second, and so on until all streams are empty.

You can write a custom InputStream implementation that does this. Example:
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Deque;
import java.util.LinkedList;
public class CatInputStream extends InputStream {
private final Deque<InputStream> streams;
public CatInputStream(InputStream... streams) {
this.streams = new LinkedList<InputStream>();
Collections.addAll(this.streams, streams);
}
private void nextStream() throws IOException {
streams.removeFirst().close();
}
#Override
public int read() throws IOException {
int result = -1;
while (!streams.isEmpty()
&& (result = streams.getFirst().read()) == -1) {
nextStream();
}
return result;
}
#Override
public int read(byte b[], int off, int len) throws IOException {
int result = -1;
while (!streams.isEmpty()
&& (result = streams.getFirst().read(b, off, len)) == -1) {
nextStream();
}
return result;
}
#Override
public long skip(long n) throws IOException {
long skipped = 0L;
while (skipped < n && !streams.isEmpty()) {
int thisSkip = streams.getFirst().skip(n - skipped);
if (thisSkip > 0)
skipped += thisSkip;
else
nextStream();
}
return skipped;
}
#Override
public int available() throws IOException {
return streams.isEmpty() ? 0 : streams.getFirst().available();
}
#Override
public void close() throws IOException {
while (!streams.isEmpty())
nextStream();
}
}
This code isn't tested, so your mileage may vary.

Not that I can think of. You would probably have to read the contents of the two stream into a byte[] and then create a ByteArrayInputStream from that.

Here is an MVar implementation specific to byte arrays (make sure to add your own package definition). From here, it is trivial to write an input stream on merged streams. I can post that too if requested.
import java.nio.ByteBuffer;
public final class MVar {
private static enum State {
EMPTY, ONE, MANY
}
private final Object lock;
private State state;
private byte b;
private ByteBuffer bytes;
private int length;
public MVar() {
lock = new Object();
state = State.EMPTY;
}
public final void put(byte b) {
synchronized (lock) {
while (state != State.EMPTY) {
try {
lock.wait();
} catch (InterruptedException e) {}
}
this.b = b;
state = State.ONE;
lock.notifyAll();
}
}
public final void put(byte[] bytes, int offset, int length) {
if (length == 0) {
return;
}
synchronized (lock) {
while (state != State.EMPTY) {
try {
lock.wait();
} catch (InterruptedException e) {}
}
this.bytes = ByteBuffer.allocateDirect(length);
this.bytes.put(bytes, offset, length);
this.bytes.position(0);
this.length = length;
state = State.MANY;
lock.notifyAll();
}
}
public final byte take() {
synchronized (lock) {
while (state == State.EMPTY) {
try {
lock.wait();
} catch (InterruptedException e) {}
}
switch (state) {
case ONE: {
state = State.EMPTY;
byte b = this.b;
lock.notifyAll();
return b;
}
case MANY: {
byte b = bytes.get();
state = --length <= 0 ? State.EMPTY : State.MANY;
lock.notifyAll();
return b;
}
default:
throw new AssertionError();
}
}
}
public final int take(byte[] bytes, int offset, int length) {
if (length == 0) {
return 0;
}
synchronized (lock) {
while (state == State.EMPTY) {
try {
lock.wait();
} catch (InterruptedException e) {}
}
switch (state) {
case ONE:
bytes[offset] = b;
state = State.EMPTY;
lock.notifyAll();
return 1;
case MANY:
if (this.length > length) {
this.bytes.get(bytes, offset, length);
this.length = this.length - length;
synchronized (lock) {
lock.notifyAll();
}
return length;
}
this.bytes.get(bytes, offset, this.length);
this.bytes = null;
state = State.EMPTY;
length = this.length;
lock.notifyAll();
return length;
default:
throw new AssertionError();
}
}
}
}

Related

jSSC Not Sending Data to Arduino

I have a problem with writing function of jSSC. My Arduino Uno board seems not getting data from my Java program.
I have a stepper motor controlled by Arduino Uno board. I made a simple program that has 2 buttons. One is for CW rotation and the other is CCW rotation. CW button sends 'H' char and CCW button sends 'L' char. Now I have:
I checked from Arduino IDE serial console my Arduino program works correct. When I send 'H' the motor turns CW and with 'L' the motor turns CCW.
I made a program in Processing with two buttons sending 'H' and 'L'. It worked.
I made a Java program with JSSC with two buttons sending 'H' and 'L'. IT FAILED.
When I push one of the buttons in my program I see "L" light on the board blinks 3-4 times but nothing happens.
I tried getting data from my board with JSSC and it worked. It seems the problem is in writing function.
I checked with another Arduino Uno board but the result is the same.
My Java program uses serialPort.writeByte((byte)'H'); and serialPort.writeByte((byte)'L');
Any ideas?
Did you try setting the parameters for flow control. Since while writing to the interface, it requires a permission.
serialPort.setParams(SerialPort.BAUDRATE_9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE,false,true);//Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0,RTSEnable,DTSEnable);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
Can it be a reset-related problem? Maybe your java function uses the DTR pin, which is connected to the RESET pin; so when you try to send data instead you are just resetting the board.
If you want to test you can make another led blink at startup or send something through the serial interface at setup. If you get that feedback try looking at the way to disable DTR ;)
You can refactor Processing's Serial class (which works pretty well) to avoid PApplet if you don't plan to use it in your java project:
/*
PSerial - class for serial port goodness
Part of the Processing project - http://processing.org
Copyright (c) 2004-05 Ben Fry & Casey Reas
Reworked by Gottfried Haider as part of GSOC 2013
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
import java.lang.reflect.Method;
import java.util.Map;
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
import jssc.SerialPortList;
public class Serial implements SerialPortEventListener {
Object parent;
public SerialPort port;
Method serialAvailableMethod;
Method serialEventMethod;
byte[] buffer = new byte[32768];
int inBuffer = 0;
int readOffset = 0;
int bufferUntilSize = 1;
byte bufferUntilByte = 0;
volatile boolean invokeSerialAvailable = false;
// Things we are currently not exposing:
// * hardware flow control
// * state of the RING, RLSD line
// * sending breaks
public Serial(Object parent) {
this(parent, "COM1", 9600, 'N', 8, 1);
}
public Serial(Object parent, int baudRate) {
this(parent, "COM1", baudRate, 'N', 8, 1);
}
public Serial(Object parent, String portName) {
this(parent, portName, 9600, 'N', 8, 1);
}
public Serial(Object parent, String portName, int baudRate) {
this(parent, portName, baudRate, 'N', 8, 1);
}
public Serial(Object parent, String portName, int baudRate, char parity, int dataBits, float stopBits) {
this.parent = parent;
//parent.registerMethod("dispose", this);
//parent.registerMethod("pre", this);
// setup parity
if (parity == 'O') {
parity = SerialPort.PARITY_ODD;
} else if (parity == 'E') {
parity = SerialPort.PARITY_EVEN;
} else if (parity == 'M') {
parity = SerialPort.PARITY_MARK;
} else if (parity == 'S') {
parity = SerialPort.PARITY_SPACE;
} else {
parity = SerialPort.PARITY_NONE;
}
// setup stop bits
int stopBitsIdx = SerialPort.STOPBITS_1;
if (stopBits == 1.5f) {
stopBitsIdx = SerialPort.STOPBITS_1_5;
} else if (stopBits == 2) {
stopBitsIdx = SerialPort.STOPBITS_2;
}
port = new SerialPort(portName);
try {
// the native open() call is not using O_NONBLOCK, so this might block for certain operations (see write())
port.openPort();
port.setParams(baudRate, dataBits, stopBitsIdx, parity);
// we could register more events here
port.addEventListener(this, SerialPort.MASK_RXCHAR);
} catch (SerialPortException e) {
// this used to be a RuntimeException before, so stick with it
throw new RuntimeException("Error opening serial port " + e.getPortName() + ": " + e.getExceptionType());
}
serialEventMethod = findCallback("serialEvent");
serialAvailableMethod = findCallback("serialAvailable");
}
private Method findCallback(final String name) {
try {
return parent.getClass().getMethod(name, this.getClass());
} catch (Exception e) {
}
// Permit callback(Object) as alternative to callback(Serial).
try {
return parent.getClass().getMethod(name, Object.class);
} catch (Exception e) {
}
return null;
}
public void dispose() {
stop();
}
public void pre() {
if (serialAvailableMethod != null && invokeSerialAvailable) {
invokeSerialAvailable = false;
try {
serialAvailableMethod.invoke(parent, this);
} catch (Exception e) {
System.err.println("Error, disabling serialAvailable() for "+port.getPortName());
System.err.println(e.getLocalizedMessage());
serialAvailableMethod = null;
}
}
}
public int available() {
return (inBuffer-readOffset);
}
public void buffer(int size) {
bufferUntilSize = size;
}
public void bufferUntil(int inByte) {
bufferUntilSize = 0;
bufferUntilByte = (byte)inByte;
}
public void clear() {
synchronized (buffer) {
inBuffer = 0;
readOffset = 0;
}
}
public boolean getCTS() {
try {
return port.isCTS();
} catch (SerialPortException e) {
throw new RuntimeException("Error reading the CTS line: " + e.getExceptionType());
}
}
public boolean getDSR() {
try {
return port.isDSR();
} catch (SerialPortException e) {
throw new RuntimeException("Error reading the DSR line: " + e.getExceptionType());
}
}
public static Map<String, String> getProperties(String portName) {
return SerialPortList.getPortProperties(portName);
}
public int last() {
if (inBuffer == readOffset) {
return -1;
}
synchronized (buffer) {
int ret = buffer[inBuffer-1] & 0xFF;
inBuffer = 0;
readOffset = 0;
return ret;
}
}
public char lastChar() {
return (char)last();
}
public static String[] list() {
// returns list sorted alphabetically, thus cu.* comes before tty.*
// this was different with RXTX
return SerialPortList.getPortNames();
}
public int read() {
if (inBuffer == readOffset) {
return -1;
}
synchronized (buffer) {
int ret = buffer[readOffset++] & 0xFF;
if (inBuffer == readOffset) {
inBuffer = 0;
readOffset = 0;
}
return ret;
}
}
public byte[] readBytes() {
if (inBuffer == readOffset) {
return null;
}
synchronized (buffer) {
byte[] ret = new byte[inBuffer-readOffset];
System.arraycopy(buffer, readOffset, ret, 0, ret.length);
inBuffer = 0;
readOffset = 0;
return ret;
}
}
public int readBytes(byte[] dest) {
if (inBuffer == readOffset) {
return 0;
}
synchronized (buffer) {
int toCopy = inBuffer-readOffset;
if (dest.length < toCopy) {
toCopy = dest.length;
}
System.arraycopy(buffer, readOffset, dest, 0, toCopy);
readOffset += toCopy;
if (inBuffer == readOffset) {
inBuffer = 0;
readOffset = 0;
}
return toCopy;
}
}
public byte[] readBytesUntil(int inByte) {
if (inBuffer == readOffset) {
return null;
}
synchronized (buffer) {
// look for needle in buffer
int found = -1;
for (int i=readOffset; i < inBuffer; i++) {
if (buffer[i] == (byte)inByte) {
found = i;
break;
}
}
if (found == -1) {
return null;
}
int toCopy = found-readOffset+1;
byte[] dest = new byte[toCopy];
System.arraycopy(buffer, readOffset, dest, 0, toCopy);
readOffset += toCopy;
if (inBuffer == readOffset) {
inBuffer = 0;
readOffset = 0;
}
return dest;
}
}
public int readBytesUntil(int inByte, byte[] dest) {
if (inBuffer == readOffset) {
return 0;
}
synchronized (buffer) {
// look for needle in buffer
int found = -1;
for (int i=readOffset; i < inBuffer; i++) {
if (buffer[i] == (byte)inByte) {
found = i;
break;
}
}
if (found == -1) {
return 0;
}
// check if bytes to copy fit in dest
int toCopy = found-readOffset+1;
if (dest.length < toCopy) {
System.err.println( "The buffer passed to readBytesUntil() is to small " +
"to contain " + toCopy + " bytes up to and including " +
"char " + (byte)inByte);
return -1;
}
System.arraycopy(buffer, readOffset, dest, 0, toCopy);
readOffset += toCopy;
if (inBuffer == readOffset) {
inBuffer = 0;
readOffset = 0;
}
return toCopy;
}
}
public char readChar() {
return (char) read();
}
public String readString() {
if (inBuffer == readOffset) {
return null;
}
return new String(readBytes());
}
public String readStringUntil(int inByte) {
byte temp[] = readBytesUntil(inByte);
if (temp == null) {
return null;
} else {
return new String(temp);
}
}
public void serialEvent(SerialPortEvent event) {
if (event.getEventType() == SerialPortEvent.RXCHAR) {
int toRead;
try {
while (0 < (toRead = port.getInputBufferBytesCount())) {
// this method can be called from the context of another thread
synchronized (buffer) {
// read one byte at a time if the sketch is using serialEvent
if (serialEventMethod != null) {
toRead = 1;
}
// enlarge buffer if necessary
if (buffer.length < inBuffer+toRead) {
byte temp[] = new byte[buffer.length<<1];
System.arraycopy(buffer, 0, temp, 0, inBuffer);
buffer = temp;
}
// read an array of bytes and copy it into our buffer
byte[] read = port.readBytes(toRead);
System.arraycopy(read, 0, buffer, inBuffer, read.length);
inBuffer += read.length;
}
if (serialEventMethod != null) {
if ((0 < bufferUntilSize && bufferUntilSize <= inBuffer-readOffset) ||
(0 == bufferUntilSize && bufferUntilByte == buffer[inBuffer-1])) {
try {
// serialEvent() is invoked in the context of the current (serial) thread
// which means that serialization and atomic variables need to be used to
// guarantee reliable operation (and better not draw() etc..)
// serialAvailable() does not provide any real benefits over using
// available() and read() inside draw - but this function has no
// thread-safety issues since it's being invoked during pre in the context
// of the Processing applet
serialEventMethod.invoke(parent, this);
} catch (Exception e) {
System.err.println("Error, disabling serialEvent() for "+port.getPortName());
System.err.println(e.getLocalizedMessage());
serialEventMethod = null;
}
}
}
invokeSerialAvailable = true;
}
} catch (SerialPortException e) {
throw new RuntimeException("Error reading from serial port " + e.getPortName() + ": " + e.getExceptionType());
}
}
}
public void setDTR(boolean state) {
// there is no way to influence the behavior of the DTR line when opening the serial port
// this means that at least on Linux and OS X, Arduino devices are always reset
try {
port.setDTR(state);
} catch (SerialPortException e) {
throw new RuntimeException("Error setting the DTR line: " + e.getExceptionType());
}
}
public void setRTS(boolean state) {
try {
port.setRTS(state);
} catch (SerialPortException e) {
throw new RuntimeException("Error setting the RTS line: " + e.getExceptionType());
}
}
public void stop() {
try {
port.closePort();
} catch (SerialPortException e) {
// ignored
}
inBuffer = 0;
readOffset = 0;
}
public void write(byte[] src) {
try {
// this might block if the serial device is not yet ready (esp. tty devices under OS X)
port.writeBytes(src);
// we used to call flush() here
} catch (SerialPortException e) {
throw new RuntimeException("Error writing to serial port " + e.getPortName() + ": " + e.getExceptionType());
}
}
public void write(int src) {
try {
port.writeInt(src);
} catch (SerialPortException e) {
throw new RuntimeException("Error writing to serial port " + e.getPortName() + ": " + e.getExceptionType());
}
}
public void write(String src) {
try {
port.writeString(src);
} catch (SerialPortException e) {
throw new RuntimeException("Error writing to serial port " + e.getPortName() + ": " + e.getExceptionType());
}
}
}
Here's an example class using the above:
public class SerialTest {
public static void main(String[] args) {
try{
Serial serial = new Serial(new SerialTest(),"/dev/tty.usbmodemfd121",115200);
serial.write("H");
serial.write("L");//can also try serial.write((int)'L');
}catch(Exception e){
System.err.println("serial not connected!");
}
}
public void serialAvailable(Serial s){
System.out.println(s.toString());
}
public void serialEvent(Serial s){
System.out.print("from serial:");
System.out.println(s.read());
}
}
Be sure to change the port and baud rate to what your Arduino Uno is using.
In Processing's serial library folder you'll also find the JNI native libraries.
For example on Windows:
C:\Program Files\processing-2.1.2\modes\java\libraries\serial\library
Where the dll would reside in the windows32 and windows64 folders, depending on what you plan to use.
The Serial class above is pretty much the same as C:\Program Files\processing-2.1.2\modes\java\libraries\serial\src\processing\serial/Serial.java replacing PApplet with Object.
Also, here is the Processing Serial library reference

What is the best way to convert an Input Stream to an Array in Java?

I'm writing code that will accept byte values from an arduino, store them as an array, preform some mathematical calculations, and then send the values back to the arduino. Right now I can send 127 values to the Arduino and I get 127 values back, but they are of type string, and any attempts to use the Integer class to convert these strings results in a program hang. I believe the buffer sometimes provides empty strings, and parseInt() doesn't know what to do. Does anyone have any suggestions? I'm very much a beginner in java and would be open to better solutions.
Here is my code:
package GridMap;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
*
*/
public class SerialWriter implements Runnable {
OutputStream out;
byte array[] = new byte[10];
byte c;
public SerialWriter(OutputStream out, byte[] in) {
this.out = out;
array = in;
}
public void run() {
try {
int index = 0;
c = array[index];
while ((c) > -1) {
this.out.write(c);
System.out.println("sent " + c);
if (index == 64){
Thread.sleep(2);
}
index++;
c = array[index];
}
TwoWaySerialComm.recieve();
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
}
public class SerialReader implements Runnable {
static byte[] output = new byte[128];
private InputStream in;
private int[] buffer = new int[11];
static SerialPort thisSerialPort;
static OutputStream thisOut;
static String total = new String("333");
public SerialReader(InputStream in) {
this.in = in;
for (byte i = 0; i < 127; i++) {
output[i] = i;
}
output[127] = - 1;
}
public void run ()
{
byte[] buffer = new byte[1024];
int len = -1;
int index = 0;
int value;
try
{
Thread.sleep(200);
while (( len = this.in.read(buffer)) > -1 && index < 200)
{
String string = new String(buffer, 0, len);
//value = Integer.getInteger(string, len);
// System.out.print(value);
//System.out.println("buffer" + value);
System.out.print(string);
index++;
}
TwoWaySerialComm.send(output);
}
catch (Exception e )
{
e.printStackTrace();
}
}
public static int byteArrayToInt(byte[] b)
{
return b[3] & 0xFF |
(b[2] & 0xFF) << 8 |
(b[1] & 0xFF) << 16 |
(b[0] & 0xFF) << 24;
}
}
public class TwoWaySerialComm {
static SerialPort serialPort;
static OutputStream out = null;
static InputStream in;
static Thread receiveThread;
static Thread sendThread;
static byte[] output = new byte[11];
public TwoWaySerialComm() {
super();
}
void connect(String portName) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if (portIdentifier.isCurrentlyOwned()) {
System.out.println("Error: Port is currently in use");
} else {
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
if (commPort instanceof SerialPort) {
serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(114400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} else {
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}
static void send(byte[] output) {
try {
out = serialPort.getOutputStream();
sendThread = new Thread(new SerialWriter(out, output));
sendThread.start();
//sendThread.join();
} catch (Exception e) {
System.out.println("Port Not Avaialable (send) ");
}
}
static void recieve(){
try {
in = serialPort.getInputStream();
receiveThread = new Thread(new SerialReader(in));
receiveThread.start();
receiveThread.join();
} catch (Exception e) {
}
}
public static void main(String[] args) {
try {
(new TwoWaySerialComm()).connect("COM3");
for (byte i = 0; i < 10; i++) {
output[i] = i;
}
output[10] = -1;
send(output);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static SerialPort returnSerialPort(){
return serialPort;
}
}
If you want get int from your stream, it is easier with a BuffereInputStream and use the read() method which return a int -> no conversion needed.
Add this in your SerialReader class :
Thread.sleep(200);
BufferedInputStream input = new BufferedInputStream(in);
while ((value = input.read()) != 1 && index < 200)
{
compute(value);
index++;
}
input.close();
Don't forget to close() your stream when you have read all the data. This is more important when you write, because if you don't close() not all data are written (except if you flush() before).
I did not quite get the description.
But the answer to the Title on the question is here.
Convert InputStream to byte array in Java
From corsiKa's answer to Determine if a String is an Integer in Java, you can check if a String is a valid int like this:
public static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch(NumberFormatException e) {
return false;
}
// only got here if we didn't return false
return true;
}

Has anyone played with NIO pipes to filter / intercept System.out?

As suggested here I would like to do that inside the selector loop. What I would really want is to read contents written to system out inside my selector loop.
EDIT1: I coded a complete solution just to find out that you CANNOT redirect GC logs by using System.setOut. It simply goes straight to the FD or something. Show stopper! Unless I redirect to a file and pipe this file into my selector. Lots of work! See here.
One way to do it would be as follows:
create a subclass of OutputStream that redirects its output to a Pipe's sink channel
redirect System.out using this class: System.setOut(new PrintStream(new MyOutputStream(pipe));
register the pipe's source channel with a selector and get whatever was written to System.out in the selector loop, i.e. the source channel's correpsonding SelectionKey is selected as readable()
The following immplementation is a naive but working implementation, which simply redirects to System.err everything that is written to System.out:
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
public class SystemOutPipe extends Thread {
public static void main(String[] args)
{
try {
SystemOutPipe sop = new SystemOutPipe();
sop.start();
System.out.println("This message should be redirected to System.err\nNow waiting 5 seconds ...");
Thread.sleep(5000L);
sop.setStopped(true);
sop.join();
} catch (Exception e) {
e.printStackTrace();
}
}
private Selector selector;
private Pipe pipe;
private boolean stopped = false;
public SystemOutPipe() throws IOException {
super("SystemOutPipe");
pipe = Pipe.open();
System.setOut(new PrintStream(new PipeOutputStream(pipe)));
selector = Selector.open();
pipe.source().configureBlocking(false);
pipe.source().register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(1024));
}
#Override
public void run() {
try {
while (!isStopped()) {
int n = selector.select(1L);
if (n > 0) {
Iterator<SelectionKey> it = selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey key = it.next();
it.remove();
if (key.isReadable()) {
new ReadHandler(key).run();
}
}
}
}
} catch (Exception e) {
e.printStackTrace(); // writes to System.err !
}
}
public synchronized boolean isStopped() {
return stopped;
}
public synchronized void setStopped(final boolean stopped) {
this.stopped = stopped;
}
public class ReadHandler implements Runnable {
private final SelectionKey key;
public ReadHandler(final SelectionKey key) {
this.key = key;
}
#Override
public void run() {
ByteBuffer bbuf = (ByteBuffer) key.attachment();
ReadableByteChannel channel = (ReadableByteChannel) key.channel();
try
{
int count = 0;
do {
bbuf.clear();
count = channel.read(bbuf);
if (count > 0) System.err.write(bbuf.array(), 0, count);
} while(count > 0);
} catch (IOException e) {
e.printStackTrace();
key.cancel();
}
}
}
public class PipeOutputStream extends OutputStream {
private final Pipe pipe;
public PipeOutputStream(final Pipe pipe) {
this.pipe = pipe;
}
#Override
public void write(final int b) throws IOException {
write(new byte[] { (byte) b });
}
#Override
public void write(final byte[] b) throws IOException {
write(b, 0, b.length);
}
#Override
public void write(final byte[] b, final int off, final int len) throws IOException {
ByteBuffer bbuf = ByteBuffer.wrap(b, off, len);
bbuf.position(len);
bbuf.flip();
int count = 0;
while (count < len) {
int n = pipe.sink().write(bbuf);
if (n == 0) {
// let's wait a bit and not consume cpu
try {
Thread.sleep(1L);
} catch (InterruptedException e) {
throw new IOException(e);
}
}
else count += n;
}
}
}
}

Is this LimitedInputStream correct?

I've written a class called LimitedInputStream. It wraps around an existing input stream to limit the number of bytes read from it to a specified length. It's meant as an alternative to:
byte[] data = readAll(length);
InputStream ins = new ByteArrayInputStream(data);
Which requires the extra buffer.
This is the class:
public static class LimitedInputStream extends InputStream {
private final InputStream ins;
private int left;
private int mark = -1;
public LimitedInputStream(InputStream ins, int limit) {
this.ins = ins;
left = limit;
}
public void skipRest() throws IOException {
ByteStreams.skipFully(ins, left);
left = 0;
}
#Override
public int read() throws IOException {
if (left == 0) return -1;
final int read = ins.read();
if (read > 0) left--;
return read;
}
#Override
public int read(byte[] b, int off, int len) throws IOException {
if (left == 0) return -1;
if (len > left) len = left;
final int read = ins.read(b, off, len);
if (read > 0) left -= read;
return read;
}
#Override
public int available() throws IOException {
final int a = ins.available();
return a > left ? left : a;
}
#Override
public void mark(int readlimit) {
ins.mark(readlimit);
mark = left;
}
#Override
public void reset() throws IOException {
if (!ins.markSupported()) throw new IOException("Mark not supported");
if (mark == -1) throw new IOException("Mark not set");
ins.reset();
left = mark;
}
#Override
public long skip(long n) throws IOException {
if (n > left) n = left;
long skipped = ins.skip(n);
left -= skipped;
return skipped;
}
}
Use case:
Object readObj() throws IOException {
int len = readInt();
final LimitedInputStream lis = new LimitedInputStream(this, len);
try {
return deserialize(new CompactInputStream(lis));
} finally {
lis.skipRest();
}
}
for (something) {
Object obj;
try {
obj = readObj();
} catch (Exception e) {
obj = null;
}
list.add(obj);
}
Could you code review my class for any serious bugs, e.g. possible mistakes in updating left?
Guava includes a LimitInputStream, so you may want to just use that.

What is the best way to create a java.util.stream.Stream from an InputStream?

As I understand, an InputStream is a stream of bytes. I am interested in converting an InputStream object to a stream of bytes. Basically, an implementation of the following method.
public Stream<byte[]> toStream(final InputStream is, final int bufferSize);
What would be the best way to get this done? The buffer size is the number of bytes read from the InputStream at a time.
You need to write your own Spliterator, something like this:
public final class ChunkingInputStreamSpliterator implements Spliterator<byte[]> {
private final InputStream is;
private final int bufferSize;
public ChunkingInputStreamSpliterator(InputStream is, int bufferSize) {
this.is = is;
this.bufferSize = bufferSize;
}
#Override
public boolean tryAdvance(Consumer<? super byte[]> action) {
byte[] bytes;
try {
bytes = this.is.readNBytes(this.bufferSize);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
if (bytes.length == 0)
return false;
action.accept(bytes);
return true;
}
#Override
public Spliterator<byte[]> trySplit() {
return null; // cannot split an InputStream
}
#Override
public long estimateSize() {
return Long.MAX_VALUE; // unknown
}
#Override
public int characteristics() {
return Spliterator.ORDERED | Spliterator.NONNULL;
}
}
Then implement your method like this:
public static Stream<byte[]> toStream(InputStream is, int bufferSize) {
return StreamSupport.stream(new ChunkingInputStreamSpliterator(is, bufferSize), false);
}
If you don't have Java 11, so you don't have the very convenient readNBytes method, then do that part yourself like this:
public boolean tryAdvance(Consumer<? super byte[]> action) {
byte[] bytes = new byte[this.bufferSize];
int len = 0;
try {
for (int read; len < bytes.length; len += read)
if ((read = this.is.read(bytes, len, bytes.length - len)) <= 0)
break;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
if (len == 0)
return false;
if (len < bytes.length)
bytes = Arrays.copyOfRange(bytes, 0, len);
action.accept(bytes);
return true;
}

Categories