Sharing my ByteStream, feel free to improve!
Hi guys,
I wrote this a few days ago for my MMORPG.
You can read and write Strings, etc in a Input- or OutputStream with bytes.
Code Java:
package com.verficon.io;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class ByteStream {
private byte[] buffer;
private int pos;
public static ByteStream create() {
final ByteStream s = new ByteStream();
s.buffer = new byte[1024];
return s;
}
public boolean readBuffer(final InputStream is) throws IOException {
return is.read(buffer) != -1;
}
public byte readByte() {
checkBuffer();
return buffer[pos++];
}
public void writeByte(final byte b) {
checkBuffer();
buffer[pos++] = b;
}
private void checkBuffer() {
if (pos >= buffer.length)
pos = 0;
}
public String readString() {
final byte length = readByte();
final byte[] b = new byte[length];
for (int i = 0; i < length; i++) {
b[i] = readByte();
}
return new String(b);
}
public void writeString(final String s) {
final byte[] b = s.getBytes();
writeByte((byte) b.length);
for (int i = 0; i < b.length; i++) {
writeByte(b[i]);
}
}
public void flushBuffer(final OutputStream os) throws IOException {
os.write(buffer);
buffer = new byte[1024];
}
}
Have fun, feel free to add something! :cool:
Feel free to click the 'Thank' button :rolleyes:
Download as External Java Libary: http://www.multiupload.com/AIRXTDUHFZ
Re: Sharing my ByteStream, feel free to improve!
Hello Verficon.
It would be good if you could comment this and write up exactly what it does.
Then you could submit it to: Java Code Snippets and Tutorials :)