Some basic entities and project structure

This commit is contained in:
2015-03-20 14:18:29 +01:00
commit caf66ea9a5
24 changed files with 1614 additions and 0 deletions

12
domain/build.gradle Normal file
View File

@ -0,0 +1,12 @@
apply plugin: 'java'
sourceCompatibility = 1.7
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}

View File

@ -0,0 +1,65 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.entity;
import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
import ch.dissem.bitmessage.utils.Encode;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* Created by chris on 13.03.15.
*/
public class Addr implements MessagePayload {
private final List<NetworkAddress> addresses;
private Addr(Builder builder) {
addresses = builder.addresses;
}
@Override
public String getCommand() {
return "addr";
}
@Override
public void write(OutputStream stream) throws IOException {
Encode.varInt(addresses.size(), stream);
for (NetworkAddress address : addresses) {
address.write(stream);
}
}
public static final class Builder {
private List<NetworkAddress> addresses = new ArrayList<NetworkAddress>();
public Builder() {
}
public Builder addAddress(final NetworkAddress address) {
this.addresses.add(address);
return this;
}
public Addr build() {
return new Addr(this);
}
}
}

View File

@ -0,0 +1,24 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.entity;
/**
* Created by chris on 10.03.15.
*/
public interface MessagePayload extends Streamable {
String getCommand();
}

View File

@ -0,0 +1,90 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.entity;
import ch.dissem.bitmessage.utils.Encode;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
/**
* Created by chris on 10.03.15.
*/
public class NetworkMessage implements Streamable {
/**
* Magic value indicating message origin network, and used to seek to next message when stream state is unknown
*/
private final static int MAGIC = 0xE9BEB4D9;
private MessagePayload payload;
public NetworkMessage(MessagePayload payload) {
this.payload = payload;
}
/**
* First 4 bytes of sha512(payload)
*/
private byte[] getChecksum(byte[] bytes) throws NoSuchProviderException, NoSuchAlgorithmException {
MessageDigest mda = MessageDigest.getInstance("SHA-512");
byte[] d = mda.digest(bytes);
return new byte[]{d[0], d[1], d[2], d[3]};
}
/**
* The actual data, a message or an object. Not to be confused with objectPayload.
*/
public MessagePayload getPayload() {
return payload;
}
@Override
public void write(OutputStream stream) throws IOException {
// magic
Encode.int32(MAGIC, stream);
// ASCII string identifying the packet content, NULL padded (non-NULL padding results in packet rejected)
stream.write(payload.getCommand().getBytes("ASCII"));
for (int i = payload.getCommand().length(); i < 12; i++) {
stream.write('\0');
}
ByteArrayOutputStream payloadStream = new ByteArrayOutputStream();
payload.write(payloadStream);
byte[] payloadBytes = payloadStream.toByteArray();
// Length of payload in number of bytes. Because of other restrictions, there is no reason why this length would
// ever be larger than 1600003 bytes. Some clients include a sanity-check to avoid processing messages which are
// larger than this.
Encode.int32(payloadBytes.length, stream);
// checksum
try {
stream.write(getChecksum(payloadBytes));
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
// message payload
stream.write(payloadBytes);
}
}

View File

@ -0,0 +1,23 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.entity;
/**
* Created by chris on 16.03.15.
*/
public interface ObjectPayload {
}

View File

@ -0,0 +1,27 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.entity;
import java.io.IOException;
import java.io.OutputStream;
/**
* Created by chris on 10.03.15.
*/
public interface Streamable {
void write(OutputStream stream) throws IOException;
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.entity;
import java.io.IOException;
import java.io.OutputStream;
/**
* Created by chris on 10.03.15.
*/
public class VerAck implements MessagePayload {
@Override
public String getCommand() {
return "verack";
}
@Override
public void write(OutputStream stream) throws IOException {
// NO OP
}
}

View File

@ -0,0 +1,191 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.entity;
import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
import ch.dissem.bitmessage.utils.Encode;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
/**
* Created by chris on 10.03.15.
*/
public class Version implements MessagePayload {
/**
* Identifies protocol version being used by the node. Should equal 3. Nodes should disconnect if the remote node's
* version is lower but continue with the connection if it is higher.
*/
private final int version;
/**
* bitfield of features to be enabled for this connection
*/
private final long services;
/**
* standard UNIX timestamp in seconds
*/
private final long timestamp;
/**
* The network address of the node receiving this message (not including the time or stream number)
*/
private final NetworkAddress addrRecv;
/**
* The network address of the node emitting this message (not including the time or stream number and the ip itself
* is ignored by the receiver)
*/
private final NetworkAddress addrFrom;
/**
* Random nonce used to detect connections to self.
*/
private final long nonce;
/**
* User Agent (0x00 if string is 0 bytes long). Sending nodes must not include a user_agent longer than 5000 bytes.
*/
private final String userAgent;
/**
* The stream numbers that the emitting node is interested in. Sending nodes must not include more than 160000
* stream numbers.
*/
private final long[] streamNumbers;
public int getVersion() {
return version;
}
public long getServices() {
return services;
}
public long getTimestamp() {
return timestamp;
}
public NetworkAddress getAddrRecv() {
return addrRecv;
}
public NetworkAddress getAddrFrom() {
return addrFrom;
}
public long getNonce() {
return nonce;
}
public String getUserAgent() {
return userAgent;
}
public long[] getStreamNumbers() {
return streamNumbers;
}
private Version(Builder builder) {
version = builder.version;
services = builder.services;
timestamp = builder.timestamp;
addrRecv = builder.addrRecv;
addrFrom = builder.addrFrom;
nonce = builder.nonce;
userAgent = builder.userAgent;
streamNumbers = builder.streamNumbers;
}
@Override
public String getCommand() {
return "ver";
}
@Override
public void write(OutputStream stream) throws IOException {
Encode.int32(version, stream);
Encode.int64(services, stream);
Encode.int64(timestamp, stream);
addrRecv.write(stream);
addrFrom.write(stream);
Encode.int64(nonce, stream);
Encode.varString(userAgent, stream);
Encode.varIntList(streamNumbers, stream);
}
public static final class Builder {
private int version = 3;
private long services = 1; // This is a normal network node
private long timestamp = System.currentTimeMillis() / 1000;
private NetworkAddress addrRecv;
private NetworkAddress addrFrom;
private long nonce = new Random().nextInt();
private String userAgent = "/Jabit:0.0.1/";
private long[] streamNumbers = {1};
public Builder() {
}
public Builder version(int version) {
this.version = version;
return this;
}
public Builder services(long services) {
this.services = services;
return this;
}
public Builder timestamp(long timestamp) {
this.timestamp = timestamp;
return this;
}
public Builder addrRecv(NetworkAddress addrRecv) {
this.addrRecv = addrRecv;
return this;
}
public Builder addrFrom(NetworkAddress addrFrom) {
this.addrFrom = addrFrom;
return this;
}
public Builder nonce(long nonce) {
this.nonce = nonce;
return this;
}
public Builder userAgent(String userAgent) {
this.userAgent = userAgent;
return this;
}
public Builder streamNumbers(long... streamNumbers) {
this.streamNumbers = streamNumbers;
return this;
}
public Version build() {
return new Version(this);
}
}
}

View File

@ -0,0 +1,41 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.entity.valueobject;
import ch.dissem.bitmessage.entity.Streamable;
import java.io.IOException;
import java.io.OutputStream;
/**
* Created by chris on 13.03.15.
*/
public class InventoryVector implements Streamable {
/**
* Hash of the object
*/
private final byte[] hash;
public InventoryVector(byte[] hash) {
this.hash = hash;
}
@Override
public void write(OutputStream stream) throws IOException {
stream.write(hash);
}
}

View File

@ -0,0 +1,155 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.entity.valueobject;
import ch.dissem.bitmessage.entity.Streamable;
import ch.dissem.bitmessage.utils.Encode;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Objects;
/**
* Created by chris on 10.03.15.
*/
public class NetworkAddress implements Streamable {
private long time;
/**
* Stream number for this node
*/
private int stream;
/**
* same service(s) listed in version
*/
private long services;
/**
* IPv6 address. IPv4 addresses are written into the message as a 16 byte IPv4-mapped IPv6 address
* (12 bytes 00 00 00 00 00 00 00 00 00 00 FF FF, followed by the 4 bytes of the IPv4 address).
*/
private byte[] ipv6;
private int port;
public int getPort() {
return port;
}
public InetAddress toInetAddress() {
try {
return InetAddress.getByAddress(ipv6);
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
}
private NetworkAddress(Builder builder) {
time = builder.time;
stream = builder.stream;
services = builder.services;
ipv6 = builder.ipv6;
port = builder.port;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
NetworkAddress that = (NetworkAddress) o;
return port == that.port && Arrays.equals(ipv6, that.ipv6);
}
@Override
public int hashCode() {
int result = ipv6 != null ? Arrays.hashCode(ipv6) : 0;
result = 31 * result + port;
return result;
}
@Override
public void write(OutputStream stream) throws IOException {
Encode.int64(time, stream);
Encode.int32(this.stream, stream);
Encode.int64(services, stream);
stream.write(ipv6);
Encode.int16(port, stream);
}
public static final class Builder {
private long time;
private int stream;
private long services = 1;
private byte[] ipv6;
private int port;
public Builder() {
}
public Builder time(final long time) {
this.time = time;
return this;
}
public Builder stream(final int stream) {
this.stream = stream;
return this;
}
public Builder services(final long services) {
this.services = services;
return this;
}
public Builder ipv6(int p00, int p01, int p02, int p03,
int p04, int p05, int p06, int p07,
int p08, int p09, int p10, int p11,
int p12, int p13, int p14, int p15) {
this.ipv6 = new byte[]{
(byte) p00, (byte) p01, (byte) p02, (byte) p03,
(byte) p04, (byte) p05, (byte) p06, (byte) p07,
(byte) p08, (byte) p09, (byte) p10, (byte) p11,
(byte) p12, (byte) p13, (byte) p14, (byte) p15
};
return this;
}
public Builder ipv4(int p00, int p01, int p02, int p03) {
this.ipv6 = new byte[]{
(byte) 0, (byte) 0, (byte) 0x00, (byte) 0x00,
(byte) 0, (byte) 0, (byte) 0x00, (byte) 0x00,
(byte) 0, (byte) 0, (byte) 0xff, (byte) 0xff,
(byte) p00, (byte) p01, (byte) p02, (byte) p03
};
return this;
}
public Builder port(final int port) {
this.port = port;
return this;
}
public NetworkAddress build() {
return new NetworkAddress(this);
}
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.ports;
import ch.dissem.bitmessage.entity.ObjectPayload;
import ch.dissem.bitmessage.entity.valueobject.InventoryVector;
import java.util.List;
/**
* Created by chris on 16.03.15.
*/
public interface Inventory {
public List<InventoryVector> getInventory();
public List<InventoryVector> getMissing(List<InventoryVector> offer);
public ObjectPayload getObject(InventoryVector vector);
public void storeObject(InventoryVector vector, ObjectPayload object);
public void cleanup();
}

View File

@ -0,0 +1,33 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.ports;
import ch.dissem.bitmessage.entity.NetworkMessage;
import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
/**
* Created by chris on 16.03.15.
*/
public interface NetworkMessageReceiver {
public void registerListener(int port);
public void registerListener(NetworkAddress node, MessageListener listener);
public static interface MessageListener {
public void receive(NetworkMessage message);
}
}

View File

@ -0,0 +1,27 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.ports;
import ch.dissem.bitmessage.entity.NetworkMessage;
import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
/**
* Created by chris on 16.03.15.
*/
public interface NetworkMessageSender {
public void send(NetworkAddress node, NetworkMessage message);
}

View File

@ -0,0 +1,77 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.utils;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
/**
* Created by chris on 13.03.15.
*/
public class Encode {
public static void varIntList(long[] values, OutputStream stream) throws IOException {
varInt(values.length, stream);
for (long value : values) {
varInt(value, stream);
}
}
public static void varInt(long value, OutputStream stream) throws IOException {
if (value < 0) {
// This is due to the fact that Java doesn't really support unsigned values.
// Please be aware that this might be an error due to a smaller negative value being cast to long.
// Normally, negative values shouldn't occur within the protocol, and I large enough longs
// to being recognized as negatives aren't realistic.
stream.write(0xff);
int64(value, stream);
} else if (value < 0xfd) {
int8(value, stream);
} else if (value <= 0xffffL) {
stream.write(0xfd);
int16(value, stream);
} else if (value <= 0xffffffffL) {
stream.write(0xfe);
int32(value, stream);
} else {
stream.write(0xff);
int64(value, stream);
}
}
public static void int8(long value, OutputStream stream) throws IOException {
stream.write((int) value);
}
public static void int16(long value, OutputStream stream) throws IOException {
stream.write(ByteBuffer.allocate(4).putInt((int) value).array(), 2, 2);
}
public static void int32(long value, OutputStream stream) throws IOException {
stream.write(ByteBuffer.allocate(4).putInt((int) value).array());
}
public static void int64(long value, OutputStream stream) throws IOException {
stream.write(ByteBuffer.allocate(8).putLong(value).array());
}
public static void varString(String value, OutputStream stream) throws IOException {
byte[] bytes = value.getBytes("utf-8");
varInt(bytes.length, stream);
stream.write(bytes);
}
}

View File

@ -0,0 +1,127 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.utils;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
/**
* Created by chris on 13.03.15.
*/
public class EncodeTest {
@Test
public void testUint8() throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Encode.int8(0, stream);
checkBytes(stream, 0);
stream = new ByteArrayOutputStream();
Encode.int8(255, stream);
checkBytes(stream, 255);
}
@Test
public void testUint16() throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Encode.int16(0, stream);
checkBytes(stream, 0, 0);
stream = new ByteArrayOutputStream();
Encode.int16(513, stream);
checkBytes(stream, 2, 1);
}
@Test
public void testUint32() throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Encode.int32(0, stream);
checkBytes(stream, 0, 0, 0, 0);
stream = new ByteArrayOutputStream();
Encode.int32(67305985, stream);
checkBytes(stream, 4, 3, 2, 1);
stream = new ByteArrayOutputStream();
Encode.int32(3355443201l, stream);
checkBytes(stream, 200, 0, 0, 1);
}
@Test
public void testUint64() throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Encode.int64(0, stream);
checkBytes(stream, 0, 0, 0, 0, 0, 0, 0, 0);
stream = new ByteArrayOutputStream();
Encode.int64(578437695752307201L, stream);
checkBytes(stream, 8, 7, 6, 5, 4, 3, 2, 1);
stream = new ByteArrayOutputStream();
// 200 * 72057594037927936L + 1
Encode.int64(0xc800000000000001L, stream);
checkBytes(stream, 200, 0, 0, 0, 0, 0, 0, 1);
}
@Test
public void testVarInt() throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Encode.varInt(0, stream);
checkBytes(stream, 0);
stream = new ByteArrayOutputStream();
Encode.varInt(252, stream);
checkBytes(stream, 252);
stream = new ByteArrayOutputStream();
Encode.varInt(253, stream);
checkBytes(stream, 253, 0, 253);
stream = new ByteArrayOutputStream();
Encode.varInt(65535, stream);
checkBytes(stream, 253, 255, 255);
stream = new ByteArrayOutputStream();
Encode.varInt(65536, stream);
checkBytes(stream, 254, 0, 1, 0, 0);
stream = new ByteArrayOutputStream();
Encode.varInt(4294967295L, stream);
checkBytes(stream, 254, 255, 255, 255, 255);
stream = new ByteArrayOutputStream();
Encode.varInt(4294967296L, stream);
checkBytes(stream, 255, 0, 0, 0, 1, 0, 0, 0, 0);
stream = new ByteArrayOutputStream();
Encode.varInt(-1L, stream);
checkBytes(stream, 255, 255, 255, 255, 255, 255, 255, 255, 255);
}
public void checkBytes(ByteArrayOutputStream stream, int... bytes) {
assertEquals(bytes.length, stream.size());
byte[] streamBytes = stream.toByteArray();
for (int i = 0; i < bytes.length; i++) {
assertEquals((byte) bytes[i], streamBytes[i]);
}
}
}