Java Code Examples for java.nio.ByteBuffer

The following are top voted examples for showing how to use java.nio.ByteBuffer. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: monarch   File: HydraLineMapper.java   View source code 7 votes vote down vote up
private String getDiskStoreIdFromInitFile(File dir, String fileName)
    throws FileNotFoundException, IOException {
  FileInputStream fis = new FileInputStream(new File(dir, fileName));
  try {
    byte[] bytes = new byte[1 + 8 + 8];
    fis.read(bytes);
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    // Skip the record type.
    buffer.get();
    long least = buffer.getLong();
    long most = buffer.getLong();
    UUID id = new UUID(most, least);
    return id.toString();
  } finally {
    fis.close();
  }
}
 
Example 2
Project: java-tutorial   File: FileChannelTest.java   View source code 7 votes vote down vote up
public static void main(String[] args) {
    Path file = Paths.get(System.getProperty("user.home") + "/test/myfile.txt");

    try (SeekableByteChannel sbc = Files.newByteChannel(file)) { // |\longremark{newByteChannel默认返回只读的Channel}|
        ByteBuffer buf = ByteBuffer.allocate(10); // |\longremark{allocate创建一个指定字节的ByteBuffer,本例中,sbc这个Channel每次读取10个字节}|

        String encoding = System.getProperty("file.encoding"); // |\longremark{获得当前系统文件编码方式,以便读取文件字节后解码}|
        while (sbc.read(buf) > 0) { // |\longremark{从通道读数据到缓冲区}|
            buf.flip(); // |\longremark{切换缓冲区为读模式}|
            System.out.print(Charset.forName(encoding).decode(buf));
            buf.clear(); // |\longremark{清空缓冲区,准备写入下一轮数据}|
        }
    } catch (IOException x) {
        System.out.println("caught exception: " + x);
    }
}
 
Example 3
Project: OpenJSharp   File: AnnotationParser.java   View source code 6 votes vote down vote up
private static Object parseCharArray(int length,
                              ByteBuffer buf, ConstantPool constPool) {
    char[] result = new char[length];
    boolean typeMismatch = false;
    byte tag = 0;

    for (int i = 0; i < length; i++) {
        tag = buf.get();
        if (tag == 'C') {
            int index = buf.getShort() & 0xFFFF;
            result[i] = (char) constPool.getIntAt(index);
        } else {
            skipMemberValue(tag, buf);
            typeMismatch = true;
        }
    }
    return typeMismatch ? exceptionProxy(tag) : result;
}
 
Example 4
Project: apkfile   File: PackageChunk.java   View source code 6 votes vote down vote up
@Override
protected void init(ByteBuffer buffer) {
    super.init(buffer);
    for (Chunk chunk : getChunks().values()) {
        if (chunk instanceof TypeChunk) {
            TypeChunk typeChunk = (TypeChunk) chunk;
            types.put(typeChunk.getId(), typeChunk);
        } else if (chunk instanceof TypeSpecChunk) {
            TypeSpecChunk typeSpecChunk = (TypeSpecChunk) chunk;
            typeSpecs.put(typeSpecChunk.getId(), typeSpecChunk);
        } else if (chunk instanceof StringPoolChunk) {
            continue;
        } else if (chunk instanceof LibraryChunk) {
            LibraryChunk libraryChunk = (LibraryChunk) chunk;
            libraries.add(libraryChunk);
        } else {
            throw new IllegalStateException(String.format("PackageChunk contains an unexpected chunk: %s", chunk.getClass()));
        }
    }
}
 
Example 5
Project: AI-Powered-Intelligent-Banking-Platform   File: EncodedAudioRecorder.java   View source code 6 votes vote down vote up
/**
 * Save the encoded (output) buffer into the complete encoded recording.
 * TODO: copy directly (without the intermediate byte array)
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void dequeueOutputBuffer(MediaCodec codec, ByteBuffer[] outputBuffers, int index, MediaCodec.BufferInfo info) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        ByteBuffer buffer = outputBuffers[index];
        Log.i("size/remaining: " + info.size + "/" + buffer.remaining());
        if (info.size <= buffer.remaining()) {
            final byte[] bufferCopied = new byte[info.size];
            buffer.get(bufferCopied); // TODO: catch BufferUnderflow
            // TODO: do we need to clear?
            // on N5: always size == remaining(), clearing is not needed
            // on SGS2: remaining decreases until it becomes less than size, which results in BufferUnderflow
            // (but SGS2 records only zeros anyway)
            //buffer.clear();
            codec.releaseOutputBuffer(index, false);
            addEncoded(bufferCopied);
            if (Log.DEBUG) {
                AudioUtils.showSomeBytes("out", bufferCopied);
            }
        } else {
            Log.e("size > remaining");
            codec.releaseOutputBuffer(index, false);
        }
    }
}
 
Example 6
Project: libRtmp   File: NReader.java   View source code 6 votes vote down vote up
@Override
public void run() {
    ByteBuffer buffer = ByteBuffer.allocate(Options.getInstance().bufferSize);
    while (running) {
        try {
            selector.select();
            Set selectedKeys = selector.selectedKeys();
            for (Object selectedKey : selectedKeys) {
                SelectionKey key = (SelectionKey) selectedKey;
                SocketChannel channel = (SocketChannel) key.channel();
                channel.read(buffer);


                //TODO
            }
        } catch (IOException e) {
            Lg.e(Error.READER, e);
        }
    }
}
 
Example 7
Project: GitHub   File: GifBytesTestUtil.java   View source code 6 votes vote down vote up
public static void writeImageDescriptor(ByteBuffer out, int imageLeft, int imageTop,
    int imageWidth, int imageHeight, boolean hasLct, int numColors) {
  verifyRemaining(out, IMAGE_DESCRIPTOR_LENGTH);
  verifyShortValues(imageLeft, imageTop, imageWidth, imageHeight);

  final byte packed;
  if (hasLct) {
    int size = log2(numColors) - 1;
    packed = (byte) (0x80 | size);
  } else {
    packed = 0x00;
  }

  // Image separator
  out.put((byte) 0x2C);
  out.putShort((short) imageLeft).putShort((short) imageTop).putShort((short) imageWidth)
      .putShort((short) imageHeight).put(packed);
}
 
Example 8
Project: dble   File: FastByteOperations.java   View source code 6 votes vote down vote up
public int compare(ByteBuffer buffer1, byte[] buffer2, int offset2, int length2) {
    Object obj1;
    long offset1;
    if (buffer1.hasArray()) {
        obj1 = buffer1.array();
        offset1 = BYTE_ARRAY_BASE_OFFSET + buffer1.arrayOffset();
    } else {
        obj1 = null;
        offset1 = THE_UNSAFE.getLong(buffer1, DIRECT_BUFFER_ADDRESS_OFFSET);
    }
    int length1;
    {
        int position = buffer1.position();
        int limit = buffer1.limit();
        length1 = limit - position;
        offset1 += position;
    }
    return compareTo(obj1, offset1, length1, buffer2, BYTE_ARRAY_BASE_OFFSET + offset2, length2);
}
 
Example 9
Project: MetadataEditor   File: OggVorbisTagWriter.java   View source code 6 votes vote down vote up
/**
 * Calculate checkSum over the Page
 *
 * @param page
 */
private void calculateChecksumOverPage(ByteBuffer page)
{           
    //CRC should be zero before calculating it
    page.putInt(OggPageHeader.FIELD_PAGE_CHECKSUM_POS, 0);

    //Compute CRC over the  page  //TODO shouldnt really use array();
    byte[] crc = OggCRCFactory.computeCRC(page.array());
    for (int i = 0; i < crc.length; i++)
    {
        page.put(OggPageHeader.FIELD_PAGE_CHECKSUM_POS + i, crc[i]);
    }

    //Rewind to start of Page
    page.rewind();
}
 
Example 10
Project: s-store   File: TestStoredProcedureInvocation.java   View source code 6 votes vote down vote up
/**
 * testGetParameterSet
 */
public void testGetParameterSet() throws Exception {
    StoredProcedureInvocation invocation = new StoredProcedureInvocation(CLIENT_HANDLE,
                                                                         TARGET_PROCEDURE,
                                                                         PARAMS);
    byte[] invocation_bytes = FastSerializer.serialize(invocation);
    assertNotNull(invocation_bytes);
    
    ByteBuffer buffer = ByteBuffer.wrap(invocation_bytes);
    ByteBuffer paramsBuffer = StoredProcedureInvocation.getParameterSet(buffer);
    assertNotNull(paramsBuffer);
    
    ParameterSet cloneParams = new ParameterSet();
    FastDeserializer fds = new FastDeserializer(paramsBuffer);
    cloneParams.readExternal(fds);
    
    assertEquals(PARAMS.length, cloneParams.size());
    for (int i = 0; i < PARAMS.length; i++) {
        assertEquals(PARAMS[i], cloneParams.toArray()[i]);
    } 
}
 
Example 11
Project: lams   File: ByteBufferIndexInput.java   View source code 6 votes vote down vote up
/** Returns a sliced view from a set of already-existing buffers: 
 *  the last buffer's limit() will be correct, but
 *  you must deal with offset separately (the first buffer will not be adjusted) */
private ByteBuffer[] buildSlice(ByteBuffer[] buffers, long offset, long length) {
  final long sliceEnd = offset + length;
  
  final int startIndex = (int) (offset >>> chunkSizePower);
  final int endIndex = (int) (sliceEnd >>> chunkSizePower);

  // we always allocate one more slice, the last one may be a 0 byte one
  final ByteBuffer slices[] = new ByteBuffer[endIndex - startIndex + 1];
  
  for (int i = 0; i < slices.length; i++) {
    slices[i] = buffers[startIndex + i].duplicate();
  }

  // set the last buffer's limit for the sliced view.
  slices[slices.length - 1].limit((int) (sliceEnd & chunkSizeMask));
  
  return slices;
}
 
Example 12
Project: openjdk-jdk10   File: ZipPlugin.java   View source code 6 votes vote down vote up
@Override
public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
    in.transformAndCopy((resource) -> {
        ResourcePoolEntry res = resource;
        if (resource.type().equals(ResourcePoolEntry.Type.CLASS_OR_RESOURCE)
                && predicate.test(resource.path())) {
            byte[] compressed;
            compressed = compress(resource.contentBytes());
            res = ResourcePoolManager.newCompressedResource(resource,
                    ByteBuffer.wrap(compressed), getName(), null,
                    ((ResourcePoolImpl)in).getStringTable(), in.byteOrder());
        }
        return res;
    }, out);

    return out.build();
}
 
Example 13
Project: kafka-0.11.0.0-src-with-comment   File: KafkaLZ4Test.java   View source code 6 votes vote down vote up
@Test
public void testArrayBackedBufferSlice() throws IOException {
    byte[] compressed = compressedBytes();

    int sliceOffset = 12;

    ByteBuffer buffer = ByteBuffer.allocate(compressed.length + sliceOffset + 123);
    buffer.position(sliceOffset);
    buffer.put(compressed).flip();
    buffer.position(sliceOffset);

    ByteBuffer slice = buffer.slice();
    testDecompression(slice);

    int offset = 42;
    buffer = ByteBuffer.allocate(compressed.length + sliceOffset + offset);
    buffer.position(sliceOffset + offset);
    buffer.put(compressed).flip();
    buffer.position(sliceOffset);

    slice = buffer.slice();
    slice.position(offset);
    testDecompression(slice);
}
 
Example 14
Project: libRtmp   File: FlvPackerHelper.java   View source code 6 votes vote down vote up
/**
 * 生成flv medadata 数据
 * @param width 视频宽度
 * @param height 视频高度
 * @param fps 视频帧率
 * @param audioRate 音频采样率
 * @param audioSize 音频大小
 * @param isStereo 音频是否为立体声
 * @return byte数组
 */
public static byte[] writeFlvMetaData(int width, int height, int fps, int audioRate, int audioSize, boolean isStereo) {
    AmfString metaDataHeader = new AmfString("onMetaData", false);
    AmfMap amfMap = new AmfMap();
    amfMap.setProperty("width", width);
    amfMap.setProperty("height", height);
    amfMap.setProperty("framerate", fps);
    amfMap.setProperty("videocodecid", FlvVideoCodecID.AVC);
    amfMap.setProperty("audiosamplerate", audioRate);
    amfMap.setProperty("audiosamplesize", audioSize);
    if(isStereo) {
        amfMap.setProperty("stereo", true);
    } else {
        amfMap.setProperty("stereo", false);
    }
    amfMap.setProperty("audiocodecid", FlvAudio.AAC);

    int size = amfMap.getSize() + metaDataHeader.getSize();
    ByteBuffer amfBuffer = ByteBuffer.allocate(size);
    amfBuffer.put(metaDataHeader.getBytes());
    amfBuffer.put(amfMap.getBytes());
    return amfBuffer.array();
}
 
Example 15
Project: lazycat   File: WsWebSocketContainer.java   View source code 6 votes vote down vote up
private static void addHeader(ByteBuffer result, String key, List<String> values) {
	StringBuilder sb = new StringBuilder();

	Iterator<String> iter = values.iterator();
	if (!iter.hasNext()) {
		return;
	}
	sb.append(iter.next());
	while (iter.hasNext()) {
		sb.append(',');
		sb.append(iter.next());
	}

	result.put(key.getBytes(StandardCharsets.ISO_8859_1));
	result.put(": ".getBytes(StandardCharsets.ISO_8859_1));
	result.put(sb.toString().getBytes(StandardCharsets.ISO_8859_1));
	result.put(crlf);
}
 
Example 16
Project: directory-ldap-api   File: CertGenerationRequestDecorator.java   View source code 6 votes vote down vote up
/**
 * Encodes the CertGenerationRequest extended operation.
 * 
 * @return A ByteBuffer that contains the encoded PDU
 * @throws org.apache.directory.api.asn1.EncoderException If anything goes wrong.
 */
/* no qualifier */ByteBuffer encodeInternal() throws EncoderException
{
    // Allocate the bytes buffer.
    ByteBuffer bb = ByteBuffer.allocate( computeLengthInternal() );

    bb.put( UniversalTag.SEQUENCE.getValue() );
    bb.put( TLV.getBytes( requestLength ) );

    BerValue.encode( bb, certGenerationRequest.getTargetDN() );
    BerValue.encode( bb, certGenerationRequest.getIssuerDN() );
    BerValue.encode( bb, certGenerationRequest.getSubjectDN() );
    BerValue.encode( bb, certGenerationRequest.getKeyAlgorithm() );

    return bb;
}
 
Example 17
Project: dble   File: ByteBufferUtil.java   View source code 6 votes vote down vote up
/**
 * Compare two ByteBuffer at specified offsets for length.
 * Compares the non equal bytes as unsigned.
 *
 * @param bytes1  First byte buffer to compare.
 * @param offset1 Position to start the comparison at in the first array.
 * @param bytes2  Second byte buffer to compare.
 * @param offset2 Position to start the comparison at in the second array.
 * @param length  How many bytes to compare?
 * @return -1 if byte1 is less than byte2, 1 if byte2 is less than byte1 or 0 if equal.
 */
public static int compareSubArrays(ByteBuffer bytes1, int offset1, ByteBuffer bytes2, int offset2, int length) {
    if (bytes1 == null) {
        return bytes2 == null ? 0 : -1;
    }
    if (bytes2 == null) {
        return 1;
    }

    assert bytes1.limit() >= offset1 + length : "The first byte array isn't long enough for the specified offset and length.";
    assert bytes2.limit() >= offset2 + length : "The second byte array isn't long enough for the specified offset and length.";
    for (int i = 0; i < length; i++) {
        byte byte1 = bytes1.get(offset1 + i);
        byte byte2 = bytes2.get(offset2 + i);
        // compare non-equal bytes as unsigned
        if (byte1 != byte2) {
            return (byte1 & 0xFF) < (byte2 & 0xFF) ? -1 : 1;
        }
    }
    return 0;
}
 
Example 18
Project: FApkSigner   File: RandomAccessFileDataSource.java   View source code 5 votes vote down vote up
@Override
public ByteBuffer getByteBuffer(long offset, int size) throws IOException {
    if (size < 0) {
        throw new IndexOutOfBoundsException("size: " + size);
    }
    ByteBuffer result = ByteBuffer.allocate(size);
    copyTo(offset, size, result);
    result.flip();
    return result;
}
 
Example 19
Project: fresco_floodlight   File: LLDPOrganizationalTLV.java   View source code 5 votes vote down vote up
@Override
public byte[] serialize() {
    int valueLength = OUI_LENGTH + SUBTYPE_LENGTH + infoString.length;
    value = new byte[valueLength];
    ByteBuffer bb = ByteBuffer.wrap(value);
    bb.put(oui);
    bb.put(subType);
    bb.put(infoString);
    return super.serialize();
}
 
Example 20
Project: jdk8u-jdk   File: WindowsAsynchronousFileChannelImpl.java   View source code 5 votes vote down vote up
ReadTask(ByteBuffer dst,
         int pos,
         int rem,
         long position,
         PendingFuture<Integer,A> result)
{
    this.dst = dst;
    this.pos = pos;
    this.rem = rem;
    this.position = position;
    this.result = result;
}
 
Example 21
Project: hadoop   File: TestAuxServices.java   View source code 5 votes vote down vote up
@Override
public void initializeApplication(ApplicationInitializationContext context) {
  ByteBuffer data = context.getApplicationDataForService();
  assertEquals(idef, data.getChar());
  assertEquals(expected_appId, data.getInt());
  assertEquals(expected_appId, context.getApplicationId().getId());
}
 
Example 22
Project: NoRiskNoFun   File: SessionImplTests.java   View source code 5 votes vote down vote up
@Test(expected = IOException.class)
public void doWriteToSocketDoesNotCatchExceptionFromSocket() throws IOException {

    // given
    when(socketMock.write(ArgumentMatchers.any(ByteBuffer.class))).thenThrow(IOException.class);
    SessionImpl target = new SessionImpl(selectorMock);
    target.write("Hello World!".getBytes());

    // when/then
    target.doWriteToSocket(socketMock);
}
 
Example 23
Project: multiple-dimension-spread   File: OptimizeDoubleColumnBinaryMaker.java   View source code 5 votes vote down vote up
@Override
public void create( final int[] dicIndexArray , final byte[] buffer , final int start , final int length ) throws IOException{
  ByteBuffer wrapBuffer = ByteBuffer.wrap( buffer , start , length );
  for( int index : dicIndexArray ){
    wrapBuffer.putShort( (short)index );
  }
}
 
Example 24
Project: lams   File: SpdyClientProvider.java   View source code 5 votes vote down vote up
private ChannelListener<StreamConnection> createOpenListener(final ClientCallback<ClientConnection> listener, final URI uri, final XnioSsl ssl, final Pool<ByteBuffer> bufferPool, final OptionMap options) {
    return new ChannelListener<StreamConnection>() {
        @Override
        public void handleEvent(StreamConnection connection) {
            handleConnected(connection, listener, uri, ssl, bufferPool, options);
        }
    };
}
 
Example 25
Project: lams   File: FlexBase64.java   View source code 5 votes vote down vote up
private static ByteBuffer decode(ByteBuffer source) throws IOException {
    int len = source.remaining();
    int remainder = len % 4;
    int size = ((len / 4) + (remainder == 0 ? 0 : 4 - remainder)) * 3;
    ByteBuffer buffer = ByteBuffer.allocate(size);
    createDecoder().decode(source, buffer);
    buffer.flip();
    return buffer;
}
 
Example 26
Project: state-channels   File: BlocksController.java   View source code 5 votes vote down vote up
@PostMapping
@SuppressWarnings("all")
public Mono<ResponseEntity<Object>> save(@RequestHeader("X-Block-Id") String blockId, @RequestBody Flux<ByteBuffer> blockFlux) {
    log.info("Block {}", blockId);
    return logsStorage.streamBlock(blockId, blockFlux)
        .map(res -> {
            if (res.getHttpCode() >= 200 && res.getHttpCode() < 300) {
                return noContent();
            } else {
                return status(res.getHttpCode());
            }
        })
        .map(r -> r.build())
        .onErrorResume(t -> Mono.just(status(INTERNAL_SERVER_ERROR).build()));
}
 
Example 27
Project: Quavo   File: ByteBufferUtils.java   View source code 5 votes vote down vote up
/**
 * Gets a unsigned smart from the buffer.
 * 
 * @param buffer The buffer.
 * @return The value.
 */
public static int getUnsignedSmart(ByteBuffer buf) {
	int peek = buf.get(buf.position()) & 0xFF;
	if (peek < 128)
		return buf.get() & 0xFF;
	else
		return (buf.getShort() & 0xFFFF) - 32768;
}
 
Example 28
Project: onekey-proxy-android   File: DnsHeader.java   View source code 5 votes vote down vote up
public static DnsHeader FromBytes(ByteBuffer buffer) {
 DnsHeader header=new DnsHeader(buffer.array(),buffer.arrayOffset()+buffer.position());
 header.ID=buffer.getShort();
 header.Flags=DnsFlags.Parse(buffer.getShort());
 header.QuestionCount=buffer.getShort();
 header.ResourceCount=buffer.getShort();
 header.AResourceCount=buffer.getShort();
 header.EResourceCount=buffer.getShort();
 return header;
}
 
Example 29
Project: sstore-soft   File: SnapshotUtil.java   View source code 5 votes vote down vote up
/**
 * Create a digest for a snapshot containing the time of the snapshot and the list of tables included.
 * The first item in the comma separated list is the time in milliseconds as a string.
 * @param snapshotTime
 * @param path
 * @param nonce
 * @param tables
 * @throws IOException
 */
public static void
    recordSnapshotTableList(
        long snapshotTime,
        String path,
        String nonce,
        List<Table> tables) throws IOException {
    final File f = new File(path, constructDigestFilenameForNonce(nonce));
    if (f.exists()) {
        if (!f.delete()) {
            throw new IOException("Unable to write table list file " + f);
        }
    }
    FileOutputStream fos = new FileOutputStream(f);
    StringWriter sw = new StringWriter();
    sw.append(Long.toString(snapshotTime));
    if (!tables.isEmpty()) {
        sw.append(',');
    }
    for (int ii = 0; ii < tables.size(); ii++) {
        sw.append(tables.get(ii).getTypeName());
        if (!(ii == (tables.size() - 1))) {
            sw.append(',');
        } else {
            sw.append('\n');
        }
    }

    final byte tableListBytes[] = sw.getBuffer().toString().getBytes("UTF-8");
    final CRC32 crc = new CRC32();
    crc.update(tableListBytes);
    ByteBuffer fileBuffer = ByteBuffer.allocate(tableListBytes.length + 4);
    fileBuffer.putInt((int)crc.getValue());
    fileBuffer.put(tableListBytes);
    fileBuffer.flip();
    fos.getChannel().write(fileBuffer);
    fos.getFD().sync();
}
 
Example 30
Project: jdk8u-jdk   File: X11KSC5601_OLD.java   View source code 5 votes vote down vote up
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
    char[] sa = src.array();
    int sp = src.arrayOffset() + src.position();
    int sl = src.arrayOffset() + src.limit();
    byte[] da = dst.array();
    int dp = dst.arrayOffset() + dst.position();
    int dl = dst.arrayOffset() + dst.limit();

    try {
        while (sp < sl) {
            char c = sa[sp];
            if (c <= '\u007f')
                return CoderResult.unmappableForLength(1);
            int ncode = encodeDouble(c);
            if (ncode != 0 && c != '\u0000' ) {
                da[dp++] = (byte) ((ncode  >> 8) & 0x7f);
                da[dp++] = (byte) (ncode & 0x7f);
                sp++;
                continue;
            }
            return CoderResult.unmappableForLength(1);
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 31
Project: jdk8u-jdk   File: SingleByte.java   View source code 5 votes vote down vote up
private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
    char[] sa = src.array();
    int sp = src.arrayOffset() + src.position();
    int sl = src.arrayOffset() + src.limit();

    byte[] da = dst.array();
    int dp = dst.arrayOffset() + dst.position();
    int dl = dst.arrayOffset() + dst.limit();

    CoderResult cr = CoderResult.UNDERFLOW;
    if ((dl - dp) < (sl - sp)) {
        sl = sp + (dl - dp);
        cr = CoderResult.OVERFLOW;
    }

    while (sp < sl) {
        char c = sa[sp];
        int b = encode(c);
        if (b == UNMAPPABLE_ENCODING) {
            if (Character.isSurrogate(c)) {
                if (sgp == null)
                    sgp = new Surrogate.Parser();
                if (sgp.parse(c, sa, sp, sl) < 0)
                    return withResult(sgp.error(), src, sp, dst, dp);
                return withResult(sgp.unmappableResult(), src, sp, dst, dp);
            }
            return withResult(CoderResult.unmappableForLength(1),
                       src, sp, dst, dp);
        }
        da[dp++] = (byte)b;
        sp++;
    }
    return withResult(cr, src, sp, dst, dp);
}
 
Example 32
Project: dubbo2   File: ByteBufferBackedChannelBuffer.java   View source code 5 votes vote down vote up
public ByteBufferBackedChannelBuffer(ByteBuffer buffer) {
    if (buffer == null) {
        throw new NullPointerException("buffer");
    }

    this.buffer = buffer.slice();
    capacity = buffer.remaining();
    writerIndex(capacity);
}
 
Example 33
Project: playTorrent   File: BitEncoder.java   View source code 5 votes vote down vote up
private void encodeByte(@NonNull OutputStream out, @NonNull ByteBuffer buffer) throws IOException {
    byte[] bytes = buffer.array();
    if (bytes.length == 0) {
        throw new IOException("Input bytes is empty");
    }

    String encodeBytes = String.format(Locale.getDefault(), "%d:", bytes.length);
    out.write(encodeBytes.getBytes("UTF-8"));
    out.write(bytes);
}
 
Example 34
Project: burstcoin   File: Attachment.java   View source code 5 votes vote down vote up
@Override
void putMyBytes(ByteBuffer buffer) {
	buffer.putLong(this.amountNQT);
	buffer.putInt(this.deadline);
	buffer.put(Escrow.decisionToByte(this.deadlineAction));
	buffer.put(this.requiredSigners);
	byte totalSigners = (byte) this.signers.size();
	buffer.put(totalSigners);
	for(Long id : this.signers) {
		buffer.putLong(id);
	}
}
 
Example 35
Project: hadoop   File: TestCryptoStreams.java   View source code 5 votes vote down vote up
@Override
public int read(ByteBuffer buf) throws IOException {
  checkStream();
  if (pos < length) {
    int n = (int) Math.min(buf.remaining(), length - pos);
    if (n > 0) {
      buf.put(data, pos, n);
    }
    pos += n;
    return n;
  }
  return -1;
}
 
Example 36
Project: OpenJSharp   File: DatagramChannelImpl.java   View source code 5 votes vote down vote up
public long read(ByteBuffer[] dsts, int offset, int length)
    throws IOException
{
    if ((offset < 0) || (length < 0) || (offset > dsts.length - length))
        throw new IndexOutOfBoundsException();
    synchronized (readLock) {
        synchronized (stateLock) {
            ensureOpen();
            if (!isConnected())
                throw new NotYetConnectedException();
        }
        long n = 0;
        try {
            begin();
            if (!isOpen())
                return 0;
            readerThread = NativeThread.current();
            do {
                n = IOUtil.read(fd, dsts, offset, length, nd);
            } while ((n == IOStatus.INTERRUPTED) && isOpen());
            return IOStatus.normalize(n);
        } finally {
            readerThread = 0;
            end((n > 0) || (n == IOStatus.UNAVAILABLE));
            assert IOStatus.check(n);
        }
    }
}
 
Example 37
Project: sstable-adaptor   File: Murmur3Partitioner.java   View source code 5 votes vote down vote up
private LongToken getToken(ByteBuffer key, long[] hash)
{
    if (key.remaining() == 0)
        return MINIMUM;

    return new LongToken(normalize(hash[0]));
}
 
Example 38
Project: ditb   File: TestThriftServer.java   View source code 5 votes vote down vote up
/**
 * Appends the value to a cell and checks that the cell value is updated properly.
 *
 * @throws Exception
 */
public static void doTestAppend() throws Exception {
  ThriftServerRunner.HBaseHandler handler =
    new ThriftServerRunner.HBaseHandler(UTIL.getConfiguration(),
      UserProvider.instantiate(UTIL.getConfiguration()));
  handler.createTable(tableAname, getColumnDescriptors());
  try {
    List<Mutation> mutations = new ArrayList<Mutation>(1);
    mutations.add(new Mutation(false, columnAname, valueAname, true));
    handler.mutateRow(tableAname, rowAname, mutations, null);

    List<ByteBuffer> columnList = new ArrayList<ByteBuffer>();
    columnList.add(columnAname);
    List<ByteBuffer> valueList = new ArrayList<ByteBuffer>();
    valueList.add(valueBname);

    TAppend append = new TAppend(tableAname, rowAname, columnList, valueList);
    handler.append(append);

    TRowResult rowResult = handler.getRow(tableAname, rowAname, null).get(0);
    assertEquals(rowAname, rowResult.row);
    assertArrayEquals(Bytes.add(valueAname.array(), valueBname.array()),
      rowResult.columns.get(columnAname).value.array());
  } finally {
    handler.disableTable(tableAname);
    handler.deleteTable(tableAname);
  }
}
 
Example 39
Project: Hotspot-master-devp   File: URLEncodedUtils.java   View source code 5 votes vote down vote up
/**
 * Decode/unescape a portion of a URL, to use with the query part ensure {@code plusAsBlank} is true.
 *
 * @param content     the portion to decode
 * @param charset     the charset to use
 * @param plusAsBlank if {@code true}, then convert '+' to space (e.g. for www-url-form-encoded content), otherwise leave as is.
 * @return encoded string
 */
private static String urldecode(
        final String content,
        final Charset charset,
        final boolean plusAsBlank) {
    if (content == null) {
        return null;
    }
    ByteBuffer bb = ByteBuffer.allocate(content.length());
    CharBuffer cb = CharBuffer.wrap(content);
    while (cb.hasRemaining()) {
        char c = cb.get();
        if (c == '%' && cb.remaining() >= 2) {
            char uc = cb.get();
            char lc = cb.get();
            int u = Character.digit(uc, 16);
            int l = Character.digit(lc, 16);
            if (u != -1 && l != -1) {
                bb.put((byte) ((u << 4) + l));
            } else {
                bb.put((byte) '%');
                bb.put((byte) uc);
                bb.put((byte) lc);
            }
        } else if (plusAsBlank && c == '+') {
            bb.put((byte) ' ');
        } else {
            bb.put((byte) c);
        }
    }
    bb.flip();
    return charset.decode(bb).toString();
}
 
Example 40
Project: heifreader   File: ItemPropertiesBox.java   View source code 5 votes vote down vote up
@Override
public void parse(ReadableByteChannel dataSource, ByteBuffer header, long contentSize, BoxParser boxParser) throws IOException {
    initContainer(dataSource, contentSize, boxParser);
    for (AbstractBox box : this.getBoxes(AbstractBox.class)) {
        box.parseDetails();
    }
}