下面列出了java.io.ByteArrayInputStream#skip ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public byte[] retrieveChunk(String filepath, long offset, int length)
throws ProductException {
try {
String hash = this.hashData(FileUtils.readFileToByteArray(new File(
filepath)));
byte[] retBytes = new byte[length];
byte[] hashBytes = hash.getBytes();
ByteArrayInputStream is = new ByteArrayInputStream(hashBytes);
is.skip(offset);
is.read(retBytes, 0, length);
return retBytes;
} catch (IOException e) {
LOG.log(Level.SEVERE, e.getMessage());
throw new ProductException("Error reading bytes from file: [" + filepath
+ "] MD5: Message: " + e.getMessage());
}
}
/**
* Returns a byte[] containing all the bytes from a ByteArrayInputStream.
* Where possible, this returns the actual array rather than a copy.
*/
private static byte[] exposeByteArrayInputStreamBytes(ByteArrayInputStream bais) {
byte[] buffer;
synchronized (bais) {
byte[] buf;
int pos;
try {
buf = (byte[]) BAIS_BUF.get(bais);
pos = BAIS_POS.getInt(bais);
} catch (IllegalAccessException iae) {
throw new AssertionError(iae);
}
int available = bais.available();
if (pos == 0 && buf.length == available) {
buffer = buf;
} else {
buffer = new byte[available];
System.arraycopy(buf, pos, buffer, 0, available);
}
bais.skip(available);
}
return buffer;
}
/**
* Returns array of int values.
*/
public int[] getArrayOfIntValues() {
byte[] bufArray = (byte[])myValue;
if (bufArray != null) {
//ArrayList valList = new ArrayList();
ByteArrayInputStream bufStream =
new ByteArrayInputStream(bufArray);
int available = bufStream.available();
// total number of values is at the end of the stream
bufStream.mark(available);
bufStream.skip(available-1);
int length = bufStream.read();
bufStream.reset();
int[] valueArray = new int[length];
for (int i = 0; i < length; i++) {
// read length
int valLength = bufStream.read();
if (valLength != 4) {
// invalid data
return null;
}
byte[] bufBytes = new byte[valLength];
bufStream.read(bufBytes, 0, valLength);
valueArray[i] = convertToInt(bufBytes);
}
return valueArray;
}
return null;
}
/**
* Returns array of String values.
*/
public String[] getArrayOfStringValues() {
byte[] bufArray = (byte[])myValue;
if (bufArray != null) {
ByteArrayInputStream bufStream =
new ByteArrayInputStream(bufArray);
int available = bufStream.available();
// total number of values is at the end of the stream
bufStream.mark(available);
bufStream.skip(available-1);
int length = bufStream.read();
bufStream.reset();
String[] valueArray = new String[length];
for (int i = 0; i < length; i++) {
// read length
int valLength = bufStream.read();
byte[] bufBytes = new byte[valLength];
bufStream.read(bufBytes, 0, valLength);
try {
valueArray[i] = new String(bufBytes, "UTF-8");
} catch (java.io.UnsupportedEncodingException uee) {
}
}
return valueArray;
}
return null;
}
/**
* Returns array of int values.
*/
public int[] getArrayOfIntValues() {
byte[] bufArray = (byte[])myValue;
if (bufArray != null) {
//ArrayList valList = new ArrayList();
ByteArrayInputStream bufStream =
new ByteArrayInputStream(bufArray);
int available = bufStream.available();
// total number of values is at the end of the stream
bufStream.mark(available);
bufStream.skip(available-1);
int length = bufStream.read();
bufStream.reset();
int[] valueArray = new int[length];
for (int i = 0; i < length; i++) {
// read length
int valLength = bufStream.read();
if (valLength != 4) {
// invalid data
return null;
}
byte[] bufBytes = new byte[valLength];
bufStream.read(bufBytes, 0, valLength);
valueArray[i] = convertToInt(bufBytes);
}
return valueArray;
}
return null;
}
/**
* Returns array of int values.
*/
public int[] getArrayOfIntValues() {
byte[] bufArray = (byte[])myValue;
if (bufArray != null) {
//ArrayList valList = new ArrayList();
ByteArrayInputStream bufStream =
new ByteArrayInputStream(bufArray);
int available = bufStream.available();
// total number of values is at the end of the stream
bufStream.mark(available);
bufStream.skip(available-1);
int length = bufStream.read();
bufStream.reset();
int[] valueArray = new int[length];
for (int i = 0; i < length; i++) {
// read length
int valLength = bufStream.read();
if (valLength != 4) {
// invalid data
return null;
}
byte[] bufBytes = new byte[valLength];
bufStream.read(bufBytes, 0, valLength);
valueArray[i] = convertToInt(bufBytes);
}
return valueArray;
}
return null;
}
/**
* Returns array of String values.
*/
public String[] getArrayOfStringValues() {
byte[] bufArray = (byte[])myValue;
if (bufArray != null) {
ByteArrayInputStream bufStream =
new ByteArrayInputStream(bufArray);
int available = bufStream.available();
// total number of values is at the end of the stream
bufStream.mark(available);
bufStream.skip(available-1);
int length = bufStream.read();
bufStream.reset();
String[] valueArray = new String[length];
for (int i = 0; i < length; i++) {
// read length
int valLength = bufStream.read();
byte[] bufBytes = new byte[valLength];
bufStream.read(bufBytes, 0, valLength);
try {
valueArray[i] = new String(bufBytes, "UTF-8");
} catch (java.io.UnsupportedEncodingException uee) {
}
}
return valueArray;
}
return null;
}
/**
* Returns array of String values.
*/
public String[] getArrayOfStringValues() {
byte[] bufArray = (byte[])myValue;
if (bufArray != null) {
ByteArrayInputStream bufStream =
new ByteArrayInputStream(bufArray);
int available = bufStream.available();
// total number of values is at the end of the stream
bufStream.mark(available);
bufStream.skip(available-1);
int length = bufStream.read();
bufStream.reset();
String[] valueArray = new String[length];
for (int i = 0; i < length; i++) {
// read length
int valLength = bufStream.read();
byte[] bufBytes = new byte[valLength];
bufStream.read(bufBytes, 0, valLength);
try {
valueArray[i] = new String(bufBytes, "UTF-8");
} catch (java.io.UnsupportedEncodingException uee) {
}
}
return valueArray;
}
return null;
}
/**
* Returns array of int values.
*/
public int[] getArrayOfIntValues() {
byte[] bufArray = (byte[])myValue;
if (bufArray != null) {
//ArrayList valList = new ArrayList();
ByteArrayInputStream bufStream =
new ByteArrayInputStream(bufArray);
int available = bufStream.available();
// total number of values is at the end of the stream
bufStream.mark(available);
bufStream.skip(available-1);
int length = bufStream.read();
bufStream.reset();
int[] valueArray = new int[length];
for (int i = 0; i < length; i++) {
// read length
int valLength = bufStream.read();
if (valLength != 4) {
// invalid data
return null;
}
byte[] bufBytes = new byte[valLength];
bufStream.read(bufBytes, 0, valLength);
valueArray[i] = convertToInt(bufBytes);
}
return valueArray;
}
return null;
}
/**
* Returns array of int values.
*/
public int[] getArrayOfIntValues() {
byte[] bufArray = (byte[])myValue;
if (bufArray != null) {
//ArrayList valList = new ArrayList();
ByteArrayInputStream bufStream =
new ByteArrayInputStream(bufArray);
int available = bufStream.available();
// total number of values is at the end of the stream
bufStream.mark(available);
bufStream.skip(available-1);
int length = bufStream.read();
bufStream.reset();
int[] valueArray = new int[length];
for (int i = 0; i < length; i++) {
// read length
int valLength = bufStream.read();
if (valLength != 4) {
// invalid data
return null;
}
byte[] bufBytes = new byte[valLength];
bufStream.read(bufBytes, 0, valLength);
valueArray[i] = convertToInt(bufBytes);
}
return valueArray;
}
return null;
}
/**
* Returns array of String values.
*/
public String[] getArrayOfStringValues() {
byte[] bufArray = (byte[])myValue;
if (bufArray != null) {
ByteArrayInputStream bufStream =
new ByteArrayInputStream(bufArray);
int available = bufStream.available();
// total number of values is at the end of the stream
bufStream.mark(available);
bufStream.skip(available-1);
int length = bufStream.read();
bufStream.reset();
String[] valueArray = new String[length];
for (int i = 0; i < length; i++) {
// read length
int valLength = bufStream.read();
byte[] bufBytes = new byte[valLength];
bufStream.read(bufBytes, 0, valLength);
try {
valueArray[i] = new String(bufBytes, "UTF-8");
} catch (java.io.UnsupportedEncodingException uee) {
}
}
return valueArray;
}
return null;
}
@Test
public void testEncodeExceptionResponse() throws Exception {
URL url = URL.valueOf(ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.Iface.class.getName());
Channel channel = new MockedChannel(url);
Request request = createRequest();
RpcResult rpcResult = new RpcResult();
String exceptionMessage = "failed";
rpcResult.setException(new RuntimeException(exceptionMessage));
Response response = new Response();
response.setResult(rpcResult);
response.setId(request.getId());
ChannelBuffer bos = ChannelBuffers.dynamicBuffer(1024);
ThriftCodec.RequestData rd = ThriftCodec.RequestData.create(
ThriftCodec.getSeqId(), Demo.Iface.class.getName(), "echoString");
ThriftCodec.cachedRequest.put(request.getId(), rd);
codec.encode(channel, bos, response);
byte[] buf = new byte[bos.writerIndex() - 4];
System.arraycopy(bos.array(), 4, buf, 0, bos.writerIndex() - 4);
ByteArrayInputStream bis = new ByteArrayInputStream(buf);
if (bis.markSupported()) {
bis.mark(0);
}
TIOStreamTransport transport = new TIOStreamTransport(bis);
TBinaryProtocol protocol = new TBinaryProtocol(transport);
Assert.assertEquals(ThriftCodec.MAGIC, protocol.readI16());
Assert.assertEquals(protocol.readI32() + 4, bos.writerIndex());
int headerLength = protocol.readI16();
Assert.assertEquals(ThriftCodec.VERSION, protocol.readByte());
Assert.assertEquals(Demo.Iface.class.getName(), protocol.readString());
Assert.assertEquals(request.getId(), protocol.readI64());
if (bis.markSupported()) {
bis.reset();
bis.skip(headerLength);
}
TMessage message = protocol.readMessageBegin();
Assert.assertEquals("echoString", message.name);
Assert.assertEquals(TMessageType.EXCEPTION, message.type);
Assert.assertEquals(ThriftCodec.getSeqId(), message.seqid);
TApplicationException exception = TApplicationException.read(protocol);
protocol.readMessageEnd();
Assert.assertEquals(exceptionMessage, exception.getMessage());
}
/**
* Returns a object of the right class from a stream of serialized data.
*
* @param data
* the serialized data
* @param offset
* where to start reading in the data-array.
* @return a message of the right class
* @throws IOException
* in case of problems with the message
* @throws InvalidVersionException
* if the message version doesn't match
*/
public Message getMessage(byte[] data, int offset) throws IOException,
InvalidVersionException {
/*
* Check the version of the network protocol.
*/
int networkProtocolVersion = data[offset];
if (networkProtocolVersion < NetConst.NETWORK_PROTOCOL_VERSION_MIN
|| networkProtocolVersion > NetConst.NETWORK_PROTOCOL_VERSION_MAX) {
logger.error("Message has incorrect protocol version (" + networkProtocolVersion + ") expected (" + NetConst.NETWORK_PROTOCOL_VERSION_MIN + " < x < " + NetConst.NETWORK_PROTOCOL_VERSION_MAX + ")");
logger.error("Message is: " + Utility.dumpByteArray(data));
throw new InvalidVersionException(data[offset]);
}
int messageTypeIndex = data[offset + 1];
/*
* Now we check if we have this message class implemented.
*/
if (factoryArray.containsKey(messageTypeIndex)) {
Message tmp = null;
try {
Class<?> messageType = factoryArray.get(messageTypeIndex);
tmp = (Message) messageType.newInstance();
tmp.setProtocolVersion(networkProtocolVersion);
ByteArrayInputStream in = new ByteArrayInputStream(data);
if (offset > 0) {
in.skip(offset);
}
InputSerializer s = new InputSerializer(in);
s.setProtocolVersion(networkProtocolVersion);
tmp.readObject(s);
s.close();
return tmp;
} catch (Exception e) {
logger.error("error in getMessage", e);
throw new IOException(e.getMessage());
}
} else {
logger.warn("Message type [" + messageTypeIndex + "] is not registered in the MessageFactory");
throw new IOException("Message type [" + messageTypeIndex + "] is not registered in the MessageFactory");
}
}
public StreamElement deserialize(String vsensor, byte[] input){
ByteArrayInputStream bais = new ByteArrayInputStream(input);
bais.skip(vsensor.getBytes().length + 2);
return kryo.readObjectOrNull(new Input(bais),StreamElement.class);
}
@Override
public void open(long offset) throws ProxyCacheException {
arrayInputStream = new ByteArrayInputStream(data);
arrayInputStream.skip(offset);
}
@Override
public void open(long offset) throws ProxyCacheException {
arrayInputStream = new ByteArrayInputStream(data);
arrayInputStream.skip(offset);
}
@Override
public void open(long offset) throws ProxyCacheException {
arrayInputStream = new ByteArrayInputStream(data);
arrayInputStream.skip(offset);
}
@Test
public void testEncodeReplyResponse() throws Exception {
URL url = URL.valueOf( ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.Iface.class.getName() );
Channel channel = new MockedChannel( url );
Request request = createRequest();
RpcResult rpcResult = new RpcResult();
rpcResult.setResult( "Hello, World!" );
Response response = new Response();
response.setResult( rpcResult );
response.setId( request.getId() );
ChannelBuffer bos = ChannelBuffers.dynamicBuffer(1024);
ThriftCodec.RequestData rd = ThriftCodec.RequestData.create(
ThriftCodec.getSeqId(), Demo.Iface.class.getName(), "echoString" );
ThriftCodec.cachedRequest.putIfAbsent( request.getId(), rd );
codec.encode( channel, bos, response );
byte[] buf = new byte[bos.writerIndex() - 4];
System.arraycopy( bos.array(), 4, buf, 0, bos.writerIndex() - 4 );
ByteArrayInputStream bis = new ByteArrayInputStream( buf );
if ( bis.markSupported() ) {
bis.mark( 0 );
}
TIOStreamTransport transport = new TIOStreamTransport( bis );
TBinaryProtocol protocol = new TBinaryProtocol( transport );
Assert.assertEquals( ThriftCodec.MAGIC, protocol.readI16() );
Assert.assertEquals( protocol.readI32() + 4, bos.writerIndex() );
int headerLength = protocol.readI16();
Assert.assertEquals( ThriftCodec.VERSION, protocol.readByte() );
Assert.assertEquals( Demo.Iface.class.getName(), protocol.readString() );
Assert.assertEquals( request.getId(), protocol.readI64() );
if ( bis.markSupported() ) {
bis.reset();
bis.skip( headerLength );
}
TMessage message = protocol.readMessageBegin();
Assert.assertEquals( "echoString", message.name );
Assert.assertEquals( TMessageType.REPLY, message.type );
Assert.assertEquals( ThriftCodec.getSeqId(), message.seqid );
Demo.echoString_result result = new Demo.echoString_result();
result.read( protocol );
protocol.readMessageEnd();
Assert.assertEquals( rpcResult.getValue(), result.getSuccess() );
}
@Test
public void testEncodeRequest() throws Exception {
Request request = createRequest();
ChannelBuffer output = ChannelBuffers.dynamicBuffer(1024);
codec.encode(channel, output, request);
byte[] bytes = new byte[output.readableBytes()];
output.readBytes(bytes);
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
TTransport transport = new TIOStreamTransport(bis);
TBinaryProtocol protocol = new TBinaryProtocol(transport);
// frame
byte[] length = new byte[4];
transport.read(length, 0, 4);
if (bis.markSupported()) {
bis.mark(0);
}
// magic
Assert.assertEquals(ThriftCodec.MAGIC, protocol.readI16());
// message length
int messageLength = protocol.readI32();
Assert.assertEquals(messageLength + 4, bytes.length);
// header length
short headerLength = protocol.readI16();
// version
Assert.assertEquals(ThriftCodec.VERSION, protocol.readByte());
// service name
Assert.assertEquals(Demo.Iface.class.getName(), protocol.readString());
// dubbo request id
Assert.assertEquals(request.getId(), protocol.readI64());
// test message header length
if (bis.markSupported()) {
bis.reset();
bis.skip(headerLength);
}
TMessage message = protocol.readMessageBegin();
Demo.echoString_args args = new Demo.echoString_args();
args.read(protocol);
protocol.readMessageEnd();
Assert.assertEquals("echoString", message.name);
Assert.assertEquals(TMessageType.CALL, message.type);
Assert.assertEquals("Hello, World!", args.getArg());
}
@Test
public void testEncodeRequest() throws Exception {
Request request = createRequest();
ChannelBuffer output = ChannelBuffers.dynamicBuffer(1024);
codec.encode( channel, output, request );
byte[] bytes = new byte[output.readableBytes()];
output.readBytes(bytes);
ByteArrayInputStream bis = new ByteArrayInputStream( bytes );
TTransport transport = new TIOStreamTransport( bis );
TBinaryProtocol protocol = new TBinaryProtocol( transport );
// frame
byte[] length = new byte[4];
transport.read( length, 0, 4 );
if ( bis.markSupported() ) {
bis.mark( 0 );
}
// magic
Assert.assertEquals( ThriftCodec.MAGIC, protocol.readI16() );
// message length
int messageLength = protocol.readI32();
Assert.assertEquals( messageLength + 4, bytes.length );
// header length
short headerLength = protocol.readI16();
// version
Assert.assertEquals( ThriftCodec.VERSION, protocol.readByte() );
// service name
Assert.assertEquals( Demo.Iface.class.getName(), protocol.readString() );
// dubbo request id
Assert.assertEquals( request.getId(), protocol.readI64() );
// test message header length
if ( bis.markSupported() ) {
bis.reset();
bis.skip( headerLength );
}
TMessage message = protocol.readMessageBegin();
Demo.echoString_args args = new Demo.echoString_args();
args.read( protocol );
protocol.readMessageEnd();
Assert.assertEquals( "echoString", message.name );
Assert.assertEquals( TMessageType.CALL, message.type );
Assert.assertEquals( "Hello, World!", args.getArg() );
}