From c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Thu, 12 Feb 2026 22:33:32 -0800 Subject: Initial commit --- NET/worlds/network/ServerOutputStream.java | 97 ++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 NET/worlds/network/ServerOutputStream.java (limited to 'NET/worlds/network/ServerOutputStream.java') diff --git a/NET/worlds/network/ServerOutputStream.java b/NET/worlds/network/ServerOutputStream.java new file mode 100644 index 0000000..a95a0f1 --- /dev/null +++ b/NET/worlds/network/ServerOutputStream.java @@ -0,0 +1,97 @@ +package NET.worlds.network; + +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +public class ServerOutputStream extends FilterOutputStream { + private int _version; + + public ServerOutputStream(OutputStream o) { + super(o); + this.setVersion(24); + } + + public ServerOutputStream(OutputStream o, int vers) { + super(o); + this.setVersion(vers); + } + + public void setVersion(int vers) { + this._version = vers; + } + + public int getVersion() { + return this._version; + } + + @Override + public final void write(int b) throws IOException { + this.out.write(b); + } + + @Override + public final void write(byte[] b, int off, int len) throws IOException { + this.out.write(b, off, len); + } + + public final void writeByte(int v) throws IOException { + this.out.write(v); + } + + public final void writeShort(int v) throws IOException { + OutputStream out = this.out; + out.write(v >>> 8 & 0xFF); + out.write(v >>> 0 & 0xFF); + } + + public final void writeInt(int v) throws IOException { + OutputStream out = this.out; + out.write(v >>> 24 & 0xFF); + out.write(v >>> 16 & 0xFF); + out.write(v >>> 8 & 0xFF); + out.write(v >>> 0 & 0xFF); + } + + public static int utfLength(String str) { + int strlen = str.length(); + int utflen = 0; + + for (int i = 0; i < strlen; i++) { + int c = str.charAt(i); + if (c >= 1 && c <= 127) { + utflen++; + } else if (c > 2047) { + utflen += 3; + } else { + utflen += 2; + } + } + + return utflen; + } + + public void writeUTF(String str) throws IOException { + OutputStream out = this.out; + int strlen = str.length(); + int utflen = utfLength(str); + + assert utflen < 256; + + out.write(utflen >>> 0 & 0xFF); + + for (int i = 0; i < strlen; i++) { + int c = str.charAt(i); + if (c >= 1 && c <= 127) { + out.write(c); + } else if (c > 2047) { + out.write(224 | c >> 12 & 15); + out.write(128 | c >> 6 & 63); + out.write(128 | c >> 0 & 63); + } else { + out.write(192 | c >> 6 & 31); + out.write(128 | c >> 0 & 63); + } + } + } +} -- cgit v1.2.3