diff --git a/.gitignore b/.gitignore index 748cfc0..c069c83 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build *.iml +.gradle diff --git a/src/main/java/org/telegram/tl/StreamingUtils.java b/src/main/java/org/telegram/tl/StreamingUtils.java index 8d8bd5e..6fda154 100644 --- a/src/main/java/org/telegram/tl/StreamingUtils.java +++ b/src/main/java/org/telegram/tl/StreamingUtils.java @@ -153,6 +153,34 @@ public static void writeTLBytes(byte[] v, OutputStream stream) throws IOExceptio } } + /** + * Writing tl-bytes value + * + * @param v value + * @param stream destination stream + * @throws IOException + */ + public static void writeTLBytes(TLBytes v, OutputStream stream) throws IOException { + int startOffset = 1; + if (v.getLength() >= 254) { + startOffset = 4; + writeByte(254, stream); + writeByte(v.getLength() & 0xFF, stream); + writeByte((v.getLength() >> 8) & 0xFF, stream); + writeByte((v.getLength() >> 16) & 0xFF, stream); + } else { + writeByte(v.getLength(), stream); + } + + writeByteArray(v.getData(), v.getOffset(), v.getLength(), stream); + + int offset = (v.getLength() + startOffset) % 4; + if (offset != 0) { + int offsetCount = 4 - offset; + writeByteArray(new byte[offsetCount], stream); + } + } + /** * Writing tl-object to stream * @@ -401,7 +429,7 @@ public static TLBytes readTLBytes(InputStream stream, TLContext context) throws startOffset = 4; } TLBytes res = context.allocateBytes(count); - readBytes(res.getData(), res.getOffset(), res.getLen(), stream); + readBytes(res.getData(), res.getOffset(), res.getLength(), stream); int offset = (count + startOffset) % 4; if (offset != 0) { diff --git a/src/main/java/org/telegram/tl/TLBytes.java b/src/main/java/org/telegram/tl/TLBytes.java index aaa2850..6a4cd2b 100644 --- a/src/main/java/org/telegram/tl/TLBytes.java +++ b/src/main/java/org/telegram/tl/TLBytes.java @@ -22,7 +22,7 @@ public int getOffset() { return offset; } - public int getLen() { + public int getLength() { return len; } }