Back to project page steamchat.
The source code is released under:
Apache License
If you think the Android project steamchat listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.kevelbreh.steamchat.steam.util; /* ww w.j ava2 s. c o m*/ import com.google.protobuf.CodedOutputStream; import com.kevelbreh.steamchat.SteamChat; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.ByteBuffer; /** * Created by kevin on 2014/08/07. */ public class BinaryWriter { CodedOutputStream writer; OutputStream os; ByteArrayOutputStream stream = null; public BinaryWriter(ByteArrayOutputStream stream) { this((OutputStream) stream); this.stream = stream; } public BinaryWriter(int size) { this(new ByteArrayOutputStream(size)); } public BinaryWriter() { this(32); } public BinaryWriter(OutputStream outputStream) { os = outputStream; writer = CodedOutputStream.newInstance(outputStream); } public void write(short data) throws IOException { final ByteBuffer buffer = ByteBuffer.allocate(2); buffer.putShort(data); writeR(buffer); } public void write(int data) throws IOException { final ByteBuffer buffer = ByteBuffer.allocate(4); buffer.putInt(data); writeR(buffer); } public void write(long data) throws IOException { final ByteBuffer buffer = ByteBuffer.allocate(8); buffer.putLong(data); writeR(buffer); } public byte[] toByteArray() { if (stream != null) { return stream.toByteArray(); } return null; } public void writeR(ByteBuffer buffer) throws IOException { for (int i = buffer.capacity() - 1; i >= 0; --i) { write(buffer.get(i)); } } public void write(byte[] data) throws IOException { writer.writeRawBytes(data); writer.flush(); } public void write(byte data) throws IOException { writer.writeRawByte(data); writer.flush(); } public CodedOutputStream getStream() { return writer; } public void flush() { try { os.flush(); } catch (final IOException e) { SteamChat.debug(this, e.toString(), e); } } }