Browse Source

Reformat rencode-java

pull/594/head
Taco 3 years ago
parent
commit
2c538e348a
  1. 9
      app/src/main/java/se/dimovski/rencode/Rencode.java
  2. 245
      app/src/main/java/se/dimovski/rencode/RencodeInputStream.java
  3. 207
      app/src/main/java/se/dimovski/rencode/RencodeOutputStream.java
  4. 6
      app/src/main/java/se/dimovski/rencode/TypeCode.java
  5. 33
      app/src/main/java/se/dimovski/rencode/Utils.java

9
app/src/main/java/se/dimovski/rencode/Rencode.java

@ -5,11 +5,9 @@ import java.io.ByteArrayOutputStream; @@ -5,11 +5,9 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class Rencode
{
public class Rencode {
public static Object decode(byte[] data) throws IOException
{
public static Object decode(byte[] data) throws IOException {
final InputStream is = new ByteArrayInputStream(data);
final RencodeInputStream inputStream = new RencodeInputStream(is);
@ -19,8 +17,7 @@ public class Rencode @@ -19,8 +17,7 @@ public class Rencode
return decoded;
}
public static byte[] encode(Object obj) throws IOException
{
public static byte[] encode(Object obj) throws IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final RencodeOutputStream output = new RencodeOutputStream(baos);
output.writeObject(obj);

245
app/src/main/java/se/dimovski/rencode/RencodeInputStream.java

@ -14,8 +14,7 @@ import java.util.List; @@ -14,8 +14,7 @@ import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class RencodeInputStream extends FilterInputStream implements DataInput
{
public class RencodeInputStream extends FilterInputStream implements DataInput {
/**
* The charset that is being used for {@link String}s.
*/
@ -29,36 +28,31 @@ public class RencodeInputStream extends FilterInputStream implements DataInput @@ -29,36 +28,31 @@ public class RencodeInputStream extends FilterInputStream implements DataInput
/**
* Creates a {@link RencodeInputStream} with the default encoding.
*/
public RencodeInputStream(InputStream in)
{
public RencodeInputStream(InputStream in) {
this(in, Utils.UTF_8, false);
}
/**
* Creates a {@link RencodeInputStream} with the given encoding.
*/
public RencodeInputStream(InputStream in, String charset)
{
public RencodeInputStream(InputStream in, String charset) {
this(in, charset, false);
}
/**
* Creates a {@link RencodeInputStream} with the default encoding.
*/
public RencodeInputStream(InputStream in, boolean decodeAsString)
{
public RencodeInputStream(InputStream in, boolean decodeAsString) {
this(in, Utils.UTF_8, decodeAsString);
}
/**
* Creates a {@link RencodeInputStream} with the given encoding.
*/
public RencodeInputStream(InputStream in, String charset, boolean decodeAsString)
{
public RencodeInputStream(InputStream in, String charset, boolean decodeAsString) {
super(in);
if (charset == null)
{
if (charset == null) {
throw new IllegalArgumentException("charset is null");
}
@ -70,24 +64,21 @@ public class RencodeInputStream extends FilterInputStream implements DataInput @@ -70,24 +64,21 @@ public class RencodeInputStream extends FilterInputStream implements DataInput
* Returns the charset that is used to decode {@link String}s. The default
* value is UTF-8.
*/
public String getCharset()
{
public String getCharset() {
return charset;
}
/**
* Returns true if all byte-Arrays are being turned into {@link String}s.
*/
public boolean isDecodeAsString()
{
public boolean isDecodeAsString() {
return decodeAsString;
}
/**
* Reads and returns an {@link Object}.
*/
public Object readObject() throws IOException
{
public Object readObject() throws IOException {
int token = readToken();
return readObject(token);
@ -96,38 +87,22 @@ public class RencodeInputStream extends FilterInputStream implements DataInput @@ -96,38 +87,22 @@ public class RencodeInputStream extends FilterInputStream implements DataInput
/**
* Reads and returns an {@link Object}.
*/
protected Object readObject(int token) throws IOException
{
if (token == TypeCode.DICTIONARY)
{
protected Object readObject(int token) throws IOException {
if (token == TypeCode.DICTIONARY) {
return readMap0(Object.class);
}
else if (Utils.isFixedDictionary(token))
{
} else if (Utils.isFixedDictionary(token)) {
return readMap0(Object.class, token);
}
else if (token == TypeCode.LIST)
{
} else if (token == TypeCode.LIST) {
return readList0(Object.class);
}
else if (Utils.isFixedList(token))
{
} else if (Utils.isFixedList(token)) {
return readList0(Object.class, token);
}
else if (Utils.isNumber(token))
{
} else if (Utils.isNumber(token)) {
return readNumber0(token);
}
else if (token == TypeCode.FALSE || token == TypeCode.TRUE)
{
} else if (token == TypeCode.FALSE || token == TypeCode.TRUE) {
return readBoolean0(token);
}
else if (token == TypeCode.NULL)
{
} else if (token == TypeCode.NULL) {
return null;
}
else if (Utils.isDigit(token) || Utils.isFixedString(token))
{
} else if (Utils.isDigit(token) || Utils.isFixedString(token)) {
return readString(token, charset);
}
@ -137,45 +112,38 @@ public class RencodeInputStream extends FilterInputStream implements DataInput @@ -137,45 +112,38 @@ public class RencodeInputStream extends FilterInputStream implements DataInput
/**
* Reads and returns a {@link Map}.
*/
public Map<String, ?> readMap() throws IOException
{
public Map<String, ?> readMap() throws IOException {
return readMap(Object.class);
}
/**
* Reads and returns a {@link Map}.
*/
public <T> Map<String, T> readMap(Class<T> clazz) throws IOException
{
public <T> Map<String, T> readMap(Class<T> clazz) throws IOException {
int token = readToken();
if (token != TypeCode.DICTIONARY)
{
if (token != TypeCode.DICTIONARY) {
throw new IOException();
}
return readMap0(clazz);
}
private <T> Map<String, T> readMap0(Class<T> clazz) throws IOException
{
private <T> Map<String, T> readMap0(Class<T> clazz) throws IOException {
Map<String, T> map = new TreeMap<String, T>();
int token = -1;
while ((token = readToken()) != TypeCode.END)
{
while ((token = readToken()) != TypeCode.END) {
readMapItem(clazz, token, map);
}
return map;
}
private <T> Map<String, T> readMap0(Class<T> clazz, int token) throws IOException
{
private <T> Map<String, T> readMap0(Class<T> clazz, int token) throws IOException {
Map<String, T> map = new TreeMap<String, T>();
int count = token - TypeCode.EMBEDDED.DICT_START;
for (int i = 0; i < count; i++)
{
for (int i = 0; i < count; i++) {
readMapItem(clazz, readToken(), map);
}
@ -183,19 +151,16 @@ public class RencodeInputStream extends FilterInputStream implements DataInput @@ -183,19 +151,16 @@ public class RencodeInputStream extends FilterInputStream implements DataInput
}
private <T> void readMapItem(Class<T> clazz, int token, Map<String, T> map) throws UnsupportedEncodingException,
IOException
{
IOException {
String key = readString(token, charset);
T value = clazz.cast(readObject());
map.put(key, value);
}
public int readToken() throws IOException
{
public int readToken() throws IOException {
int token = super.read();
if (token == -1)
{
if (token == -1) {
throw new EOFException();
}
return token;
@ -204,101 +169,81 @@ public class RencodeInputStream extends FilterInputStream implements DataInput @@ -204,101 +169,81 @@ public class RencodeInputStream extends FilterInputStream implements DataInput
/**
* Reads and returns a {@link List}.
*/
public List<?> readList() throws IOException
{
public List<?> readList() throws IOException {
return readList(Object.class);
}
/**
* Reads and returns a {@link List}.
*/
public <T> List<T> readList(Class<T> clazz) throws IOException
{
public <T> List<T> readList(Class<T> clazz) throws IOException {
int token = readToken();
if (token != TypeCode.LIST)
{
if (token != TypeCode.LIST) {
throw new IOException();
}
return readList0(clazz);
}
private <T> List<T> readList0(Class<T> clazz) throws IOException
{
private <T> List<T> readList0(Class<T> clazz) throws IOException {
List<T> list = new ArrayList<T>();
int token = -1;
while ((token = readToken()) != TypeCode.END)
{
while ((token = readToken()) != TypeCode.END) {
list.add(clazz.cast(readObject(token)));
}
return list;
}
private <T> List<T> readList0(Class<T> clazz, int token) throws IOException
{
private <T> List<T> readList0(Class<T> clazz, int token) throws IOException {
List<T> list = new ArrayList<T>();
int length = token - TypeCode.EMBEDDED.LIST_START;
for (int i = 0; i < length; i++)
{
for (int i = 0; i < length; i++) {
list.add(clazz.cast(readObject()));
}
return list;
}
public boolean readBoolean() throws IOException
{
public boolean readBoolean() throws IOException {
return readBoolean0(readToken());
}
public boolean readBoolean0(int token) throws IOException
{
if (token == TypeCode.FALSE)
{
public boolean readBoolean0(int token) throws IOException {
if (token == TypeCode.FALSE) {
return false;
}
else if (token == TypeCode.TRUE)
{
} else if (token == TypeCode.TRUE) {
return true;
}
throw new IOException();
}
public byte readByte() throws IOException
{
public byte readByte() throws IOException {
return (byte) readToken();
}
public char readChar() throws IOException
{
public char readChar() throws IOException {
return (char) readToken();
}
public double readDouble() throws IOException
{
public double readDouble() throws IOException {
return readNumber().doubleValue();
}
public float readFloat() throws IOException
{
public float readFloat() throws IOException {
return readNumber().floatValue();
}
public void readFully(byte[] dst) throws IOException
{
public void readFully(byte[] dst) throws IOException {
readFully(dst, 0, dst.length);
}
public void readFully(byte[] dst, int off, int len) throws IOException
{
public void readFully(byte[] dst, int off, int len) throws IOException {
int total = 0;
while (total < len)
{
while (total < len) {
int r = read(dst, total, len - total);
if (r == -1)
{
if (r == -1) {
throw new EOFException();
}
@ -306,60 +251,49 @@ public class RencodeInputStream extends FilterInputStream implements DataInput @@ -306,60 +251,49 @@ public class RencodeInputStream extends FilterInputStream implements DataInput
}
}
public int readInt() throws IOException
{
public int readInt() throws IOException {
return readNumber().intValue();
}
public String readLine() throws IOException
{
public String readLine() throws IOException {
return readString();
}
public long readLong() throws IOException
{
public long readLong() throws IOException {
return readNumber().longValue();
}
public short readShort() throws IOException
{
public short readShort() throws IOException {
return readNumber().shortValue();
}
public String readUTF() throws IOException
{
public String readUTF() throws IOException {
return readString(Utils.UTF_8);
}
public int readUnsignedByte() throws IOException
{
public int readUnsignedByte() throws IOException {
return readByte() & 0xFF;
}
public int readUnsignedShort() throws IOException
{
public int readUnsignedShort() throws IOException {
return readShort() & 0xFFFF;
}
/**
* Reads and returns a {@link Number}.
*/
public Number readNumber() throws IOException
{
public Number readNumber() throws IOException {
int token = readToken();
if (!Utils.isNumber(token))
{
if (!Utils.isNumber(token)) {
throw new IOException();
}
return readNumber0(token);
}
private Number readNumber0(int token) throws IOException
{
switch (token)
{
private Number readNumber0(int token) throws IOException {
switch (token) {
case TypeCode.BYTE:
return (int) readToBuffer(1).get();
case TypeCode.SHORT:
@ -376,67 +310,52 @@ public class RencodeInputStream extends FilterInputStream implements DataInput @@ -376,67 +310,52 @@ public class RencodeInputStream extends FilterInputStream implements DataInput
case TypeCode.NUMBER:
return readNumber0();
}
if (Utils.isNegativeFixedNumber(token))
{
if (Utils.isNegativeFixedNumber(token)) {
return TypeCode.EMBEDDED.INT_NEG_START - 1 - token;
}
else if (Utils.isPositiveFixedNumber(token))
{
} else if (Utils.isPositiveFixedNumber(token)) {
return TypeCode.EMBEDDED.INT_POS_START + token;
}
throw new IOException("Unknown number. TypeCode: " + token);
}
private ByteBuffer readToBuffer(int count) throws IOException
{
private ByteBuffer readToBuffer(int count) throws IOException {
return ByteBuffer.wrap(readBytesFixed(count));
}
private Number readNumber0() throws IOException
{
private Number readNumber0() throws IOException {
StringBuilder buffer = new StringBuilder();
boolean decimal = false;
int token = -1;
while ((token = readToken()) != TypeCode.END)
{
if (token == '.')
{
while ((token = readToken()) != TypeCode.END) {
if (token == '.') {
decimal = true;
}
buffer.append((char) token);
}
try
{
if (decimal)
{
try {
if (decimal) {
return new BigDecimal(buffer.toString());
}
else
{
} else {
return new BigInteger(buffer.toString());
}
}
catch (NumberFormatException err)
{
} catch (NumberFormatException err) {
throw new IOException("NumberFormatException", err);
}
}
public int skipBytes(int n) throws IOException
{
public int skipBytes(int n) throws IOException {
return (int) skip(n);
}
/**
* Reads and returns a byte-Array.
*/
public byte[] readBytes() throws IOException
{
public byte[] readBytes() throws IOException {
int token = readToken();
return readBytes(token);
@ -445,46 +364,38 @@ public class RencodeInputStream extends FilterInputStream implements DataInput @@ -445,46 +364,38 @@ public class RencodeInputStream extends FilterInputStream implements DataInput
/**
* Reads and returns a {@link String}.
*/
public String readString() throws IOException
{
public String readString() throws IOException {
return readString(charset);
}
private String readString(String encoding) throws IOException
{
private String readString(String encoding) throws IOException {
return readString(readToken(), encoding);
}
private String readString(int token, String charset) throws IOException
{
if (Utils.isFixedString(token))
{
private String readString(int token, String charset) throws IOException {
if (Utils.isFixedString(token)) {
int length = token - TypeCode.EMBEDDED.STR_START;
return new String(readBytesFixed(length), charset);
}
return new String(readBytes(token), charset);
}
private byte[] readBytes(int token) throws IOException
{
private byte[] readBytes(int token) throws IOException {
int length = readLength(token);
return readBytesFixed(length);
}
private byte[] readBytesFixed(int count) throws IOException
{
private byte[] readBytesFixed(int count) throws IOException {
byte[] data = new byte[count];
readFully(data);
return data;
}
private int readLength(int token) throws IOException
{
private int readLength(int token) throws IOException {
StringBuilder buffer = new StringBuilder();
buffer.append((char) token);
while ((token = readToken()) != TypeCode.LENGTH_DELIM)
{
while ((token = readToken()) != TypeCode.LENGTH_DELIM) {
buffer.append((char) token);
}

207
app/src/main/java/se/dimovski/rencode/RencodeOutputStream.java

@ -11,8 +11,7 @@ import java.util.Map; @@ -11,8 +11,7 @@ import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
public class RencodeOutputStream extends FilterOutputStream implements DataOutput
{
public class RencodeOutputStream extends FilterOutputStream implements DataOutput {
/**
* The {@link String} charset.
@ -22,20 +21,17 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu @@ -22,20 +21,17 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu
/**
* Creates a {@link RencodeOutputStream} with the default charset.
*/
public RencodeOutputStream(OutputStream out)
{
public RencodeOutputStream(OutputStream out) {
this(out, Utils.UTF_8);
}
/**
* Creates a {@link RencodeOutputStream} with the given encoding.
*/
public RencodeOutputStream(OutputStream out, String charset)
{
public RencodeOutputStream(OutputStream out, String charset) {
super(out);
if (charset == null)
{
if (charset == null) {
throw new NullPointerException("charset");
}
@ -46,66 +42,43 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu @@ -46,66 +42,43 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu
* Returns the charset that is used to encode {@link String}s. The default
* value is UTF-8.
*/
public String getCharset()
{
public String getCharset() {
return charset;
}
/**
* Writes an {@link Object}.
*/
public void writeObject(Object value) throws IOException
{
if (value == null)
{
public void writeObject(Object value) throws IOException {
if (value == null) {
writeNull();
}
else if (value instanceof byte[])
{
} else if (value instanceof byte[]) {
writeBytes((byte[]) value);
}
else if (value instanceof Boolean)
{
} else if (value instanceof Boolean) {
writeBoolean((Boolean) value);
}
else if (value instanceof Character)
{
} else if (value instanceof Character) {
writeChar((Character) value);
}
else if (value instanceof Number)
{
} else if (value instanceof Number) {
writeNumber((Number) value);
}
else if (value instanceof String)
{
} else if (value instanceof String) {
writeString((String) value);
}
else if (value instanceof Collection<?>)
{
} else if (value instanceof Collection<?>) {
writeCollection((Collection<?>) value);
}
else if (value instanceof Map<?, ?>)
{
} else if (value instanceof Map<?, ?>) {
writeMap((Map<?, ?>) value);
}
else if (value instanceof Enum<?>)
{
} else if (value instanceof Enum<?>) {
writeEnum((Enum<?>) value);
}
else if (value.getClass().isArray())
{
} else if (value.getClass().isArray()) {
writeArray(value);
}
else
{
} else {
writeCustom(value);
}
}
@ -113,8 +86,7 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu @@ -113,8 +86,7 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu
/**
* Writes a null value
*/
public void writeNull() throws IOException
{
public void writeNull() throws IOException {
write(TypeCode.NULL);
}
@ -122,48 +94,42 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu @@ -122,48 +94,42 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu
* Overwrite this method to write custom objects. The default implementation
* throws an {@link IOException}.
*/
protected void writeCustom(Object value) throws IOException
{
protected void writeCustom(Object value) throws IOException {
throw new IOException("Cannot encode " + value);
}
/**
* Writes the given byte-Array
*/
public void writeBytes(byte[] value) throws IOException
{
public void writeBytes(byte[] value) throws IOException {
writeBytes(value, 0, value.length);
}
/**
* Writes the given byte-Array
*/
public void writeBytes(byte[] value, int offset, int length) throws IOException
{
public void writeBytes(byte[] value, int offset, int length) throws IOException {
write(value, offset, length);
}
/**
* Writes a boolean
*/
public void writeBoolean(boolean value) throws IOException
{
public void writeBoolean(boolean value) throws IOException {
write(value ? TypeCode.TRUE : TypeCode.FALSE);
}
/**
* Writes a char
*/
public void writeChar(int value) throws IOException
{
public void writeChar(int value) throws IOException {
writeByte(value);
}
/**
* Writes a byte
*/
public void writeByte(int value) throws IOException
{
public void writeByte(int value) throws IOException {
write(TypeCode.BYTE);
write(value);
}
@ -171,8 +137,7 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu @@ -171,8 +137,7 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu
/**
* Writes a short
*/
public void writeShort(int value) throws IOException
{
public void writeShort(int value) throws IOException {
write(TypeCode.SHORT);
ByteBuffer buffer = ByteBuffer.allocate(Utils.SHORT_BYTES).putShort((short) value);
write(buffer.array());
@ -181,8 +146,7 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu @@ -181,8 +146,7 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu
/**
* Writes an int
*/
public void writeInt(int value) throws IOException
{
public void writeInt(int value) throws IOException {
write(TypeCode.INT);
ByteBuffer buffer = ByteBuffer.allocate(Utils.INTEGER_BYTES).putInt(value);
write(buffer.array());
@ -191,8 +155,7 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu @@ -191,8 +155,7 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu
/**
* Writes a long
*/
public void writeLong(long value) throws IOException
{
public void writeLong(long value) throws IOException {
write(TypeCode.LONG);
ByteBuffer buffer = ByteBuffer.allocate(Utils.LONG_BYTES).putLong(value);
write(buffer.array());
@ -201,8 +164,7 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu @@ -201,8 +164,7 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu
/**
* Writes a float
*/
public void writeFloat(float value) throws IOException
{
public void writeFloat(float value) throws IOException {
write(TypeCode.FLOAT);
ByteBuffer buffer = ByteBuffer.allocate(Utils.FLOAT_BYTES).putFloat(value);
write(buffer.array());
@ -211,8 +173,7 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu @@ -211,8 +173,7 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu
/**
* Writes a double
*/
public void writeDouble(double value) throws IOException
{
public void writeDouble(double value) throws IOException {
write(TypeCode.DOUBLE);
ByteBuffer buffer = ByteBuffer.allocate(Utils.DOUBLE_BYTES).putDouble(value);
write(buffer.array());
@ -221,42 +182,25 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu @@ -221,42 +182,25 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu
/**
* Writes a {@link Number}
*/
public void writeNumber(Number num) throws IOException
{
if (num instanceof Float)
{
public void writeNumber(Number num) throws IOException {
if (num instanceof Float) {
writeFloat(num.floatValue());
}
else if (num instanceof Double)
{
} else if (num instanceof Double) {
writeDouble(num.doubleValue());
}
if (0 <= num.intValue() && num.intValue() < TypeCode.EMBEDDED.INT_POS_COUNT)
{
if (0 <= num.intValue() && num.intValue() < TypeCode.EMBEDDED.INT_POS_COUNT) {
write(TypeCode.EMBEDDED.INT_POS_START + num.intValue());
}
else if (-TypeCode.EMBEDDED.INT_NEG_COUNT <= num.intValue() && num.intValue() < 0)
{
} else if (-TypeCode.EMBEDDED.INT_NEG_COUNT <= num.intValue() && num.intValue() < 0) {
write(TypeCode.EMBEDDED.INT_NEG_START - 1 - num.intValue());
}
else if (Byte.MIN_VALUE <= num.intValue() && num.intValue() < Byte.MAX_VALUE)
{
} else if (Byte.MIN_VALUE <= num.intValue() && num.intValue() < Byte.MAX_VALUE) {
writeByte(num.byteValue());
}
else if (Short.MIN_VALUE <= num.intValue() && num.intValue() < Short.MAX_VALUE)
{
} else if (Short.MIN_VALUE <= num.intValue() && num.intValue() < Short.MAX_VALUE) {
writeShort(num.shortValue());
}
else if (Integer.MIN_VALUE <= num.longValue() && num.longValue() < Integer.MAX_VALUE)
{
} else if (Integer.MIN_VALUE <= num.longValue() && num.longValue() < Integer.MAX_VALUE) {
writeInt(num.intValue());
}
else if (Long.MIN_VALUE <= num.longValue() && num.longValue() < Long.MAX_VALUE)
{
} else if (Long.MIN_VALUE <= num.longValue() && num.longValue() < Long.MAX_VALUE) {
writeLong(num.longValue());
}
else
{
} else {
String number = num.toString();
write(TypeCode.NUMBER);
write(number.getBytes(charset));
@ -267,15 +211,11 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu @@ -267,15 +211,11 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu
/**
* Writes a {@link String}
*/
public void writeString(String value) throws IOException
{
public void writeString(String value) throws IOException {
int len = value.length();
if (len < TypeCode.EMBEDDED.STR_COUNT)
{
if (len < TypeCode.EMBEDDED.STR_COUNT) {
write(TypeCode.EMBEDDED.STR_START + len);
}
else
{
} else {
String lenString = Integer.toString(len);
writeBytes(lenString.getBytes(charset));
write(TypeCode.LENGTH_DELIM);
@ -287,25 +227,19 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu @@ -287,25 +227,19 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu
/**
* Writes a {@link Collection}.
*/
public void writeCollection(Collection<?> value) throws IOException
{
public void writeCollection(Collection<?> value) throws IOException {
boolean useEndToken = value.size() >= TypeCode.EMBEDDED.LIST_COUNT;
if (useEndToken)
{
if (useEndToken) {
write(TypeCode.LIST);
}
else
{
} else {
write(TypeCode.EMBEDDED.LIST_START + value.size());
}
for (Object element : value)
{
for (Object element : value) {
writeObject(element);
}
if (useEndToken)
{
if (useEndToken) {
write(TypeCode.END);
}
}
@ -313,32 +247,25 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu @@ -313,32 +247,25 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu
/**
* Writes a {@link Map}.
*/
public void writeMap(Map<?, ?> map) throws IOException
{
if (!(map instanceof SortedMap<?, ?>))
{
public void writeMap(Map<?, ?> map) throws IOException {
if (!(map instanceof SortedMap<?, ?>)) {
map = new TreeMap<Object, Object>(map);
}
boolean untilEnd = map.size() >= TypeCode.EMBEDDED.DICT_COUNT;
if (untilEnd)
{
if (untilEnd) {
write(TypeCode.DICTIONARY);
}
else
{
} else {
write(TypeCode.EMBEDDED.DICT_START + map.size());
}
for (Map.Entry<?, ?> entry : map.entrySet())
{
for (Map.Entry<?, ?> entry : map.entrySet()) {
writeObject(entry.getKey());
writeObject(entry.getValue());
}
if (untilEnd)
{
if (untilEnd) {
write(TypeCode.END);
}
}
@ -346,34 +273,27 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu @@ -346,34 +273,27 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu
/**
* Writes an {@link Enum}.
*/
public void writeEnum(Enum<?> value) throws IOException
{
public void writeEnum(Enum<?> value) throws IOException {
writeString(value.name());
}
/**
* Writes an array
*/
public void writeArray(Object value) throws IOException
{
public void writeArray(Object value) throws IOException {
int length = Array.getLength(value);
boolean useEndToken = length >= TypeCode.EMBEDDED.LIST_COUNT;
if (useEndToken)
{
if (useEndToken) {
write(TypeCode.LIST);
}
else
{
} else {
write(TypeCode.EMBEDDED.LIST_START + length);
}
for (int i = 0; i < length; i++)
{
for (int i = 0; i < length; i++) {
writeObject(Array.get(value, i));
}
if (useEndToken)
{
if (useEndToken) {
write(TypeCode.END);
}
}
@ -381,24 +301,21 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu @@ -381,24 +301,21 @@ public class RencodeOutputStream extends FilterOutputStream implements DataOutpu
/**
* Writes the given {@link String}
*/
public void writeBytes(String value) throws IOException
{
public void writeBytes(String value) throws IOException {
writeString(value);
}
/**
* Writes the given {@link String}
*/
public void writeChars(String value) throws IOException
{
public void writeChars(String value) throws IOException {
writeString(value);
}
/**
* Writes an UTF encoded {@link String}
*/
public void writeUTF(String value) throws IOException
{
public void writeUTF(String value) throws IOException {
writeBytes(value.getBytes(Utils.UTF_8));
}
}

6
app/src/main/java/se/dimovski/rencode/TypeCode.java

@ -1,7 +1,6 @@ @@ -1,7 +1,6 @@
package se.dimovski.rencode;
public class TypeCode
{
public class TypeCode {
// The bencode 'typecodes' such as i, d, etc have been
// extended and relocated on the base-256 character set.
public static final char LIST = 59;
@ -22,8 +21,7 @@ public class TypeCode @@ -22,8 +21,7 @@ public class TypeCode
/*
* TypeCodes with embedded values/lengths
*/
public static class EMBEDDED
{
public static class EMBEDDED {
// Positive integers
public static final int INT_POS_START = 0;
public static final int INT_POS_COUNT = 44;

33
app/src/main/java/se/dimovski/rencode/Utils.java

@ -1,7 +1,6 @@ @@ -1,7 +1,6 @@
package se.dimovski.rencode;
public class Utils
{
public class Utils {
// Character Encodings
public final static String UTF_8 = "UTF-8";
public final static String ISO_8859 = "ISO-8859-1";
@ -16,15 +15,12 @@ public class Utils @@ -16,15 +15,12 @@ public class Utils
// Maximum length of integer when written as base 10 string.
public static final int MAX_INT_LENGTH = 64;
private static boolean tokenInRange(int token, int start, int count)
{
private static boolean tokenInRange(int token, int start, int count) {
return start <= token && token < (start + count);
}
public static boolean isNumber(int token)
{
switch (token)
{
public static boolean isNumber(int token) {
switch (token) {
case TypeCode.NUMBER:
case TypeCode.BYTE:
case TypeCode.SHORT:
@ -37,38 +33,31 @@ public class Utils @@ -37,38 +33,31 @@ public class Utils
return isFixedNumber(token);
}
public static boolean isFixedNumber(int token)
{
public static boolean isFixedNumber(int token) {
return isPositiveFixedNumber(token) || isNegativeFixedNumber(token);
}
public static boolean isPositiveFixedNumber(int token)
{
public static boolean isPositiveFixedNumber(int token) {
return tokenInRange(token, TypeCode.EMBEDDED.INT_POS_START, TypeCode.EMBEDDED.INT_POS_COUNT);
}
public static boolean isNegativeFixedNumber(int token)
{
public static boolean isNegativeFixedNumber(int token) {
return tokenInRange(token, TypeCode.EMBEDDED.INT_NEG_START, TypeCode.EMBEDDED.INT_NEG_COUNT);
}
public static boolean isFixedList(int token)
{
public static boolean isFixedList(int token) {
return tokenInRange(token, TypeCode.EMBEDDED.LIST_START, TypeCode.EMBEDDED.LIST_COUNT);
}
public static boolean isFixedDictionary(int token)
{
public static boolean isFixedDictionary(int token) {
return tokenInRange(token, TypeCode.EMBEDDED.DICT_START, TypeCode.EMBEDDED.DICT_COUNT);
}
public static boolean isFixedString(int token)
{
public static boolean isFixedString(int token) {
return tokenInRange(token, TypeCode.EMBEDDED.STR_START, TypeCode.EMBEDDED.STR_COUNT);
}
public static boolean isDigit(int token)
{
public static boolean isDigit(int token) {
return '0' <= token && token <= '9';
}
}

Loading…
Cancel
Save