Example usage for java.nio ByteBuffer limit

List of usage examples for java.nio ByteBuffer limit

Introduction

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

Prototype

public final int limit() 

Source Link

Document

Returns the limit of this buffer.

Usage

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

public static boolean isFull(ByteBuffer buff) {
    return buff != null && buff.limit() == buff.capacity();
}

From source file:io.Text.java

/** Write a UTF8 encoded string to out
 *///w w w  . ja  v a 2 s.  co  m
public static int writeString(DataOutput out, String s) throws IOException {
    ByteBuffer bytes = encode(s);
    int length = bytes.limit();
    WritableUtils.writeVInt(out, length);
    out.write(bytes.array(), 0, length);
    return length;
}

From source file:org.cloudata.core.common.io.CText.java

/** Write a UTF8 encoded string to out
 *//*from   ww  w  . jav a 2 s.c  om*/
public static int writeString(DataOutput out, String s) throws IOException {
    ByteBuffer bytes = encode(s);
    int length = bytes.limit();
    CWritableUtils.writeVInt(out, length);
    out.write(bytes.array(), 0, length);
    return length;
}

From source file:com.gamesalutes.utils.ByteUtils.java

/**
 * Extends the size of <code>buf</code> to at least meet <code>minCap</code>.
 * If <code>buf</code> is too small, then a new buffer is allocated and
 * any existing contents in <code>buf</code> will be transfered.  The position
 * of the new buffer will be that of the old buffer if it was not <code>null</code>, and
 * the previous mark will be discarded if one was set.
 * /*from   w w  w  .ja  v a2  s .  c  o  m*/
 * @param buf the input <code>ByteBuffer</code>
 * @param minCap the minimum capacity
 * @return a <code>ByteBuffer</code> that can meet <code>minCap</code>
 */
public static ByteBuffer growBuffer(ByteBuffer buf, int minCap) {
    int myLimit = buf != null ? buf.limit() : 0;
    // limit can accomidate capacity requirements
    if (buf != null && myLimit >= minCap)
        return buf;
    int myCap = buf != null ? buf.capacity() : 0;
    // capacity can accomidate but limit is too small
    if (buf != null && myCap >= minCap) {
        buf.limit(myCap);
        return buf;
    } else //if(myCap < minCap)
    {
        ByteBuffer newBuffer = null;
        if (myCap == 0)
            myCap = 1;
        while (myCap < minCap)
            myCap <<= 1;
        if (buf != null && buf.isDirect())
            newBuffer = ByteBuffer.allocateDirect(myCap);
        else
            newBuffer = ByteBuffer.allocate(myCap);
        // copy contents of original buffer
        if (buf != null) {
            int pos = buf.position();
            buf.clear();
            newBuffer.put(buf);
            newBuffer.position(pos);
        }
        return newBuffer;

    }
}

From source file:edu.umn.cs.spatialHadoop.nasa.HDFRecordReader.java

/**
 * Converts a water mask from the byte_array format to the bit_array format.
 * In the byte array format, 0 means land, anything else means water.
 * In the bit array format, false means land and true means water.
 * Each square with side length of <code>size</code> will be converted to
 * one value in the output bit array depending on average value in this
 * square box. If at least half of the values are land (i.e., 0), the
 * corresponding value in the bit array is set to false. Otherwise, the
 * corresponding value in the bit array is set to true. 
 * @param waterMaskBytes//from w w w  . j  a v a2  s.com
 * @param size
 * @return
 */
static BitArray convertWaterMaskToBits(ByteBuffer waterMaskBytes, int size) {
    int wmRes = (int) Math.sqrt(waterMaskBytes.limit());
    int dataRes = wmRes / size;
    BitArray waterMaskBits = new BitArray(dataRes * dataRes);
    // Size of each pixel of the data when mapped to the water mask
    for (int row = 0; row < dataRes; row++)
        for (int col = 0; col < dataRes; col++) {
            int r1 = row * size;
            int r2 = (row + 1) * size;
            int c1 = col * size;
            int c2 = (col + 1) * size;

            byte wm_sum = 0;
            for (int r = r1; r < r2; r++)
                for (int c = c1; c < c2; c++) {
                    byte wm_value = waterMaskBytes.get(r * wmRes + c);
                    if (wm_value == 0)
                        wm_sum++;
                }
            waterMaskBits.set(row * dataRes + col, wm_sum < (size * size) / 2);
        }
    return waterMaskBits;
}

From source file:org.apache.hadoop.hbase.io.hfile.FixedFileTrailer.java

/**
 * Reads a file trailer from the given file.
 *
 * @param istream the input stream with the ability to seek. Does not have to
 *          be buffered, as only one read operation is made.
 * @param fileSize the file size. Can be obtained using
 *          {@link org.apache.hadoop.fs.FileSystem#getFileStatus(
 *          org.apache.hadoop.fs.Path)}.
 * @return the fixed file trailer read//from  w ww .  jav a 2  s  .  c o  m
 * @throws IOException if failed to read from the underlying stream, or the
 *           trailer is corrupted, or the version of the trailer is
 *           unsupported
 */
public static FixedFileTrailer readFromStream(FSDataInputStream istream, long fileSize) throws IOException {
    int bufferSize = MAX_TRAILER_SIZE;
    long seekPoint = fileSize - bufferSize;
    if (seekPoint < 0) {
        // It is hard to imagine such a small HFile.
        seekPoint = 0;
        bufferSize = (int) fileSize;
    }

    istream.seek(seekPoint);
    ByteBuffer buf = ByteBuffer.allocate(bufferSize);
    istream.readFully(buf.array(), buf.arrayOffset(), buf.arrayOffset() + buf.limit());

    // Read the version from the last int of the file.
    buf.position(buf.limit() - Bytes.SIZEOF_INT);
    int version = buf.getInt();

    // Extract the major and minor versions.
    //version ??major version??minor version
    int majorVersion = extractMajorVersion(version);
    int minorVersion = extractMinorVersion(version);

    HFile.checkFormatVersion(majorVersion); // throws IAE if invalid

    int trailerSize = getTrailerSize(majorVersion);

    FixedFileTrailer fft = new FixedFileTrailer(majorVersion, minorVersion);
    fft.deserialize(new DataInputStream(
            new ByteArrayInputStream(buf.array(), buf.arrayOffset() + bufferSize - trailerSize, trailerSize)));
    return fft;
}

From source file:edu.umn.cs.spatialHadoop.nasa.HDFRecordReader.java

/**
 * Find runs of true values in each row of the given 2D array of value.
 * @param values All the short values stored in a {@link ByteBuffer}
 * @param fillValue The marker that marks fillValue
 * @return An array of runs as one for each row in the given array.
 *//*w ww . j av a 2s  . com*/
static ShortArray[] findTrueRuns(ByteBuffer values, short fillValue) {
    int resolution = (int) Math.sqrt(values.limit() / 2);
    ShortArray[] trueRuns = new ShortArray[resolution];
    for (short row = 0; row < resolution; row++) {
        trueRuns[row] = new ShortArray();
        // A flag that is set to true if currently inside a run of fillValues.
        boolean insideFillValue = true;
        for (short col = 0; col < resolution; col++) {
            if ((values.getShort((row * resolution + col) * 2) == fillValue) ^ insideFillValue) {
                // Found a flip between true and fill values.
                if (!insideFillValue && col != 0)
                    trueRuns[row].append((short) (col - 1));
                else
                    trueRuns[row].append(col);
                insideFillValue = !insideFillValue;
            }
        }
        if (!insideFillValue)
            trueRuns[row].append((short) (resolution - 1));
    }
    return trueRuns;
}

From source file:io.mycat.util.ByteBufferUtil.java

/**
 * 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.
 *//*from  www .  j  a  va 2 s .  c o  m*/
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);
        //            if (byte1 == byte2)
        //                continue;
        // compare non-equal bytes as unsigned
        if (byte1 != byte2) {
            return (byte1 & 0xFF) < (byte2 & 0xFF) ? -1 : 1;
        }
    }
    return 0;
}

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

/**
 * fill?//from  w w w. ja v  a 2s.c  o  m
 * 
 * @param buffer
 * @return
 */
public static int flipToFill(ByteBuffer buffer) {
    int position = buffer.position();
    int limit = buffer.limit();
    // flush??fill?
    if (position == limit) {
        buffer.position(0);
        buffer.limit(buffer.capacity());
        return 0;
    }
    // ?limit equal capacity,?
    int capacity = buffer.capacity();
    if (limit == capacity) {
        buffer.compact();
        return 0;
    }
    // ??
    buffer.position(limit);
    buffer.limit(capacity);
    return position;
}

From source file:eap.util.EDcodeUtil.java

public static byte[] utf8Encode(CharSequence string) {
    try {/*w  w  w . j a va 2s.  co m*/
        ByteBuffer bytes = CHARSET.newEncoder().encode(CharBuffer.wrap(string));
        byte[] bytesCopy = new byte[bytes.limit()];
        System.arraycopy(bytes.array(), 0, bytesCopy, 0, bytes.limit());

        return bytesCopy;
    } catch (CharacterCodingException e) {
        throw new IllegalArgumentException("Encoding failed", e);
    }
}