當前位置: 首頁>>代碼示例>>Java>>正文


Java ByteArrayInputStream.skip方法代碼示例

本文整理匯總了Java中java.io.ByteArrayInputStream.skip方法的典型用法代碼示例。如果您正苦於以下問題:Java ByteArrayInputStream.skip方法的具體用法?Java ByteArrayInputStream.skip怎麽用?Java ByteArrayInputStream.skip使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.io.ByteArrayInputStream的用法示例。


在下文中一共展示了ByteArrayInputStream.skip方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: readMarkAndReset

import java.io.ByteArrayInputStream; //導入方法依賴的package包/類
public void readMarkAndReset(byte[] bytes,int mark) {
    StringBuffer sb = new StringBuffer();
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    bais.mark(mark);

    bais.skip(mark + 1);
    int tmp;
    while ((tmp = bais.read()) != -1) {
        sb.append(Integer.toHexString(tmp));
    }

    System.out.println("越過標記後的字符串");
    System.out.println(sb.toString());

    bais.reset();
    sb.setLength(0);

    int m;
    while ((m = bais.read()) != -1) {
        sb.append(Integer.toHexString(m));
    }

    System.out.println("重置之後的字符串");
    System.out.println(sb.toString());
}
 
開發者ID:byhieg,項目名稱:JavaTutorial,代碼行數:26,代碼來源:ByteArrayInputStreamExample.java

示例2: deserialize

import java.io.ByteArrayInputStream; //導入方法依賴的package包/類
public static final Object deserialize(byte[] bytes) throws Exception {
	if (bytes == null || bytes.length < 4) {
		return new LinkedHashMap<>();
	}

	// Try to deserialize content as Object (type-safe serialization)
	if (bytes[0] == 1) {
		try {
			ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
			bais.skip(1);
			return new ObjectInputStream(bais).readObject();
		} catch (Throwable ignored) {
		}
	}

	// Read content as JSON
	bytes = Arrays.copyOfRange(bytes, 1, bytes.length);
	String json = new String(bytes, StandardCharsets.UTF_8);
	return TreeReaderRegistry.getReader(null).parse(json);
}
 
開發者ID:berkesa,項目名稱:datatree,代碼行數:21,代碼來源:JavaBuiltin.java

示例3: generatePatchAndCheckCtrlEntries

import java.io.ByteArrayInputStream; //導入方法依賴的package包/類
/**
 * Generates a patch from the differences between |oldData| and |newData| and checks that the
 * patch's control data matches |expected|. For the sake of simplicity, assumes that chars are
 * always 1 byte.
 *
 * @param oldData
 * @param newData
 * @param expected The expected control entries in the generated patch
 * @return returns whether the actual control entries in the generated patch match the expected
 *     ones
 */
private boolean generatePatchAndCheckCtrlEntries(
    String oldData, String newData, CtrlEntry[] expected) throws Exception {
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  byte[] oldBytes = oldData.getBytes(Charset.forName("US-ASCII"));
  byte[] newBytes = newData.getBytes(Charset.forName("US-ASCII"));
  RandomAccessObject oldBytesRo = new RandomAccessObject.RandomAccessByteArrayObject(oldBytes);
  RandomAccessObject newBytesRo = new RandomAccessObject.RandomAccessByteArrayObject(newBytes);
  BsDiffPatchWriter.generatePatchWithMatcher(
      oldBytesRo, newBytesRo, new NaiveMatcher(oldBytes, newBytes), outputStream);

  ByteArrayInputStream patchInputStream = new ByteArrayInputStream(outputStream.toByteArray());
  for (CtrlEntry element : expected) {
    if (patchInputStream.available() < 24
        || BsUtil.readFormattedLong(patchInputStream) != element.diffLength
        || BsUtil.readFormattedLong(patchInputStream) != element.extraLength
        || BsUtil.readFormattedLong(patchInputStream) != element.oldOffset) {
      return false;
    }

    patchInputStream.skip(element.diffLength + element.extraLength);
  }

  if (patchInputStream.available() > 0) {
    return false;
  }

  return true;
}
 
開發者ID:lizhangqu,項目名稱:CorePatch,代碼行數:40,代碼來源:BsDiffTest.java

示例4: getArrayOfIntValues

import java.io.ByteArrayInputStream; //導入方法依賴的package包/類
/**
 * 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;
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:38,代碼來源:AttributeClass.java

示例5: getArrayOfStringValues

import java.io.ByteArrayInputStream; //導入方法依賴的package包/類
/**
 * 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;
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:33,代碼來源:AttributeClass.java

示例6: testEncodeReplyResponse

import java.io.ByteArrayInputStream; //導入方法依賴的package包/類
@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() );
}
 
開發者ID:dachengxi,項目名稱:EatDubbo,代碼行數:58,代碼來源:ThriftCodecTest.java

示例7: open

import java.io.ByteArrayInputStream; //導入方法依賴的package包/類
@Override
public void open(long offset) throws ProxyCacheException {
    arrayInputStream = new ByteArrayInputStream(data);
    arrayInputStream.skip(offset);
}
 
開發者ID:Achenglove,項目名稱:AndroidVideoCache,代碼行數:6,代碼來源:ByteArraySource.java

示例8: testEncodeExceptionResponse

import java.io.ByteArrayInputStream; //導入方法依賴的package包/類
@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() );

}
 
開發者ID:zhuxiaolei,項目名稱:dubbo2,代碼行數:58,代碼來源:ThriftCodecTest.java

示例9: readFrom

import java.io.ByteArrayInputStream; //導入方法依賴的package包/類
public static Configuration readFrom(ByteArrayInputStream bis) {
    int len = bis.read();

    int rfAddress = Parser.readRfAddress(bis);
    DeviceType type = DeviceType.fromId(bis.read());

    byte[] unknown = new byte[3];
    bis.read(unknown, 0, unknown.length);

    byte[] serialBytes = new byte[10];
    bis.read(serialBytes, 0, serialBytes.length);
    String serial = new String(serialBytes, UTF_8);

    if (type == DeviceType.THERMOSTAST_PLUS || type == DeviceType.THERMOSTAST) {
        double temperatureComfort = bis.read() / 2.0;
        double temperatureEco = bis.read() / 2.0;
        double temperatureSetpointMax = bis.read() / 2.0;
        double temperatureSetpointMin = bis.read() / 2.0;
        double temperatureOffset = bis.read() / 2.0 - 3.5;
        double temperatureWindowOpen = bis.read() / 2.0;
        int durationWindowOpen = bis.read();
        int durationBoost = bis.read();
        int decalcification = bis.read();
        double valveMaximum = bis.read() * 100/255.0;
        double valveOffset = bis.read() * 100/255.0;

        // TODO decipher the weekly program, always has a length of 182
        bis.skip(bis.available());

        return new ValveConfiguration(type, rfAddress, serial,
                temperatureComfort, temperatureEco, temperatureSetpointMax, temperatureSetpointMin, temperatureOffset,
                temperatureWindowOpen, durationWindowOpen, durationBoost, decalcification, valveMaximum, valveOffset);
    } else if (type == DeviceType.CUBE) {
        // TODO decipher the cube configuration
        bis.skip(bis.available());
    }

    if (bis.available() > 0) {
        String message = String.format(Locale.ROOT, "Device of type [%s] claims a length of [%s] and has [%s] unread bytes", type,
                len, bis.available());
        throw new IllegalStateException(message);
    }

    return new Configuration(type, rfAddress, serial);
}
 
開發者ID:spinscale,項目名稱:maxcube-java,代碼行數:46,代碼來源:Configuration.java

示例10: seek

import java.io.ByteArrayInputStream; //導入方法依賴的package包/類
private void seek(ByteArrayInputStream is, long pos){
    is.reset();
    is.skip(pos);
}
 
開發者ID:Ptrk25,項目名稱:CDN-FX-2.2,代碼行數:5,代碼來源:IconImgInfoReceiver.java

示例11: testEncodeRequest

import java.io.ByteArrayInputStream; //導入方法依賴的package包/類
@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() );

}
 
開發者ID:dachengxi,項目名稱:EatDubbo,代碼行數:64,代碼來源:ThriftCodecTest.java


注:本文中的java.io.ByteArrayInputStream.skip方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。