Since we're sharing our codez, might as well post mine. (These are for Java - without using any sort of IO)
ServerMessage/Response
PHP Code:
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
public class Response
{
private int Header;
private ByteArrayOutputStream lastByteStream;
private ByteArrayOutputStream packetByteStream;
private DataOutputStream packet;
private DataOutputStream last;
public Response() {
}
public Response Initialize(int Header)
{
try
{
this.lastByteStream = new ByteArrayOutputStream();
this.packetByteStream = new ByteArrayOutputStream();
this.last = new DataOutputStream(lastByteStream);
this.packet = new DataOutputStream(packetByteStream);
this.Header = Header;
}
catch (Exception e)
{
e.printStackTrace();
}
return this;
}
public Response AppendInt32(int obj)
{
try
{
packet.writeInt(obj);
}
catch (Exception e)
{
e.printStackTrace();
}
return this;
}
public Response AppendString(String str)
{
try
{
packet.writeShort(str.length());
packet.writeBytes(str);
}
catch (Exception e)
{
e.printStackTrace();
}
return this;
}
public Response AppendString(Integer i)
{
this.AppendString("" + i);
return this;
}
public Response AppendBoolean(Boolean b)
{
try
{
packet.writeByte(b ? 1 : 0);
}
catch (Exception e)
{
e.printStackTrace();
}
return this;
}
public void AppendBody(ISerialize obj) {
obj.SerializePacket(this);
}
public byte[] toBytes()
{
try
{
byte[] data = packetByteStream.toByteArray();
last.writeInt(data.length + 2);
last.writeShort(Header);
last.write(data);
return lastByteStream.toByteArray();
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
public int getHeader() {
return Header;
}
}
ClientMessage/Request
PHP Code:
import java.io.DataInputStream;
public class Request implements Cloneable
{
private Short Header;
private byte[] content;
private DataInputStream stream;
public Request(Short id, DataInputStream stream, byte[] content)
{
this.Header = id;
this.content = content;
this.stream = stream;
}
public Integer PopInt32()
{
int i = 0;
try
{
i = stream.readInt();
}
catch (Exception e)
{
e.printStackTrace();
}
return i;
}
public int popFixedInt()
{
int i = 0;
String s = PopFixedString();
i = Integer.parseInt(s);
return i;
}
public String PopFixedString()
{
String i = "";
try
{
i = stream.readUTF();
}
catch (Exception e)
{
e.printStackTrace();
}
return i;
}
public Short GetHeader()
{
return this.Header;
}
public String getBodyString()
{
String str = new String(content);
String consoleText = str;
for (int i = 0; i < 13; i++) {
consoleText = consoleText.replace(Character.toString((char)i), "{" + i + "}");
}
return consoleText;
}
public int getCurrentLength()
{
int len = 0;
try
{
len = stream.available();
}
catch (Exception e)
{
e.printStackTrace();
}
return len;
}
}