com.github.moaxcp.x11client.protocol.X11InputStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of x11-client Show documentation
Show all versions of x11-client Show documentation
An x11 client implemented in java
package com.github.moaxcp.x11client.protocol;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public class X11InputStream implements X11Input {
private final DataInputStream in;
public X11InputStream(InputStream inputStream) {
in = new DataInputStream(inputStream);
}
@Override
public boolean readBool() throws IOException {
return in.readByte() > 0;
}
@Override
public List readBool(int length) throws IOException {
return readList(length, this::readBool);
}
@Override
public byte readByte() throws IOException {
return (byte) in.readUnsignedByte();
}
@Override
public byte readInt8() throws IOException {
return in.readByte();
}
@Override
public short readInt16() throws IOException {
return in.readShort();
}
@Override
public int readInt32() throws IOException {
return in.readInt();
}
@Override
public List readInt32(int length) throws IOException {
return readList(length, this::readInt32);
}
private interface IOSupplier {
T get() throws IOException;
}
private List readList(int length, IOSupplier supplier) throws IOException {
List result = new ArrayList<>(length);
for(int i = 0; i < length; i++) {
result.add(supplier.get());
}
return result;
}
@Override
public long readInt64() throws IOException {
return in.readLong();
}
@Override
public byte readCard8() throws IOException {
return (byte) in.readUnsignedByte();
}
@Override
public List readCard8(int length) throws IOException {
return readList(length, this::readCard8);
}
@Override
public short readCard16() throws IOException {
return (short) in.readUnsignedShort();
}
@Override
public List readCard16(int length) throws IOException {
return readList(length, this::readCard16);
}
@Override
public int readCard32() throws IOException {
return in.readInt();
}
@Override
public List readCard32(int length) throws IOException {
return readList(length, this::readCard32);
}
@Override
public long readCard64() throws IOException {
return in.readLong();
}
@Override
public List readCard64(int length) throws IOException {
return readList(length, this::readCard64);
}
@Override
public float readFloat() throws IOException {
return in.readFloat();
}
@Override
public List readFloat(int length) throws IOException {
return readList(length, this::readFloat);
}
@Override
public double readDouble() throws IOException {
return in.readDouble();
}
@Override
public List readDouble(int length) throws IOException {
return readList(length, this::readDouble);
}
@Override
public List readChar(int length) throws IOException {
return readList(length, this::readByte);
}
@Override
public String readString8(int length) throws IOException {
byte[] bytes = new byte[length];
int read = in.read(bytes);
if(read != bytes.length) {
throw new IllegalStateException("could not read all bytes for length: \"" + bytes.length + "\"");
}
return new String(bytes, StandardCharsets.US_ASCII);
}
@Override
public List readByte(int length) throws IOException {
if(length == 0) {
return new ArrayList<>();
}
byte[] bytes = new byte[length];
int read = in.read(bytes);
if(read != bytes.length) {
throw new IllegalStateException("could not read all bytes for length: \"" + bytes.length + "\"");
}
List result = new ArrayList<>(length);
for(byte b : bytes) {
result.add(b);
}
return result;
}
@Override
public List readVoid(int length) throws IOException {
return readByte(length);
}
@Override
public int readFd() throws IOException {
return readInt32();
}
@Override
public List readFd(int length) throws IOException {
return readInt32(length);
}
@Override
public int available() throws IOException {
return in.available();
}
public void close() throws IOException {
in.close();
}
public void mark(int readLimit) {
in.mark(readLimit);
}
public void reset() throws IOException {
in.reset();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy