bytewriter.java

来自「JRemoteControl is a simple Java&#8482 dr」· Java 代码 · 共 104 行

JAVA
104
字号
/* * JRemCntl - Copyright (C) 2007 Filippo Di Vattimo <fildiv@gmail.com> * See COPYING */package fildiv.jremcntl.common.proto;import java.io.UnsupportedEncodingException;import fildiv.jremcntl.common.core.JRemRuntimeException;public class ByteWriter {	private int offset; 	private byte[] bytes;	public ByteWriter() {		this.bytes = null;	}	public ByteWriter(byte[] data) {		this.bytes = data;	}		public void setBytes(byte[] bytes) {		this.bytes = bytes;		offset = 0;	}		protected void safeOffsetAdd(int offset) {				this.offset += offset;				if (bytes != null && this.offset > bytes.length)			throw new IndexOutOfBoundsException();			}		public void append(byte[] b2) {				if (bytes != null) {			for (int index = 0; index < b2.length; ++index) {				bytes[offset + index] = b2[index];			}		}					safeOffsetAdd(b2.length);	}		public void addShort(short value) {				if (bytes != null) {			bytes[offset] = (byte) ((value >> 8) & 0xff);			bytes[offset + 1] = (byte) (value & 0xff);		}				safeOffsetAdd(JRemAbstractProtocol.LENGHT_SHORT);	}		public void addInt(int value) {				if (bytes != null) {			bytes[offset] = (byte) ((value >> 24) & 0xff);			bytes[offset + 1] = (byte) ((value >> 16) & 0xff);			bytes[offset + 2] = (byte) ((value >> 8) & 0xff);			bytes[offset + 3] = (byte) (value & 0xff);		}				safeOffsetAdd(JRemAbstractProtocol.LENGHT_INT);	}		public void addString(String value) {				// len & value				byte[] bytes = null;						try {			bytes = value.getBytes("UTF-8");		} catch (UnsupportedEncodingException e) {			throw new JRemRuntimeException(e);		}				addInt(value.length());		append(value.getBytes());	}			public byte[] getBytes() {		return bytes;	}		public int getSize() {		return offset;	}		public int getBufferLenght() {		if (bytes == null)			return 0;				return bytes.length;	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?