From ff20c8f2a59269dbcd3dfa1bc31d96a7d5b32f37 Mon Sep 17 00:00:00 2001 From: ipbeegle Date: Mon, 25 May 2020 05:21:10 -0400 Subject: [PATCH] Change StreamNetInput, StreamNetOutput convert to using Java filter streams update syntax --- .../packetlib/io/stream/StreamNetInput.java | 39 +++++-------------- .../packetlib/io/stream/StreamNetOutput.java | 19 ++++----- 2 files changed, 16 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetInput.java b/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetInput.java index 97c95e2b..d96bf9f7 100644 --- a/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetInput.java +++ b/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetInput.java @@ -3,23 +3,23 @@ package com.github.steveice10.packetlib.io.stream; import com.github.steveice10.packetlib.io.NetInput; import java.io.EOFException; +import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.UUID; /** * A NetInput implementation using an InputStream as a backend. */ -public class StreamNetInput implements NetInput { - private InputStream in; - +public class StreamNetInput extends FilterInputStream implements NetInput { /** * Creates a new StreamNetInput instance. * * @param in InputStream to read from. */ public StreamNetInput(InputStream in) { - this.in = in; + super(in); } @Override @@ -34,7 +34,7 @@ public class StreamNetInput implements NetInput { @Override public int readUnsignedByte() throws IOException { - int b = this.in.read(); + int b = this.read(); if(b < 0) { throw new EOFException(); } @@ -125,7 +125,7 @@ public class StreamNetInput implements NetInput { byte b[] = new byte[length]; int n = 0; while(n < length) { - int count = this.in.read(b, n, length - n); + int count = this.read(b, n, length - n); if(count < 0) { throw new EOFException(); } @@ -138,12 +138,12 @@ public class StreamNetInput implements NetInput { @Override public int readBytes(byte[] b) throws IOException { - return this.in.read(b); + return this.read(b); } @Override public int readBytes(byte[] b, int offset, int length) throws IOException { - return this.in.read(b, offset, length); + return this.read(b, offset, length); } @Override @@ -249,32 +249,11 @@ public class StreamNetInput implements NetInput { public String readString() throws IOException { int length = this.readVarInt(); byte bytes[] = this.readBytes(length); - return new String(bytes, "UTF-8"); + return new String(bytes, StandardCharsets.UTF_8); } @Override public UUID readUUID() throws IOException { return new UUID(this.readLong(), this.readLong()); } - - @Override - public int available() throws IOException { - return this.in.available(); - } - - public void mark(int readLimit) { - this.in.mark(readLimit); - } - - public boolean markSupported() { - return this.in.markSupported(); - } - - public void reset() throws IOException { - this.in.reset(); - } - - public long skip(long n) throws IOException { - return this.in.skip(n); - } } diff --git a/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetOutput.java b/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetOutput.java index bd6ae79a..78562929 100644 --- a/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetOutput.java +++ b/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetOutput.java @@ -2,23 +2,23 @@ package com.github.steveice10.packetlib.io.stream; import com.github.steveice10.packetlib.io.NetOutput; +import java.io.FilterOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import java.util.UUID; /** * A NetOutput implementation using an OutputStream as a backend. */ -public class StreamNetOutput implements NetOutput { - private OutputStream out; - +public class StreamNetOutput extends FilterOutputStream implements NetOutput { /** * Creates a new StreamNetOutput instance. * * @param out OutputStream to write to. */ public StreamNetOutput(OutputStream out) { - this.out = out; + super(out); } @Override @@ -28,7 +28,7 @@ public class StreamNetOutput implements NetOutput { @Override public void writeByte(int b) throws IOException { - this.out.write(b); + this.write(b); } @Override @@ -100,7 +100,7 @@ public class StreamNetOutput implements NetOutput { @Override public void writeBytes(byte b[], int length) throws IOException { - this.out.write(b, 0, length); + this.write(b, 0, length); } @Override @@ -145,7 +145,7 @@ public class StreamNetOutput implements NetOutput { throw new IllegalArgumentException("String cannot be null!"); } - byte[] bytes = s.getBytes("UTF-8"); + byte[] bytes = s.getBytes(StandardCharsets.UTF_8); if(bytes.length > 32767) { throw new IOException("String too big (was " + s.length() + " bytes encoded, max " + 32767 + ")"); } else { @@ -159,9 +159,4 @@ public class StreamNetOutput implements NetOutput { this.writeLong(uuid.getMostSignificantBits()); this.writeLong(uuid.getLeastSignificantBits()); } - - @Override - public void flush() throws IOException { - this.out.flush(); - } }