Example usage for java.nio ByteBuffer getInt

List of usage examples for java.nio ByteBuffer getInt

Introduction

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

Prototype

public abstract int getInt();

Source Link

Document

Returns the int at the current position and increases the position by 4.

Usage

From source file:tachyon.util.CommonUtils.java

public static void printByteBuffer(Logger LOG, ByteBuffer buf) {
    StringBuilder sb = new StringBuilder();
    for (int k = 0; k < buf.limit() / 4; k++) {
        sb.append(buf.getInt()).append(" ");
    }/* w  w w  .  j  a v  a  2 s .co m*/

    LOG.info(sb.toString());
}

From source file:xbird.storage.io.RemoteVarSegments.java

public static void handleResponse(final TrackReadRequestMessage request, final ProtocolEncoderOutput out,
        final ConcurrentMap<String, FileChannel> fdCacheMap,
        final ConcurrentMap<String, IDescriptor> directoryCache) throws IOException {
    final String filePath = request.filePath;
    final long[] idxs = request.idxs;
    final int size = idxs.length;

    // look-up directory
    final File dataFile = new File(filePath);
    final long[] offsets = new long[size];
    IDescriptor directory = directoryCache.get(filePath);
    try {//from   w  w  w.  j  ava2s  . com
        if (directory == null) {
            directory = VarSegments.initDescriptor(dataFile);
            directoryCache.put(filePath, directory);
        }
        for (int i = 0; i < size; i++) {
            offsets[i] = directory.getRecordAddr(idxs[i]);
        }
    } catch (IOException e) {
        LOG.error(e);
        throw e;
    }

    FileChannel fileChannel = fdCacheMap.get(filePath);
    if (fileChannel == null) {
        if (!dataFile.exists()) {
            throw new IllegalStateException("file not exists: " + filePath);
        }
        final RandomAccessFile raf;
        try {
            raf = new RandomAccessFile(dataFile, "r");
        } catch (FileNotFoundException e) {
            throw new IllegalStateException(e);
        }
        fileChannel = raf.getChannel();
        fdCacheMap.put(filePath, fileChannel);
    }

    for (int i = 0; i < size; i++) {
        final long offset = offsets[i];
        // get data length
        final ByteBuffer tmpBuf = ByteBuffer.allocate(4);
        try {
            fileChannel.read(tmpBuf, offset);
        } catch (IOException e) {
            LOG.error(e);
            throw e;
        }
        tmpBuf.flip();
        final int length = tmpBuf.getInt();
        tmpBuf.rewind();
        IoBuffer ioBuf = IoBuffer.wrap(tmpBuf);
        out.write(ioBuf);
        // attempt zero-copy sendfile
        long position = offset + 4;
        long count = length;
        FileRegion fileRegion = new DefaultFileRegion(fileChannel, position, count);
        out.write(fileRegion);
    }
}

From source file:com.tongbanjie.tarzan.rpc.protocol.RpcCommand.java

public static RpcCommand decode(final ByteBuffer byteBuffer) {
    ///*w  ww.j  ava 2s . co m*/
    int length = byteBuffer.limit();
    //?? 1byte
    byte protocolType = byteBuffer.get();
    //Header 4type
    int headerLength = byteBuffer.getInt();
    //Header
    byte[] headerData = new byte[headerLength];
    byteBuffer.get(headerData);

    RpcCommand cmd = headerDecode(headerData, getProtocolType(protocolType));
    //body
    int bodyLength = length - 5 - headerLength;
    byte[] bodyData = null;
    if (bodyLength > 0) {
        bodyData = new byte[bodyLength];
        byteBuffer.get(bodyData);
    }
    cmd.body = bodyData;

    return cmd;
}

From source file:org.wso2.carbon.analytics.data.commons.utils.AnalyticsCommonUtils.java

public static Map<String, Object> decodeRecordValues(byte[] data, Set<String> columns)
        throws AnalyticsException {
    /* using LinkedHashMap to retain the column order */
    Map<String, Object> result = new LinkedHashMap<>();
    int type, size;
    String colName;/*from   w ww .  j a v a2s  . com*/
    Object value;
    byte[] buff;
    byte boolVal;
    byte[] binData;
    try {
        ByteBuffer buffer = ByteBuffer.wrap(data);
        while (buffer.remaining() > 0) {
            size = buffer.getInt();
            if (size == 0) {
                break;
            }
            buff = new byte[size];
            buffer.get(buff, 0, size);
            colName = new String(buff, StandardCharsets.UTF_8);
            type = buffer.get();
            switch (type) {
            case DATA_TYPE_STRING:
                size = buffer.getInt();
                buff = new byte[size];
                buffer.get(buff, 0, size);
                value = new String(buff, StandardCharsets.UTF_8);
                break;
            case DATA_TYPE_LONG:
                value = buffer.getLong();
                break;
            case DATA_TYPE_DOUBLE:
                value = buffer.getDouble();
                break;
            case DATA_TYPE_BOOLEAN:
                boolVal = buffer.get();
                if (boolVal == BOOLEAN_TRUE) {
                    value = true;
                } else if (boolVal == BOOLEAN_FALSE) {
                    value = false;
                } else {
                    throw new AnalyticsException("Invalid encoded boolean value: " + boolVal);
                }
                break;
            case DATA_TYPE_INTEGER:
                value = buffer.getInt();
                break;
            case DATA_TYPE_FLOAT:
                value = buffer.getFloat();
                break;
            case DATA_TYPE_BINARY:
                size = buffer.getInt();
                binData = new byte[size];
                buffer.get(binData);
                value = binData;
                break;
            case DATA_TYPE_OBJECT:
                size = buffer.getInt();
                binData = new byte[size];
                buffer.get(binData);
                value = deserializeObject(binData);
                break;
            case DATA_TYPE_NULL:
                value = null;
                break;
            default:
                throw new AnalyticsException("Unknown encoded data source type : " + type);
            }
            if (columns == null || columns.contains(colName)) {
                result.put(colName, value);
            }
        }
    } catch (Exception e) {
        throw new AnalyticsException("Error in decoding record values: " + e.getMessage(), e);
    }
    return result;
}

From source file:VASSAL.tools.image.tilecache.TileUtils.java

/**
 * Reads the dimensions of the tile from a stream.
 *
 * @param in the stream//  w  w  w  .  j  ava 2 s.c om
 * @return the dimensions
 *
 * @throws IOException if the read fails
 */
public static Dimension size(InputStream in) throws IOException {
    ByteBuffer bb;

    // read the header
    final byte[] header = readHeader(in);
    bb = ByteBuffer.wrap(header);

    // validate the signature
    final byte[] sig = new byte[6];
    bb.get(sig);
    checkSignature(sig);

    // get the dimensions
    return new Dimension(bb.getInt(), bb.getInt());
}

From source file:org.mrgeo.data.raster.RasterWritable.java

private static Raster read(final byte[] rasterBytes, Writable payload) throws IOException {
    WritableRaster raster;/*from   www  .  j a va2 s  . com*/

    final ByteBuffer rasterBuffer = ByteBuffer.wrap(rasterBytes);

    @SuppressWarnings("unused")
    final int headersize = rasterBuffer.getInt(); // this isn't really used anymore...
    final int height = rasterBuffer.getInt();
    final int width = rasterBuffer.getInt();
    final int bands = rasterBuffer.getInt();
    final int datatype = rasterBuffer.getInt();
    final SampleModelType sampleModelType = SampleModelType.values()[rasterBuffer.getInt()];

    SampleModel model;
    switch (sampleModelType) {
    case BANDED:
        model = new BandedSampleModel(datatype, width, height, bands);
        break;
    case MULTIPIXELPACKED:
        throw new NotImplementedException("MultiPixelPackedSampleModel not implemented yet");
        // model = new MultiPixelPackedSampleModel(dataType, w, h, numberOfBits)
    case PIXELINTERLEAVED: {
        final int pixelStride = rasterBuffer.getInt();
        final int scanlineStride = rasterBuffer.getInt();
        final int bandcnt = rasterBuffer.getInt();
        final int[] bandOffsets = new int[bandcnt];
        for (int i = 0; i < bandcnt; i++) {
            bandOffsets[i] = rasterBuffer.getInt();
        }
        model = new PixelInterleavedSampleModel(datatype, width, height, pixelStride, scanlineStride,
                bandOffsets);
        break;
    }
    case SINGLEPIXELPACKED:
        throw new NotImplementedException("SinglePixelPackedSampleModel not implemented yet");
        // model = new SinglePixelPackedSampleModel(dataType, w, h, bitMasks);
    case COMPONENT: {
        final int pixelStride = rasterBuffer.getInt();
        final int scanlineStride = rasterBuffer.getInt();
        final int bandcnt = rasterBuffer.getInt();
        final int[] bandOffsets = new int[bandcnt];
        for (int i = 0; i < bandcnt; i++) {
            bandOffsets[i] = rasterBuffer.getInt();
        }
        model = new ComponentSampleModel(datatype, width, height, pixelStride, scanlineStride, bandOffsets);
        break;
    }
    default:
        throw new RasterWritableException("Unknown RasterSampleModel type");
    }

    // include the header size param in the count
    int startdata = rasterBuffer.position();

    // calculate the data size
    int[] samplesize = model.getSampleSize();
    int samplebytes = 0;
    for (int ss : samplesize) {
        // bits to bytes
        samplebytes += (ss / 8);
    }
    int databytes = model.getHeight() * model.getWidth() * samplebytes;

    // final ByteBuffer rasterBuffer = ByteBuffer.wrap(rasterBytes, headerbytes, databytes);
    // the corner of the raster is always 0,0
    raster = Raster.createWritableRaster(model, null);

    switch (datatype) {
    case DataBuffer.TYPE_BYTE: {
        // we can't use the byte buffer explicitly because the header info is
        // still in it...
        final byte[] bytedata = new byte[databytes];
        rasterBuffer.get(bytedata);

        raster.setDataElements(0, 0, width, height, bytedata);
        break;
    }
    case DataBuffer.TYPE_FLOAT: {
        final FloatBuffer floatbuff = rasterBuffer.asFloatBuffer();
        final float[] floatdata = new float[databytes / RasterUtils.FLOAT_BYTES];

        floatbuff.get(floatdata);

        raster.setDataElements(0, 0, width, height, floatdata);
        break;
    }
    case DataBuffer.TYPE_DOUBLE: {
        final DoubleBuffer doublebuff = rasterBuffer.asDoubleBuffer();
        final double[] doubledata = new double[databytes / RasterUtils.DOUBLE_BYTES];

        doublebuff.get(doubledata);

        raster.setDataElements(0, 0, width, height, doubledata);

        break;
    }
    case DataBuffer.TYPE_INT: {
        final IntBuffer intbuff = rasterBuffer.asIntBuffer();
        final int[] intdata = new int[databytes / RasterUtils.INT_BYTES];

        intbuff.get(intdata);

        raster.setDataElements(0, 0, width, height, intdata);

        break;
    }
    case DataBuffer.TYPE_SHORT:
    case DataBuffer.TYPE_USHORT: {
        final ShortBuffer shortbuff = rasterBuffer.asShortBuffer();
        final short[] shortdata = new short[databytes / RasterUtils.SHORT_BYTES];
        shortbuff.get(shortdata);
        raster.setDataElements(0, 0, width, height, shortdata);
        break;
    }
    default:
        throw new RasterWritableException("Error trying to read raster.  Bad raster data type");
    }

    // should we even try to extract the payload?
    if (payload != null) {
        // test to see if this is a raster with a possible payload
        final int payloadStart = startdata + databytes;
        if (rasterBytes.length > payloadStart) {
            // extract the payload
            final ByteArrayInputStream bais = new ByteArrayInputStream(rasterBytes, payloadStart,
                    rasterBytes.length - payloadStart);
            final DataInputStream dis = new DataInputStream(bais);
            payload.readFields(dis);
        }
    }
    return raster;
}

From source file:org.carbondata.query.util.CacheUtil.java

private static int[][] getIndexArray(ByteBuffer buffer) {
    int arraySize = buffer.getInt();
    int[][] sortorderIndexArray = new int[1][arraySize];
    if (arraySize > CarbonCommonConstants.LEVEL_ARRAY_SIZE) {
        int div = arraySize / CarbonCommonConstants.LEVEL_ARRAY_SIZE;
        int rem = arraySize % CarbonCommonConstants.LEVEL_ARRAY_SIZE;
        if (rem > 0) {
            div++;// w  ww. j  a  v  a 2 s  .  c om
        }
        sortorderIndexArray = new int[div][];
        for (int i = 0; i < div - 1; i++) {
            sortorderIndexArray[i] = new int[CarbonCommonConstants.LEVEL_ARRAY_SIZE];
        }

        if (rem > 0) {
            sortorderIndexArray[sortorderIndexArray.length - 1] = new int[rem];
        } else {
            sortorderIndexArray[sortorderIndexArray.length
                    - 1] = new int[CarbonCommonConstants.LEVEL_ARRAY_SIZE];
        }
    }
    int index = 0;
    int prvArrayIndex = 0;
    int current = 0;
    for (int i = 0; i < arraySize; i++) {
        sortorderIndexArray[current / CarbonCommonConstants.LEVEL_ARRAY_SIZE][index] = buffer.getInt();
        current++;
        if (current / CarbonCommonConstants.LEVEL_ARRAY_SIZE > prvArrayIndex) {
            prvArrayIndex++;
            index = 0;
        } else {
            index++;
        }
    }
    return sortorderIndexArray;
}

From source file:com.github.seqware.queryengine.plugins.runners.hbasemr.MRHBasePluginRunner.java

public static List<FeatureSet> convertBase64StrToFeatureSets(final String sourceSets) {
    byte[] data = (byte[]) Base64.decodeBase64(sourceSets);
    ByteBuffer buf = ByteBuffer.wrap(data);
    int numSets = buf.getInt();
    List<FeatureSet> sSets = new ArrayList<FeatureSet>();
    for (int i = 0; i < numSets; i++) {
        // get size of blob
        int bSize = buf.getInt();
        byte[] dst = new byte[bSize];
        buf.get(dst);/*from  w ww  .  jav  a 2s .  c  o m*/
        FeatureSet deserialize = SWQEFactory.getSerialization().deserialize(dst, FeatureSet.class);
        sSets.add(deserialize);
    }
    return sSets;
}

From source file:VASSAL.tools.image.tilecache.TileUtils.java

/**
 * Reads an image tile.//from  w w w .j  av a 2 s  .  c  o  m
 *
 * @param in a stream containing the tile data
 * @return the tile image
 *
 * @throws IOException if the read fails
 */
public static BufferedImage read(InputStream in) throws IOException {
    ByteBuffer bb;

    // read the header
    final byte[] header = readHeader(in);
    bb = ByteBuffer.wrap(header);

    // validate the signature
    final byte[] sig = new byte[6];
    bb.get(sig);
    checkSignature(sig);

    // get the dimensions and type
    final int w = bb.getInt();
    final int h = bb.getInt();
    final int type = bb.getInt();

    // read the image data
    final byte[] cdata = IOUtils.toByteArray(in);

    // decompress the image data
    InputStream zin = null;
    try {
        zin = new GZIPInputStream(new ByteArrayInputStream(cdata));
        bb = ByteBuffer.wrap(IOUtils.toByteArray(zin));
        zin.close();
    } finally {
        IOUtils.closeQuietly(zin);
    }

    // build the image
    final BufferedImage img = new BufferedImage(w, h, type);

    // FIXME: This might decelerate the image? If so, then we should
    // make a copy.
    final DataBufferInt db = (DataBufferInt) img.getRaster().getDataBuffer();
    final int[] data = db.getData();

    final IntBuffer ib = bb.asIntBuffer();
    ib.get(data);

    /*
        if (ib.hasRemaining()) {
          // buffer contains garbage at the end!
          throw new IOException("found " + (4*ib.remaining()) + " more bytes!");
        }
    */

    return img;
}

From source file:org.broadinstitute.gatk.utils.io.IOUtils.java

/**
 * Determines the uncompressed size of a GZIP file. Uses the GZIP ISIZE field in the last
 * 4 bytes of the file to get this information.
 *
 * @param gzipFile GZIP-format file whose uncompressed size to determine
 * @return The uncompressed size (in bytes) of the GZIP file
 *///  w  w  w .ja  v  a 2  s  . c  om
public static int getGZIPFileUncompressedSize(File gzipFile) {
    if (gzipFile == null) {
        throw new ReviewedGATKException("GZIP file to examine was null");
    }

    try {
        // The GZIP ISIZE field holds the uncompressed size of the compressed data.
        // It occupies the last 4 bytes of any GZIP file:
        RandomAccessFile in = new RandomAccessFile(gzipFile, "r");
        in.seek(gzipFile.length() - 4);
        byte[] sizeBytes = new byte[4];
        in.read(sizeBytes, 0, 4);

        ByteBuffer byteBuf = ByteBuffer.wrap(sizeBytes);
        byteBuf.order(ByteOrder.LITTLE_ENDIAN); // The GZIP spec mandates little-endian byte order
        int uncompressedSize = byteBuf.getInt();

        // If the size read in is negative, we've overflowed our signed integer:
        if (uncompressedSize < 0) {
            throw new UserException.CouldNotReadInputFile(String.format(
                    "Cannot accurately determine the uncompressed size of file %s "
                            + "because it's either larger than %d bytes or the GZIP ISIZE field is corrupt",
                    gzipFile.getAbsolutePath(), Integer.MAX_VALUE));
        }

        return uncompressedSize;
    } catch (IOException e) {
        throw new UserException.CouldNotReadInputFile(gzipFile, e);
    }
}