Nagram/TMessagesProj/src/main/java/org/telegram/messenger/SerializedData.java

317 lines
8.0 KiB
Java
Raw Normal View History

2013-10-25 15:19:00 +00:00
/*
2013-12-20 19:25:49 +00:00
* This is the source code of Telegram for Android v. 1.3.2.
2013-10-25 15:19:00 +00:00
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
* Copyright Nikolai Kudashov, 2013.
*/
package org.telegram.messenger;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class SerializedData {
protected boolean isOut = true;
private ByteArrayOutputStream outbuf;
private DataOutputStream out;
private ByteArrayInputStream inbuf;
private DataInputStream in;
public SerializedData() {
outbuf = new ByteArrayOutputStream();
out = new DataOutputStream(outbuf);
}
2013-12-20 19:25:49 +00:00
public SerializedData(int size) {
outbuf = new ByteArrayOutputStream(size);
out = new DataOutputStream(outbuf);
}
2013-10-25 15:19:00 +00:00
public SerializedData(byte[] data){
isOut = false;
inbuf = new ByteArrayInputStream(data);
in = new DataInputStream(inbuf);
}
public SerializedData(File file) throws IOException {
FileInputStream is = new FileInputStream(file);
byte[] data = new byte[(int)file.length()];
new DataInputStream(is).readFully(data);
is.close();
isOut = false;
inbuf = new ByteArrayInputStream(data);
in = new DataInputStream(inbuf);
}
public void writeInt32(int x){
writeInt32(x, out);
}
protected void writeInt32(int x, DataOutputStream out){
try {
for(int i = 0; i < 4; i++){
out.write(x >> (i * 8));
}
} catch(IOException gfdsgd) {
2013-12-20 19:25:49 +00:00
FileLog.e("tmessages", "write int32 error");
2013-10-25 15:19:00 +00:00
}
}
public void writeInt64(long i) {
writeInt64(i, out);
}
protected void writeInt64(long x, DataOutputStream out){
try {
for(int i = 0; i < 8; i++){
out.write((int)(x >> (i * 8)));
}
} catch(IOException gfdsgd) {
2013-12-20 19:25:49 +00:00
FileLog.e("tmessages", "write int64 error");
2013-10-25 15:19:00 +00:00
}
}
public boolean readBool() {
int consructor = readInt32();
if (consructor == 0x997275b5) {
return true;
} else if (consructor == 0xbc799737) {
return false;
}
2013-12-20 19:25:49 +00:00
FileLog.e("tmessages", "Not bool value!");
2013-10-25 15:19:00 +00:00
return false;
}
public void writeBool(boolean value) {
if (value) {
writeInt32(0x997275b5);
} else {
writeInt32(0xbc799737);
}
}
2013-12-20 19:25:49 +00:00
public int readInt32() {
return readInt32(null);
}
public int readInt32(boolean[] error) {
2013-10-25 15:19:00 +00:00
try {
int i = 0;
for(int j = 0; j < 4; j++){
i |= (in.read() << (j * 8));
}
2013-12-20 19:25:49 +00:00
if (error != null) {
error[0] = false;
}
2013-10-25 15:19:00 +00:00
return i;
} catch(IOException x) {
2013-12-20 19:25:49 +00:00
if (error != null) {
error[0] = true;
}
FileLog.e("tmessages", "read int32 error");
2013-10-25 15:19:00 +00:00
}
return 0;
}
2013-12-20 19:25:49 +00:00
public long readInt64() {
return readInt64(null);
}
public long readInt64(boolean[] error) {
2013-10-25 15:19:00 +00:00
try {
long i = 0;
for(int j = 0; j < 8; j++){
i |= ((long)in.read() << (j * 8));
}
2013-12-20 19:25:49 +00:00
if (error != null) {
error[0] = false;
}
2013-10-25 15:19:00 +00:00
return i;
} catch(IOException x) {
2013-12-20 19:25:49 +00:00
if (error != null) {
error[0] = true;
}
FileLog.e("tmessages", "read int64 error");
2013-10-25 15:19:00 +00:00
}
return 0;
}
public void writeRaw(byte[] b){
try {
out.write(b);
} catch(Exception x) {
2013-12-20 19:25:49 +00:00
FileLog.e("tmessages", "write raw error");
2013-10-25 15:19:00 +00:00
}
}
public void writeRaw(byte[] b, int offset, int count) {
try {
out.write(b, offset, count);
} catch(Exception x) {
2013-12-20 19:25:49 +00:00
FileLog.e("tmessages", "write raw error");
2013-10-25 15:19:00 +00:00
}
}
public void writeByte(int i) {
try {
out.writeByte((byte)i);
} catch (Exception e) {
2013-12-20 19:25:49 +00:00
FileLog.e("tmessages", "write byte error");
2013-10-25 15:19:00 +00:00
}
}
public void writeByte(byte b) {
try {
out.writeByte(b);
} catch (Exception e) {
2013-12-20 19:25:49 +00:00
FileLog.e("tmessages", "write byte error");
2013-10-25 15:19:00 +00:00
}
}
public void readRaw(byte[] b){
try {
in.read(b);
} catch(Exception x) {
2013-12-20 19:25:49 +00:00
FileLog.e("tmessages", "read raw error");
2013-10-25 15:19:00 +00:00
}
}
public byte[] readData(int count) {
byte[] arr = new byte[count];
readRaw(arr);
return arr;
}
public String readString(){
try {
int sl = 1;
int l = in.read();
if(l >= 254){
l = in.read() | (in.read() << 8) | (in.read() << 16);
sl = 4;
}
byte[] b = new byte[l];
in.read(b);
int i=sl;
while((l + i) % 4 != 0) {
in.read();
i++;
}
return new String(b, "UTF-8");
} catch(Exception x) {
2013-12-20 19:25:49 +00:00
FileLog.e("tmessages", "read string error");
2013-10-25 15:19:00 +00:00
}
return null;
}
public byte[] readByteArray() {
try {
int sl = 1;
int l = in.read();
if (l >= 254){
l = in.read() | (in.read() << 8) | (in.read() << 16);
sl = 4;
}
byte[] b = new byte[l];
in.read(b);
int i = sl;
while((l + i) % 4 != 0){
in.read();
i++;
}
return b;
} catch(Exception x) {
2013-12-20 19:25:49 +00:00
FileLog.e("tmessages", "read byte array error");
2013-10-25 15:19:00 +00:00
}
return null;
}
public void writeByteArray(byte[] b){
try {
if (b.length <= 253){
out.write(b.length);
} else {
out.write(254);
out.write(b.length);
out.write(b.length >> 8);
out.write(b.length >> 16);
}
out.write(b);
int i = b.length <= 253 ? 1 : 4;
while((b.length + i) % 4 != 0){
out.write(0);
i++;
}
2013-12-20 19:25:49 +00:00
} catch(Exception x) {
FileLog.e("tmessages", "write byte array error");
2013-10-25 15:19:00 +00:00
}
}
public void writeString(String s){
try {
writeByteArray(s.getBytes("UTF-8"));
} catch(Exception x) {
2013-12-20 19:25:49 +00:00
FileLog.e("tmessages", "write string error");
2013-10-25 15:19:00 +00:00
}
}
public void writeByteArray(byte[] b, int offset, int count) {
try {
if(count <= 253){
out.write(count);
} else {
out.write(254);
out.write(count);
out.write(count >> 8);
out.write(count >> 16);
}
out.write(b, offset, count);
int i = count <= 253 ? 1 : 4;
while ((count + i) % 4 != 0){
out.write(0);
i++;
}
} catch(Exception x) {
2013-12-20 19:25:49 +00:00
FileLog.e("tmessages", "write byte array error");
2013-10-25 15:19:00 +00:00
}
}
2013-12-20 19:25:49 +00:00
public double readDouble() {
2013-10-25 15:19:00 +00:00
try {
return Double.longBitsToDouble(readInt64());
} catch(Exception x) {
2013-12-20 19:25:49 +00:00
FileLog.e("tmessages", "read double error");
2013-10-25 15:19:00 +00:00
}
return 0;
}
public void writeDouble(double d){
try {
writeInt64(Double.doubleToRawLongBits(d));
} catch(Exception x) {
2013-12-20 19:25:49 +00:00
FileLog.e("tmessages", "write double error");
2013-10-25 15:19:00 +00:00
}
}
public int length() {
return isOut ? outbuf.size() : inbuf.available();
}
protected void set(byte[] newData) {
isOut = false;
inbuf = new ByteArrayInputStream(newData);
in = new DataInputStream(inbuf);
}
public byte[] toByteArray() {
return outbuf.toByteArray();
}
}