@Override protected void processRemaining(ByteBuffer buffer) { b += buffer.remaining(); for (int i = 0; buffer.hasRemaining(); i += 8) { finalM ^= (buffer.get() & 0xFFL) << i; } }
@Override public int read() { if (!byteBuffer.hasRemaining()) { return -1; } return byteBuffer.get(); }
@Override public int read(ByteBuffer dst) throws IOException { int read=0; while(dst.hasRemaining() && bytes_to_read.hasRemaining()) { dst.put(bytes_to_read.get()); read++; } return read > 0? read : closed? -1 : read; }
/** * Returns the checksum of all but the first 12 bytes of {@code dex}. */ public int computeChecksum() throws IOException { Adler32 adler32 = new Adler32(); byte[] buffer = new byte[8192]; ByteBuffer data = this.data.duplicate(); // positioned ByteBuffers aren't thread safe data.limit(data.capacity()); data.position(CHECKSUM_OFFSET + SizeOf.CHECKSUM); while (data.hasRemaining()) { int count = Math.min(buffer.length, data.remaining()); data.get(buffer, 0, count); adler32.update(buffer, 0, count); } return (int) adler32.getValue(); }
public static boolean readAndFlip( ReadableByteChannel channel, ByteBuffer buffer, int bytes ) throws IOException { buffer.clear(); buffer.limit( bytes ); while ( buffer.hasRemaining() ) { int read = channel.read( buffer ); if ( read == -1 ) { return false; } } buffer.flip(); return true; }
private int copy(final ByteBuffer src, final ByteBuffer dst) { if (src != null && src.hasRemaining()) { final int bytesToCopy = Math.min(dst.remaining(), src.remaining()); if (bytesToCopy < 1) { return bytesToCopy; } final byte[] buff = new byte[bytesToCopy]; src.get(buff); dst.put(buff); return bytesToCopy; } return 0; }
private void tlsWrite(ByteBuffer buf) throws IOException { ByteBuffer tlsBuffer; ByteBuffer tlsOutput; do { // TODO Consider optimizing by not creating new instances each time tlsBuffer = ByteBuffer.allocate(Math.min(buf.remaining(), wrapper.getAppBuffSize())); tlsOutput = ByteBuffer.allocate(wrapper.getNetBuffSize()); while (tlsBuffer.hasRemaining() && buf.hasRemaining()) { tlsBuffer.put(buf.get()); } tlsBuffer.flip(); wrapper.wrap(tlsBuffer, tlsOutput); tlsOutput.flip(); writeToSocket(tlsOutput); tlsOutput.clear(); } while (buf.hasRemaining()); }
/** * Calculates hash code for the given byte buffers contents. Compatible with {@link Arrays#hashCode(byte[])} * with the same content. Does not change buffers positions. * * @param bufs Byte buffers. * @return Hash code. */ public static int hashCode(ByteBuffer... bufs) { int res = 1; for (ByteBuffer buf : bufs) { int pos = buf.position(); while (buf.hasRemaining()) res = 31 * res + buf.get(); buf.position(pos); } return res; }
boolean mask = role == Role.CLIENT; int sizebytes = getSizeBytes(mes); ByteBuffer buf = ByteBuffer.allocate( 1 + ( sizebytes > 1 ? sizebytes + 1 : sizebytes ) + ( mask ? 4 : 0 ) + mes.remaining() ); byte optcode = fromOpcode( framedata.getOpcode() ); byte one = ( byte ) ( framedata.isFin() ? -128 : 0 ); one |= optcode; buf.put( one ); byte[] payloadlengthbytes = toByteArray( mes.remaining(), sizebytes ); assert ( payloadlengthbytes.length == sizebytes ); buf.put( ( byte ) ( payloadlengthbytes[0] | getMaskByte(mask) ) ); } else if( sizebytes == 2 ) { buf.put( ( byte ) ( ( byte ) 126 | getMaskByte(mask))); buf.put( payloadlengthbytes ); } else if( sizebytes == 8 ) { maskkey.putInt( reuseableRandom.nextInt() ); buf.put( maskkey.array() ); for( int i = 0; mes.hasRemaining(); i++ ) { buf.put( ( byte ) ( mes.get() ^ maskkey.get( i % 4 ) ) ); buf.put( mes ); mes.flip(); assert ( buf.remaining() == 0 ) : buf.remaining(); buf.flip(); return buf;
@Override public Hasher putBytes(ByteBuffer buffer) { ByteOrder bo = buffer.order(); buffer.order(ByteOrder.LITTLE_ENDIAN); while (buffer.remaining() >= 4) { putInt(buffer.getInt()); } while (buffer.hasRemaining()) { putByte(buffer.get()); } buffer.order(bo); return this; }
private void read(ByteBuffer buffer) throws IOException { buffer.clear(); while (buffer.hasRemaining()) if (channel.read(buffer) == -1) throw new EOFException(); buffer.flip(); }
public static ChannelBuffer wrappedBuffer(ByteBuffer buffer) { if (!buffer.hasRemaining()) { return EMPTY_BUFFER; } if (buffer.hasArray()) { return wrappedBuffer(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); } else { return new ByteBufferBackedChannelBuffer(buffer); } }
public void put(byte data) { if (lineByteBuffer.hasRemaining()) { lineByteBuffer.put(data); } else { final ByteBuffer newLineByteBuffer = ByteBuffer.allocate(lineByteBuffer.capacity() + bufferSize); lineByteBuffer.flip(); newLineByteBuffer.put(lineByteBuffer); newLineByteBuffer.put(data); this.lineByteBuffer = newLineByteBuffer; } }
private void convertToDenseStorage() { ByteBuffer tmpBuffer = ByteBuffer.allocate(getNumBytesForDenseStorage()); // put header setVersion(tmpBuffer); setRegisterOffset(tmpBuffer, getRegisterOffset()); setNumNonZeroRegisters(tmpBuffer, getNumNonZeroRegisters()); setMaxOverflowValue(tmpBuffer, getMaxOverflowValue()); setMaxOverflowRegister(tmpBuffer, getMaxOverflowRegister()); storageBuffer.position(getPayloadBytePosition()); tmpBuffer.position(getPayloadBytePosition(tmpBuffer)); // put payload while (storageBuffer.hasRemaining()) { tmpBuffer.put(storageBuffer.getShort(), storageBuffer.get()); } tmpBuffer.rewind(); storageBuffer = tmpBuffer; initPosition = 0; }
/** * Zero a buffer. Ensures that any potentially sensitive information in the buffer is * overwritten. * * @param buffer the buffer */ public static void zero(ByteBuffer buffer) { buffer.clear(); while (buffer.remaining() >= 8) { buffer.putLong(0L); } while (buffer.hasRemaining()) { buffer.put((byte) 0); } buffer.clear(); }
private boolean writeToSocket () throws IOException { SocketChannel socketChannel = this.socketChannel; if (socketChannel == null) throw new SocketException("Connection is closed."); ByteBuffer buffer = writeBuffer; buffer.flip(); while (buffer.hasRemaining()) { if (bufferPositionFix) { buffer.compact(); buffer.flip(); } if (socketChannel.write(buffer) == 0) break; } buffer.compact(); return buffer.position() == 0; }
private int parse_mdia(ByteBuffer data) { while (data.hasRemaining()) { int end = data.position() + data.getInt(); if (data.getInt() == ATOM_MDHD) { byte version = data.get(); data.position(data.position() + 3 + ((version == 0 ? 4 : 8) * 2)); return data.getInt(); } data.position(end); } return 0;// this NEVER should happen }
@Override public VSizeColumnarInts get(int index) { if (index >= size) { throw new IAE("Index[%d] >= size[%d]", index, size); } ByteBuffer myBuffer = theBuffer.asReadOnlyBuffer(); int startOffset = 0; int endOffset; if (index == 0) { endOffset = myBuffer.getInt(); } else { myBuffer.position(myBuffer.position() + ((index - 1) * Integer.BYTES)); startOffset = myBuffer.getInt(); endOffset = myBuffer.getInt(); } myBuffer.position(valuesOffset + startOffset); myBuffer.limit(myBuffer.position() + (endOffset - startOffset) + bufferBytes); return myBuffer.hasRemaining() ? new VSizeColumnarInts(myBuffer, numBytes) : null; }
private boolean reportSlaveMaxOffset(final long maxOffset) { this.reportOffset.position(0); this.reportOffset.limit(8); this.reportOffset.putLong(maxOffset); this.reportOffset.position(0); this.reportOffset.limit(8); for (int i = 0; i < 3 && this.reportOffset.hasRemaining(); i++) { try { this.socketChannel.write(this.reportOffset); } catch (IOException e) { log.error(this.getServiceName() + "reportSlaveMaxOffset this.socketChannel.write exception", e); return false; } } return !this.reportOffset.hasRemaining(); }
@Override public void addValue(int val) throws IOException { if (endBuffer == null) { throw new IllegalStateException("written out already"); } if (!endBuffer.hasRemaining()) { endBuffer.rewind(); flattener.write(endBuffer); endBuffer.clear(); } intBuffer.putInt(0, val); if (isBigEndian) { endBuffer.put(intBuffer.array(), Integer.BYTES - numBytes, numBytes); } else { endBuffer.put(intBuffer.array(), 0, numBytes); } numInserted++; }