Example usage for java.nio IntBuffer get

List of usage examples for java.nio IntBuffer get

Introduction

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

Prototype

public abstract int get(int index);

Source Link

Document

Returns an int at the specified index; the position is not changed.

Usage

From source file:Main.java

public static void main(String[] args) {
    IntBuffer bb = IntBuffer.allocate(10);
    bb.put(100);/*from   w  w w.  ja va2  s .  c  om*/

    bb.rewind();
    System.out.println(bb.get(0));

}

From source file:MainClass.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    IntBuffer ib = bb.asIntBuffer();

    ib.put(new int[] { 1, 2, 7, 9, 3, 8, 6 });

    System.out.println(ib.get(3));
    ib.put(3, 1811);//from   w  w w.j a v a 2s.  co  m
    ib.rewind();
    while (ib.hasRemaining()) {
        int i = ib.get();
        if (i == 0)
            break; // Else we'll get the entire buffer
        System.out.println(i);
    }
}

From source file:Main.java

public static void main(String[] args) {
    IntBuffer bb = IntBuffer.allocate(10);
    bb.put(100);//from   w  w  w.jav  a 2  s.c  o m

    bb.rewind();

    int[] intArray = new int[10];
    bb.get(intArray);

    System.out.println(Arrays.toString(intArray));

}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    FileChannel fc = new RandomAccessFile(new File("temp.tmp"), "rw").getChannel();
    IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer();
    ib.put(0);//from  w w w .  j  av a2  s.  c  o  m
    for (int i = 1; i < 10; i++)
        ib.put(ib.get(i - 1));
    fc.close();

}

From source file:IntBufferDemo.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    IntBuffer ib = bb.asIntBuffer();
    // Store an array of int:
    ib.put(new int[] { 11, 42, 47, 99, 143, 811, 1016 });
    // Absolute location read and write:
    System.out.println(ib.get(3));
    ib.put(3, 1811);/*from  w  w  w  . j av  a 2 s. com*/
    ib.rewind();
    while (ib.hasRemaining()) {
        int i = ib.get();
        if (i == 0)
            break; // Else we'll get the entire buffer
        System.out.println(i);
    }

}

From source file:Main.java

static void scaleHalf(IntBuffer in, int w, int h, IntBuffer out, int rotate) {
    for (int i = 0; i < w / 2; ++i) {
        for (int j = 0; j < h / 2; ++j) {
            int k = w * 2 * j + 2 * i;
            int pixel00 = in.get(k);
            int pixel01 = in.get(k + 1);
            int pixel10 = in.get(k + w);
            int pixel11 = in.get(k + w + 1);

            if (rotate != 0) {
                pixel00 = Integer.rotateLeft(pixel00, rotate);
                pixel01 = Integer.rotateLeft(pixel01, rotate);
                pixel10 = Integer.rotateLeft(pixel10, rotate);
                pixel11 = Integer.rotateLeft(pixel11, rotate);
            }/* w w w. j  a v a 2  s  .c om*/

            int pixel = average4RGBA(pixel00, pixel01, pixel10, pixel11);

            if (rotate != 0) {
                pixel = Integer.rotateRight(pixel, rotate);
            }

            out.put(w / 2 * j + i, pixel);
        }
    }
}

From source file:org.blockartistry.DynSurround.client.sound.SoundEngine.java

private static void configureSound() {
    int totalChannels = -1;

    try {//w w w .  ja v  a2s. co  m

        final boolean create = !AL.isCreated();
        if (create) {
            AL.create();
            alErrorCheck();
        }

        final IntBuffer ib = BufferUtils.createIntBuffer(1);
        ALC10.alcGetInteger(AL.getDevice(), ALC11.ALC_MONO_SOURCES, ib);
        alErrorCheck();
        totalChannels = ib.get(0);

        if (create)
            AL.destroy();

    } catch (final Throwable e) {
        e.printStackTrace();
    }

    int normalChannelCount = ModOptions.normalSoundChannelCount;
    int streamChannelCount = ModOptions.streamingSoundChannelCount;

    if (ModOptions.autoConfigureChannels && totalChannels > 64) {
        totalChannels = ((totalChannels + 1) * 3) / 4;
        streamChannelCount = Math.min(totalChannels / 5, MAX_STREAM_CHANNELS);
        normalChannelCount = totalChannels - streamChannelCount;
    }

    DSurround.log().info("Sound channels: %d normal, %d streaming (total avail: %s)", normalChannelCount,
            streamChannelCount, totalChannels == -1 ? "UNKNOWN" : Integer.toString(totalChannels));
    SoundSystemConfig.setNumberNormalChannels(normalChannelCount);
    SoundSystemConfig.setNumberStreamingChannels(streamChannelCount);

    // Setup sound buffering
    if (ModOptions.streamBufferCount != 0)
        SoundSystemConfig.setNumberStreamingBuffers(ModOptions.streamBufferCount);
    if (ModOptions.streamBufferSize != 0)
        SoundSystemConfig.setStreamingBufferSize(ModOptions.streamBufferSize * 1024);
    DSurround.log().info("Stream buffers: %d x %d", SoundSystemConfig.getNumberStreamingBuffers(),
            SoundSystemConfig.getStreamingBufferSize());
}

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

/**
 * Reads an image tile./*  www  . j a va2s.c om*/
 *
 * @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.mrgeo.data.raster.RasterWritable.java

private static Raster read(final byte[] rasterBytes, Writable payload) throws IOException {
    WritableRaster raster;//from  w  w  w.j  a va 2s  .  co m

    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:edu.iu.daal_pca.Service.java

public static void printAprioriRules(HomogenNumericTable leftItemsTable, HomogenNumericTable rightItemsTable,
        HomogenNumericTable confidenceTable) {
    int nRulesToPrint = 20;
    /* Get sizes of tables to store association rules */
    int nLeftItems = (int) leftItemsTable.getNumberOfRows();
    int nRightItems = (int) rightItemsTable.getNumberOfRows();
    int nRules = (int) confidenceTable.getNumberOfRows();

    /* Get association rules data */

    IntBuffer bufLeftItems = IntBuffer.allocate(nLeftItems * (int) leftItemsTable.getNumberOfColumns());
    bufLeftItems = leftItemsTable.getBlockOfRows(0, nLeftItems, bufLeftItems);
    int[] leftItems = new int[bufLeftItems.capacity()];
    bufLeftItems.get(leftItems);

    IntBuffer bufRightItems = IntBuffer.allocate(nRightItems * (int) rightItemsTable.getNumberOfColumns());
    bufRightItems = rightItemsTable.getBlockOfRows(0, nRightItems, bufRightItems);
    int[] rightItems = new int[bufRightItems.capacity()];
    bufRightItems.get(rightItems);/*from www.j a  v a  2s  .  c  o  m*/

    double[] confidence = confidenceTable.getDoubleArray();

    ArrayList<ArrayList<Integer>> leftItemsVector = new ArrayList<ArrayList<Integer>>(nRules);
    for (int i = 0; i < nRules; i++) {
        leftItemsVector.add(new ArrayList<Integer>());
    }

    if (nRules == 0) {
        System.out.println("No association rules were found ");
        return;
    }

    for (int i = 0; i < nLeftItems; i++) {
        leftItemsVector.get((leftItems[2 * i])).add(leftItems[2 * i + 1]);
    }

    ArrayList<ArrayList<Integer>> rightItemsVector = new ArrayList<ArrayList<Integer>>(nRules);
    for (int i = 0; i < nRules; i++) {
        rightItemsVector.add(new ArrayList<Integer>());
    }

    for (int i = 0; i < nRightItems; i++) {
        rightItemsVector.get((rightItems[2 * i])).add(rightItems[2 * i + 1]);
    }

    ArrayList<Double> confidenceVector = new ArrayList<Double>(nRules);
    for (int i = 0; i < nRules; i++) {
        confidenceVector.add(confidence[i]);
    }

    System.out.println("Last " + nRulesToPrint + " association rules: ");
    System.out.println("Rule" + "\t\t\t\tConfidence");

    int iMin = ((nRules > nRulesToPrint) ? (nRules - nRulesToPrint) : 0);
    for (int i = iMin; i < nRules; i++) {
        System.out.print("{");
        for (int l = 0; l < leftItemsVector.get(i).size() - 1; l++) {
            System.out.print(leftItemsVector.get(i).get(l) + ", ");
        }
        System.out.print(leftItemsVector.get(i).get(leftItemsVector.get(i).size() - 1) + "} => {");

        for (int l = 0; l < rightItemsVector.get(i).size() - 1; l++) {
            System.out.print(rightItemsVector.get(i).get(l) + ", ");
        }
        System.out.print(rightItemsVector.get(i).get(rightItemsVector.get(i).size() - 1) + "}\t\t");

        System.out.println(confidenceVector.get(i));
    }
}