Du bist nicht angemeldet.

Lieber Besucher, herzlich willkommen bei: Banana-Coding.com - Java Knuddels Emulator. Falls dies dein erster Besuch auf dieser Seite ist, lies bitte die Hilfe durch. Dort wird dir die Bedienung dieser Seite näher erläutert. Darüber hinaus solltest du dich registrieren, um alle Funktionen dieser Seite nutzen zu können. Benutze das Registrierungsformular, um dich zu registrieren oder informiere dich ausführlich über den Registrierungsvorgang. Falls du dich bereits zu einem früheren Zeitpunkt registriert hast, kannst du dich hier anmelden.

1

28.08.2012, 12:29

PacketReader/Writer

Update (04. September 2012):
- Fehler in den Methoden readShort und readInt in PacketReader behoben.
- Strings werden mit ISO-LATIN-1 en-/dekodiert.
- Die Länge bei Strings wird standardmäßig als Unsigned Short (bis zu 65 kb) vorangestellt.

Hier sind die Packet-Klassen aus einem neuen Projekt von mir. Es werden sowohl Big Endian (Standard) als auch Little Endian Byte Order sowie Signed und Unsigned Werte unterstützt.

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package tools;

import java.nio.ByteOrder;
import java.nio.charset.Charset;

/**
 *
 * @author Flav
 */
public class PacketReader {
	private static Charset charset;

	private ByteOrder order;
	private byte[] bytes;
	private int offset;

	static {
		charset = Charset.forName("ISO-8859-1");
	}

	public PacketReader(byte[] bytes) {
		this(ByteOrder.BIG_ENDIAN, bytes);
	}

	public PacketReader(ByteOrder order, byte[] bytes) {
		this.order = order;
		this.bytes = bytes;
		offset = 0;
	}

	public int remaining() {
		return bytes.length - offset;
	}

	public byte readByte() {
		return bytes[offset++];
	}

	public short readUnsignedByte() {
		return (short) (readByte() & 0xFF);
	}

	public byte[] readByteArray(int length) {
		byte[] arr = new byte[length];

		for (int i = 0; i < length; i++) {
			arr[i] = readByte();
		}

		return arr;
	}

	public short readShort() {
		short b1 = readUnsignedByte();
		short b2 = readUnsignedByte();

		if (order.equals(ByteOrder.BIG_ENDIAN)) {
			return (short) (b1 << 8 | b2);
		}

		return (short) (b2 << 8 | b1);
	}

	public int readUnsignedShort() {
		return readShort() & 0xFFFF;
	}

	public int readInt() {
		short b1 = readUnsignedByte();
		short b2 = readUnsignedByte();
		short b3 = readUnsignedByte();
		short b4 = readUnsignedByte();

		if (order.equals(ByteOrder.BIG_ENDIAN)) {
			return b1 << 24 | b2 << 16 | b3 << 8 | b4;
		}

		return b4 << 24 | b3 << 16 | b2 << 8 | b1;
	}

	public long readUnsignedInt() {
		return readInt() & 0xFFFFFFFFL;
	}

	public long readLong() {
		short b1 = readUnsignedByte();
		short b2 = readUnsignedByte();
		short b3 = readUnsignedByte();
		short b4 = readUnsignedByte();
		short b5 = readUnsignedByte();
		short b6 = readUnsignedByte();
		short b7 = readUnsignedByte();
		short b8 = readUnsignedByte();

		if (order.equals(ByteOrder.BIG_ENDIAN)) {
			return (long) b1 << 56 | (long) b2 << 48 | (long) b3 << 40
					| (long) b4 << 32 | (long) b5 << 24 | b6 << 16 | b7 << 8 | b8;
		}

		return (long) b8 << 56 | (long) b7 << 48 | (long) b6 << 40
				| (long) b5 << 32 | (long) b4 << 24 | b3 << 16 | b2 << 8 | b1;
	}

	public String readString() {
		return readString(readUnsignedShort());
	}

	public String readString(int length) {
		return new String(readByteArray(length), charset);
	}
}


Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package tools;

import java.io.ByteArrayOutputStream;
import java.nio.ByteOrder;
import java.nio.charset.Charset;

/**
 *
 * @author Flav
 */
public class PacketWriter {
	private static Charset charset;

	private ByteOrder order;
	private ByteArrayOutputStream out;

	static {
		charset = Charset.forName("ISO-8859-1");
	}

	public PacketWriter() {
		this(ByteOrder.BIG_ENDIAN);
	}

	public PacketWriter(int capacity) {
		this(ByteOrder.BIG_ENDIAN, capacity);
	}

	public PacketWriter(ByteOrder order) {
		this.order = order;
		out = new ByteArrayOutputStream();
	}

	public PacketWriter(ByteOrder order, int capacity) {
		this.order = order;
		out = new ByteArrayOutputStream(capacity);
	}

	public byte[] toByteArray() {
		return out.toByteArray();
	}

	public void writeByte(long value) {
		out.write((byte) value);
	}

	public void writeByteArray(byte[] arr) {
		int length = arr.length;

		for (int i = 0; i < length; i++) {
			writeByte(arr[i]);
		}
	}

	public void writeShort(int value) {
		if (order.equals(ByteOrder.BIG_ENDIAN)) {
			writeByte(value >> 8);
			writeByte(value);
		} else {
			writeByte(value);
			writeByte(value >> 8);
		}
	}

	public void writeInt(long value) {
		if (order.equals(ByteOrder.BIG_ENDIAN)) {
			writeByte(value >> 24);
			writeByte(value >> 16);
			writeByte(value >> 8);
			writeByte(value);
		} else {
			writeByte(value);
			writeByte(value >> 8);
			writeByte(value >> 16);
			writeByte(value >> 24);
		}
	}

	public void writeLong(long value) {
		if (order.equals(ByteOrder.BIG_ENDIAN)) {
			writeByte(value >> 56);
			writeByte(value >> 48);
			writeByte(value >> 40);
			writeByte(value >> 32);
			writeByte(value >> 24);
			writeByte(value >> 16);
			writeByte(value >> 8);
			writeByte(value);
		} else {
			writeByte(value);
			writeByte(value >> 8);
			writeByte(value >> 16);
			writeByte(value >> 24);
			writeByte(value >> 32);
			writeByte(value >> 40);
			writeByte(value >> 48);
			writeByte(value >> 56);
		}
	}

	public void writeString(String str) {
		writeString(str, true);
	}

	public void writeString(String str, boolean writeLength) {
		byte[] bytes = str.getBytes(charset);

		if (writeLength) {
			writeShort(bytes.length);
		}

		writeByteArray(bytes);
	}
}
Tschüß.

Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »Flav« (04.09.2012, 09:08)


Es haben sich bereits 2 registrierte Benutzer bedankt.

Benutzer, die sich für diesen Beitrag bedankt haben:

Blizzard, Basti

Ähnliche Themen