From fb459defea96e542fd1eadce994f4d8b99132029 Mon Sep 17 00:00:00 2001 From: TudbuT Date: Sun, 9 Jun 2024 22:01:40 +0200 Subject: [PATCH] add byte arrays to TypesStreams --- src/main/java/de/tudbut/io/TypedInputStream.java | 12 ++++++++++++ src/main/java/de/tudbut/io/TypedOutputStream.java | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/src/main/java/de/tudbut/io/TypedInputStream.java b/src/main/java/de/tudbut/io/TypedInputStream.java index 920dd72..c5f2459 100644 --- a/src/main/java/de/tudbut/io/TypedInputStream.java +++ b/src/main/java/de/tudbut/io/TypedInputStream.java @@ -115,6 +115,18 @@ public class TypedInputStream { } return booleans; } + + public byte[] readByteArray() throws IOException { + byte[] bytes = new byte[readInt()]; + int n = 0; + while ((n += stream.read(bytes, n, bytes.length - n)) != 0); + if(n != bytes.length) { + byte[] cutBytes = new byte[n]; + System.arraycopy(bytes, 0, cutBytes, 0, n); + return cutBytes; + } + return bytes; + } public String readString() throws IOException { int i = readInt(); diff --git a/src/main/java/de/tudbut/io/TypedOutputStream.java b/src/main/java/de/tudbut/io/TypedOutputStream.java index f3017a8..ee0e06b 100644 --- a/src/main/java/de/tudbut/io/TypedOutputStream.java +++ b/src/main/java/de/tudbut/io/TypedOutputStream.java @@ -118,6 +118,13 @@ public class TypedOutputStream { stream.flush(); return booleans; } + + public byte[] writeByteArray(byte[] bytes) throws IOException { + int i = bytes.length; + writeInt(i); + stream.write(bytes); + return bytes; + } public String writeString(String string) throws IOException { int i = string.length();