bytereader.java

来自「一个基于J2ME技术的程序,使你可以远程控制装有服务端的PC.」· Java 代码 · 共 79 行

JAVA
79
字号
/* * 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 ByteReader {	private byte[] bytes;	private int offset;	public ByteReader() {	}			public ByteReader(byte[] bytes) {		setBytes(bytes);	}		public void setBytes(byte[] bytes) {		if (bytes == null)			throw new IllegalArgumentException();		this.bytes = bytes;		offset = 0;	}		public byte[] getBytes() {		return bytes;	}		protected void safeOffsetAdd(int offset) {				this.offset += offset;				if(this.offset > bytes.length)			throw new IndexOutOfBoundsException();	}		public String getString() {		int size = getInt();				String str = "";				try {			str = new String(bytes, offset, size, "UTF-8");		} catch (UnsupportedEncodingException e) {			throw new JRemRuntimeException(e);		}		safeOffsetAdd(size);				return str;	}	public int getInt() {		int value = (((bytes[offset] & 0xff) << 24)               | ((bytes[offset + 1] & 0xff) << 16)               | ((bytes[offset + 2] & 0xff) << 8)                | (bytes[offset + 3] & 0xff));						safeOffsetAdd(JRemAbstractProtocol.LENGHT_INT);				return value;	}	public short getShort() {		short value = (short) (((bytes[offset + 0] & 0xff) << 8) 	           | (bytes[offset + 1] & 0xff));						safeOffsetAdd(JRemAbstractProtocol.LENGHT_SHORT);				return value;	}}

⌨️ 快捷键说明

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