Example usage for java.nio ByteBuffer get

List of usage examples for java.nio ByteBuffer get

Introduction

In this page you can find the example usage for java.nio ByteBuffer get.

Prototype

public abstract byte get();

Source Link

Document

Returns the byte at the current position and increases the position by 1.

Usage

From source file:com.mcxiaoke.next.http.util.URLUtils.java

private static String urlEncode(final String content, final Charset charset, final BitSet safechars,
        final boolean blankAsPlus) {
    if (content == null) {
        return null;
    }//from   w  ww  .  j  a  v a 2s  .  c om
    final StringBuilder buf = new StringBuilder();
    final ByteBuffer bb = charset.encode(content);
    while (bb.hasRemaining()) {
        final int b = bb.get() & 0xff;
        if (safechars.get(b)) {
            buf.append((char) b);
        } else if (blankAsPlus && b == ' ') {
            buf.append('+');
        } else {
            buf.append("%");
            final char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, RADIX));
            final char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, RADIX));
            buf.append(hex1);
            buf.append(hex2);
        }
    }
    return buf.toString();
}

From source file:au.org.ala.layers.grid.GridCacheBuilder.java

static void nextRowOfFloats(float[] row, String datatype, boolean byteOrderLSB, int ncols, RandomAccessFile raf,
        byte[] b, float noDataValue) throws IOException {
    int size = 4;
    if (datatype.charAt(0) == 'U') {
        size = 1;/*from w w w .j a  va  2s. c  om*/
    } else if (datatype.charAt(0) == 'B') {
        size = 1;
    } else if (datatype.charAt(0) == 'S') {
        size = 2;
    } else if (datatype.charAt(0) == 'I') {
        size = 4;
    } else if (datatype.charAt(0) == 'L') {
        size = 8;
    } else if (datatype.charAt(0) == 'F') {
        size = 4;
    } else if (datatype.charAt(0) == 'D') {
        size = 8;
    }

    raf.read(b, 0, size * ncols);
    ByteBuffer bb = ByteBuffer.wrap(b);
    if (byteOrderLSB) {
        bb.order(ByteOrder.LITTLE_ENDIAN);
    } else {
        bb.order(ByteOrder.BIG_ENDIAN);
    }

    int i;
    int length = ncols;
    if (datatype.charAt(0) == 'U') {
        for (i = 0; i < length; i++) {
            float ret = bb.get();
            if (ret < 0) {
                ret += 256;
            }
            row[i] = ret;
        }
    } else if (datatype.charAt(0) == 'B') {

        for (i = 0; i < length; i++) {
            row[i] = (float) bb.get();
        }
    } else if (datatype.charAt(0) == 'S') {
        for (i = 0; i < length; i++) {
            row[i] = (float) bb.getShort();
        }
    } else if (datatype.charAt(0) == 'I') {
        for (i = 0; i < length; i++) {
            row[i] = (float) bb.getInt();
        }
    } else if (datatype.charAt(0) == 'L') {
        for (i = 0; i < length; i++) {
            row[i] = (float) bb.getLong();
        }
    } else if (datatype.charAt(0) == 'F') {
        for (i = 0; i < length; i++) {
            row[i] = (float) bb.getFloat();
        }
    } else if (datatype.charAt(0) == 'D') {
        for (i = 0; i < length; i++) {
            row[i] = (float) bb.getDouble();
        }
    } else {
        logger.info("UNKNOWN TYPE: " + datatype);
    }

    for (i = 0; i < length; i++) {
        if (row[i] == noDataValue) {
            row[i] = Float.NaN;
        }
    }
}

From source file:de.metalcon.imageServer.protocol.CreateRequestTest.java

/**
 * compare two input streams//from w  ww  . j a va2 s . c o  m
 * 
 * @param stream1
 *            first input stream
 * @param stream2
 *            second input stream
 * @return true - if the two streams does contain the same content<br>
 *         false - otherwise
 * @throws IOException
 *             if IO errors occurred
 */
private static boolean compareInputStreams(final InputStream stream1, final InputStream stream2)
        throws IOException {
    final ReadableByteChannel channel1 = Channels.newChannel(stream1);
    final ReadableByteChannel channel2 = Channels.newChannel(stream2);
    final ByteBuffer buffer1 = ByteBuffer.allocateDirect(4096);
    final ByteBuffer buffer2 = ByteBuffer.allocateDirect(4096);

    try {
        while (true) {

            int n1 = channel1.read(buffer1);
            int n2 = channel2.read(buffer2);

            if ((n1 == -1) || (n2 == -1)) {
                return n1 == n2;
            }

            buffer1.flip();
            buffer2.flip();

            for (int i = 0; i < Math.min(n1, n2); i++) {
                if (buffer1.get() != buffer2.get()) {
                    return false;
                }
            }

            buffer1.compact();
            buffer2.compact();
        }

    } finally {
        if (stream1 != null) {
            stream1.close();
        }
        if (stream2 != null) {
            stream2.close();
        }
    }
}

From source file:com.glaf.core.util.ByteBufferUtils.java

public static InputStream inputStream(ByteBuffer bytes) {
    final ByteBuffer copy = bytes.duplicate();

    return new InputStream() {
        public int read() throws IOException {
            if (!copy.hasRemaining())
                return -1;

            return copy.get() & 0xFF;
        }/*from   w w  w .  j  a  va  2 s .  c o m*/

        @Override
        public int read(byte[] bytes, int off, int len) throws IOException {
            if (!copy.hasRemaining())
                return -1;

            len = Math.min(len, copy.remaining());
            copy.get(bytes, off, len);
            return len;
        }

        @Override
        public int available() throws IOException {
            return copy.remaining();
        }
    };
}

From source file:com.healthmarketscience.jackcess.impl.OleUtil.java

private static String readZeroTermStr(ByteBuffer bb) {
    int off = bb.position();
    while (bb.hasRemaining()) {
        byte b = bb.get();
        if (b == 0) {
            break;
        }//  w  w  w .j  av  a2  s.  c  om
    }
    int len = bb.position() - off;
    return readStr(bb, off, len);
}

From source file:com.healthmarketscience.jackcess.impl.UsageMap.java

/**
 * @param database database that contains this usage map
 * @param buf buffer which contains the usage map row info
 * @return Either an InlineUsageMap or a ReferenceUsageMap, depending on
 *         which type of map is found//from  w  w w.ja v a2  s. c o m
 */
public static UsageMap read(DatabaseImpl database, ByteBuffer buf, boolean assumeOutOfRangeBitsOn)
        throws IOException {
    int umapRowNum = buf.get();
    int umapPageNum = ByteUtil.get3ByteInt(buf);
    return read(database, umapPageNum, umapRowNum, false);
}

From source file:com.offbynull.portmapper.pcp.PcpOption.java

/**
 * Constructs a {@link PcpOption} object by parsing a buffer.
 * @param buffer buffer containing PCP option data
 * @throws NullPointerException if any argument is {@code null}
 * @throws BufferUnderflowException if not enough data is available in {@code buffer}
 */// w ww . j a  va 2s  .c om
public PcpOption(ByteBuffer buffer) {
    Validate.notNull(buffer);

    code = buffer.get() & 0xFF;

    buffer.get(); // skip over reserved

    length = buffer.getShort() & 0xFFFF;

    byte[] dataArr = new byte[length];
    buffer.get(dataArr);

    data = ByteBuffer.wrap(dataArr).asReadOnlyBuffer();

    // skip over padding
    int remainder = length % 4;
    for (int i = 0; i < remainder; i++) {
        buffer.get();
    }
}

From source file:net.jenet.Header.java

public void fromBuffer(ByteBuffer buffer) {
    peerID = buffer.getShort();/*from   w w w  .j a  v a2s . c om*/
    flags = buffer.get();
    commandCount = buffer.get();
    sentTime = buffer.getInt();
    challenge = buffer.getInt();
}

From source file:net.jenet.CommandHeader.java

public void fromBuffer(ByteBuffer buffer) {
    command = buffer.get();
    channelID = buffer.get();/* w w w  .  ja v a  2s  . com*/
    flags = buffer.get();
    reserved = buffer.get();
    commandLength = buffer.getInt();
    reliableSequenceNumber = buffer.getInt();
}

From source file:com.codeabovelab.dm.common.security.token.SignedTokenServiceBackend.java

private byte[] restoreArray(ByteBuffer buffer) {
    int len = ((int) buffer.get() & 0xff);
    byte[] bytes = new byte[len];
    buffer.get(bytes);/*from   ww  w  .  j a  va  2  s .  c  o m*/
    return bytes;
}