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); } } } }