package org.kne.io;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;

public class VarDataOutputStream extends DataOutputStream {

	public VarDataOutputStream(OutputStream out) {
		super(out);
	}
	int cache=0;
	public void writeVarInt_add(int input)throws IOException{
		writeVarInt (input-cache);
		cache=input;
	}
	public void writeVarBytes(byte[]b) throws IOException {
		writeVarInt(b.length);
		write(b);
	}
	public void writeVarUTF8(String str) throws IOException {
		writeVarBytes(str.getBytes(Charset.forName("UTF-8")));
	}
	public void writeVarInt(int input) throws IOException {
		while ((input & -128) != 0) {
			this.out.write(input & 127 | 128);
			input >>>= 7;
		}

		this.out.write(input);
	}


	public void writeVarLong(long value) throws IOException {
		while ((value & -128L) != 0L) {
			this.writeByte((int) (value & 127L) | 128);
			value >>>= 7;
		}

		this.writeByte((int) value);
	}
}
