Java Code Examples for java.nio.ByteBuffer#asIntBuffer()

The following examples show how to use java.nio.ByteBuffer#asIntBuffer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileSerializationBenchmark.java    From imhotep with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(int[] a, File file) throws IOException {
    FileChannel ch = new RandomAccessFile(file, "rw").getChannel();
    ByteBuffer buffer = ByteBuffer.allocateDirect(8192);
    buffer.order(ByteOrder.BIG_ENDIAN);
    IntBuffer intBuffer = buffer.asIntBuffer();
    for (int i = 0; i < a.length; i += 2048) {
        intBuffer.clear();
        int lim = Math.min(2048, a.length - i);
        for (int j = 0; j < lim; ++j) {
            intBuffer.put(j, a[i+j]);
        }
        buffer.position(0).limit(4*lim);
        ch.write(buffer);
    }
    ch.close();
}
 
Example 2
Source File: FileSerializationBenchmark.java    From imhotep with Apache License 2.0 6 votes vote down vote up
@Override
public int[] deserialize(File file) throws IOException {
    FileChannel ch = new RandomAccessFile(file, "r").getChannel();
    int[] ret = new int[(int)(file.length() / 4)];
    ByteBuffer buffer = ByteBuffer.allocateDirect(8192);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    IntBuffer intBuffer = buffer.asIntBuffer();
    for (int i = 0; i < ret.length; i += 2048) {
        buffer.clear();
        int lim = ch.read(buffer) / 4;
        intBuffer.clear();
        intBuffer.get(ret, i, lim);
    }
    ch.close();
    return ret;
}
 
Example 3
Source File: ShapeBuffer.java    From AndroidDemoProjects with Apache License 2.0 6 votes vote down vote up
public ShapeBuffer() {
    mVertexData = new float[COORDS_PER_VERTEX * MAX_BUFFER_SIZE];
    // All 4 color channels are packed into a single int, so only need one int per vertex.
    mColorData = new int[MAX_BUFFER_SIZE];
    clear();

    ByteBuffer bb;

    // size = buffer size x4 values per color, x1 bytes per color.
    bb = ByteBuffer.allocateDirect(MAX_BUFFER_SIZE * COLOR_STRIDE);
    bb.order(ByteOrder.nativeOrder());
    mColorBuffer = bb.asIntBuffer();

    // size = buffer size x3 values per coord, x4 bytes per float
    bb = ByteBuffer.allocateDirect(MAX_BUFFER_SIZE * VERTEX_STRIDE);
    bb.order(ByteOrder.nativeOrder());
    mVertexBuffer = bb.asFloatBuffer();
}
 
Example 4
Source File: FileSerializationBenchmark.java    From imhotep with Apache License 2.0 6 votes vote down vote up
@Override
public int[] deserialize(File file) throws IOException {
    FileChannel ch = new RandomAccessFile(file, "r").getChannel();
    int[] ret = new int[(int)(file.length() / 4)];
    ByteBuffer buffer = ByteBuffer.allocateDirect(8192);
    buffer.order(ByteOrder.BIG_ENDIAN);
    IntBuffer intBuffer = buffer.asIntBuffer();
    for (int i = 0; i < ret.length; i += 2048) {
        buffer.clear();
        int lim = ch.read(buffer) / 4;
        intBuffer.clear();
        intBuffer.get(ret, i, lim);
    }
    ch.close();
    return ret;
}
 
Example 5
Source File: SmallGLUT.java    From opengl with Apache License 2.0 5 votes vote down vote up
IntBuffer getIntBufferFromIntArray( int array[]) {
ByteBuffer tempBuffer = ByteBuffer.allocateDirect(array.length * 4);
tempBuffer.order(ByteOrder.nativeOrder());
IntBuffer buffer = tempBuffer.asIntBuffer();
buffer.put(array);
buffer.position(0);
return buffer;
   }
 
Example 6
Source File: FileSerializationBenchmark.java    From imhotep with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(int[] a, File file) throws IOException {
    FileChannel ch = new RandomAccessFile(file, "rw").getChannel();
    ByteBuffer buffer = ByteBuffer.allocateDirect(8192);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    IntBuffer intBuffer = buffer.asIntBuffer();
    for (int i = 0; i < a.length; i += 2048) {
        intBuffer.clear();
        int lim = Math.min(2048, a.length - i);
        intBuffer.put(a, i, lim);
        buffer.position(0).limit(4*lim);
        ch.write(buffer);
    }
    ch.close();
}
 
Example 7
Source File: SmallGLUT.java    From opengl with Apache License 2.0 5 votes vote down vote up
IntBuffer getIntBufferFromIntArray( int array[]) {
ByteBuffer tempBuffer = ByteBuffer.allocateDirect(array.length * 4);
tempBuffer.order(ByteOrder.nativeOrder());
IntBuffer buffer = tempBuffer.asIntBuffer();
buffer.put(array);
buffer.position(0);
return buffer;
   }
 
Example 8
Source File: TypedArrayFunctions.java    From es6draft with MIT License 5 votes vote down vote up
private static final IntBuffer asIntBuffer(ArrayBuffer buffer, int byteOffset, int length) {
    ByteBuffer data = byteBuffer(buffer);
    int byteLength = Integer.BYTES * length;

    data.limit(byteOffset + byteLength).position(byteOffset);
    IntBuffer view = data.asIntBuffer();
    data.clear();
    return view;
}
 
Example 9
Source File: GeometryUtils.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public static byte[] intArrayToByteArray(int[] indices) {
	if (indices == null) {
		return null;
	}
	ByteBuffer buffer = ByteBuffer.wrap(new byte[indices.length * 4]);
	buffer.order(ByteOrder.LITTLE_ENDIAN);
	IntBuffer asIntBuffer = buffer.asIntBuffer();
	for (int i : indices) {
		asIntBuffer.put(i);
	}
	return buffer.array();
}
 
Example 10
Source File: OfflineGeometryGenerator.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
private byte[] intArrayToByteArray(int[] indices) {
	if (indices == null) {
		return null;
	}
	ByteBuffer buffer = ByteBuffer.wrap(new byte[indices.length * 4]);
	buffer.order(ByteOrder.LITTLE_ENDIAN);
	IntBuffer asIntBuffer = buffer.asIntBuffer();
	for (int i : indices) {
		asIntBuffer.put(i);
	}
	return buffer.array();
}
 
Example 11
Source File: LittleEndianBinUtils.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public static byte[] intToByteArray(int inInt) {
	byte[] bArray = new byte[4];
	ByteBuffer bBuffer = ByteBuffer.wrap(bArray);
	bBuffer.order(ByteOrder.LITTLE_ENDIAN);
	IntBuffer lBuffer = bBuffer.asIntBuffer();
	lBuffer.put(inInt);
	return bArray;
}
 
Example 12
Source File: BufferTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private void testBuffersIndependentLimit(ByteBuffer b) {
    CharBuffer c = b.asCharBuffer();
    b.limit(b.capacity()); c.put(1, (char)1);
    b.limit(0);  c.put(1, (char)1);

    b.limit(b.capacity());
    ShortBuffer s = b.asShortBuffer();
    b.limit(b.capacity()); s.put(1, (short)1);
    b.limit(0);  s.put(1, (short)1);

    b.limit(b.capacity());
    IntBuffer i = b.asIntBuffer();
    i.put(1, (int)1);
    b.limit(0);  i.put(1, (int)1);

    b.limit(b.capacity());
    LongBuffer l = b.asLongBuffer();
    l.put(1, (long)1);
    b.limit(0);  l.put(1, (long)1);

    b.limit(b.capacity());
    FloatBuffer f = b.asFloatBuffer();
    f.put(1, (float)1);
    b.limit(0);  f.put(1, (float)1);

    b.limit(b.capacity());
    DoubleBuffer d = b.asDoubleBuffer();
    d.put(1, (double)1);
    b.limit(0);  d.put(1, (double)1);
}
 
Example 13
Source File: CMap.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
CMapFormat12(ByteBuffer buffer, int offset, char[] xlat) {
    if (xlat != null) {
        throw new RuntimeException("xlat array for cmap fmt=12");
    }

    buffer.position(offset+12);
    numGroups = buffer.getInt() & INTMASK;
    // A map group record is three uint32's making for 12 bytes total
    if (buffer.remaining() < (12 * (long)numGroups)) {
        throw new RuntimeException("Format 12 table exceeded");
    }
    startCharCode = new long[numGroups];
    endCharCode = new long[numGroups];
    startGlyphID = new int[numGroups];
    buffer = buffer.slice();
    IntBuffer ibuffer = buffer.asIntBuffer();
    for (int i=0; i<numGroups; i++) {
        startCharCode[i] = ibuffer.get() & INTMASK;
        endCharCode[i] = ibuffer.get() & INTMASK;
        startGlyphID[i] = ibuffer.get() & INTMASK;
    }

    /* Finds the high bit by binary searching through the bits */
    int value = numGroups;

    if (value >= 1 << 16) {
        value >>= 16;
        highBit += 16;
    }

    if (value >= 1 << 8) {
        value >>= 8;
        highBit += 8;
    }

    if (value >= 1 << 4) {
        value >>= 4;
        highBit += 4;
    }

    if (value >= 1 << 2) {
        value >>= 2;
        highBit += 2;
    }

    if (value >= 1 << 1) {
        value >>= 1;
        highBit += 1;
    }

    power = 1 << highBit;
    extra = numGroups - power;
}
 
Example 14
Source File: CMap.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
CMapFormat12(ByteBuffer buffer, int offset, char[] xlat) {
    if (xlat != null) {
        throw new RuntimeException("xlat array for cmap fmt=12");
    }

    buffer.position(offset+12);
    numGroups = buffer.getInt() & INTMASK;
    // A map group record is three uint32's making for 12 bytes total
    if (buffer.remaining() < (12 * (long)numGroups)) {
        throw new RuntimeException("Format 12 table exceeded");
    }
    startCharCode = new long[numGroups];
    endCharCode = new long[numGroups];
    startGlyphID = new int[numGroups];
    buffer = buffer.slice();
    IntBuffer ibuffer = buffer.asIntBuffer();
    for (int i=0; i<numGroups; i++) {
        startCharCode[i] = ibuffer.get() & INTMASK;
        endCharCode[i] = ibuffer.get() & INTMASK;
        startGlyphID[i] = ibuffer.get() & INTMASK;
    }

    /* Finds the high bit by binary searching through the bits */
    int value = numGroups;

    if (value >= 1 << 16) {
        value >>= 16;
        highBit += 16;
    }

    if (value >= 1 << 8) {
        value >>= 8;
        highBit += 8;
    }

    if (value >= 1 << 4) {
        value >>= 4;
        highBit += 4;
    }

    if (value >= 1 << 2) {
        value >>= 2;
        highBit += 2;
    }

    if (value >= 1 << 1) {
        value >>= 1;
        highBit += 1;
    }

    power = 1 << highBit;
    extra = numGroups - power;
}
 
Example 15
Source File: CacheWriter.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void write (BufferedImage img, boolean append, String id) throws IOException {
    File out = getOutfile(append);
    File meta = getMetafile(append);
    System.err.println("Writing to " + out + " and " + meta);
    
    int width = img.getWidth();
    int height = img.getHeight();
    
    ByteBuffer buf = ByteBuffer.allocate(width * height * 4);
    IntBuffer ibuf = buf.asIntBuffer();
  
    for (int y=0; y < height; y++) {
        for (int x=0; x < width; x++) {
            int pixel = img.getRGB(x, y);
            ibuf.put(pixel);
        }
    }
    FileOutputStream fileOut = new FileOutputStream (out, append);
    FileOutputStream metaOut = new FileOutputStream (meta, append);
    FileChannel fileChannel = fileOut.getChannel();
    
    if (append) {
        fileChannel.position(out.length());
    }
    
    //Check the size of the file we're creating - nio bytebuffers are
    //limited to dealing with files < Integer.MAX_VALUE large
    if (fileChannel.position() + buf.limit() > Integer.MAX_VALUE) {
        //Can handle this and create a second cache file in the unlikely
        //event this comes to pass
        throw new BufferOverflowException();
    }
    
    long start = fileChannel.position();
    
    fileChannel.write(buf);
    
    long end = fileChannel.position();
    
    fileChannel.force(true);
    fileChannel.close();
    
    FileChannel metaChannel = metaOut.getChannel();
    if (append) {
        metaChannel.position(meta.length());
    }
    
    metaChannel.write(getMetadata(img, id, start, end));
    metaChannel.force(true);
    metaChannel.close();
}
 
Example 16
Source File: Utils.java    From amcgala with Educational Community License v2.0 4 votes vote down vote up
public final static String saveAsBMP(String filename, ByteBuffer pixel_data, int width, int height) {
    long before = System.currentTimeMillis();
    int pad = 4 - (width * 3) % 4;
    if (pad == 4)
        pad = 0;
    int size = (width * 3 + pad) * height + 54;
    ByteBuffer buffer = ByteBuffer.allocate(size);

    //write BMP header
    buffer.put((byte) 0x42);                             // signature, must be 4D42 hex
    buffer.put((byte) 0x4D);                             // ...
    buffer.put((byte) (size & 0x000000ff));        // size of BMP file in bytes
    buffer.put((byte) ((size & 0x0000ff00) >> 8));   // ...
    buffer.put((byte) ((size & 0x00ff0000) >> 16));  // ...
    buffer.put((byte) ((size & 0xff000000) >> 24));  // ...
    buffer.put((byte) 0);                                // reserved, must be zero
    buffer.put((byte) 0);                                // reserved, must be zero
    buffer.put((byte) 0);                                // reserved, must be zero
    buffer.put((byte) 0);                                // reserved, must be zero
    buffer.put((byte) 54);                               // offset to start of image data in bytes
    buffer.put((byte) 0);                                // ...
    buffer.put((byte) 0);                                // ...
    buffer.put((byte) 0);                                // ...
    buffer.put((byte) 40);                               // size of BITMAPINFOHEADER structure, must be 40
    buffer.put((byte) 0);                                // ...
    buffer.put((byte) 0);                                // ...
    buffer.put((byte) 0);                                // ...
    buffer.put((byte) (width & 0x000000ff));       // image width in pixels
    buffer.put((byte) ((width & 0x0000ff00) >> 8));  // ...
    buffer.put((byte) ((width & 0x00ff0000) >> 16)); // ...
    buffer.put((byte) ((width & 0xff000000) >> 24)); // ...
    buffer.put((byte) (height & 0x000000ff));      // image width in pixels
    buffer.put((byte) ((height & 0x0000ff00) >> 8)); // ...
    buffer.put((byte) ((height & 0x00ff0000) >> 16));// ...
    buffer.put((byte) ((height & 0xff000000) >> 24));// ...
    buffer.put((byte) 1);                                // number of planes in the image, must be 1
    buffer.put((byte) 0);                                // ...
    buffer.put((byte) 24);           // number of bits per pixel (1, 4, 8, or 24)
    buffer.put((byte) 0);                                // ...
    buffer.put((byte) 0);                                // compression type (0=none, 1=RLE-8, 2=RLE-4)
    buffer.put((byte) 0);                                // ...
    buffer.put((byte) 0);                                // ...
    buffer.put((byte) 0);                                // ...
    buffer.put((byte) ((size - 54) & 0x000000ff));        // size of image data in bytes (including padding)
    buffer.put((byte) (((size - 54) & 0x0000ff00) >> 8));   // ...
    buffer.put((byte) (((size - 54) & 0x00ff0000) >> 16));  // ...
    buffer.put((byte) (((size - 54) & 0xff000000) >> 24));  // ...
    buffer.put((byte) 0);                                // horizontal resolution in pixels per meter (unreliable)
    buffer.put((byte) 0);                                // ...
    buffer.put((byte) 0);                                // ...
    buffer.put((byte) 0);                                // ...
    buffer.put((byte) 0);                                // vertical resolution in pixels per meter (unreliable)
    buffer.put((byte) 0);                                // ...
    buffer.put((byte) 0);                                // ...
    buffer.put((byte) 0);                                // ...
    buffer.put((byte) 0);                                // number of colors in image, or zero
    buffer.put((byte) 0);                                // ...
    buffer.put((byte) 0);                                // ...
    buffer.put((byte) 0);                                // ...
    buffer.put((byte) 0);                                // number of important colors, or zero
    buffer.put((byte) 0);                                // ...
    buffer.put((byte) 0);                                // ...
    buffer.put((byte) 0);                                // ...

    pixel_data.rewind();
    IntBuffer int_pixel_data = pixel_data.asIntBuffer();
    //write BMP image data
    for (int y = height - 1; y >= 0; y--) {
        for (int x = 0; x < width; x++) {
            int pixel = int_pixel_data.get(y * width + x);
            byte r = (byte) ((pixel >> 24) & 0xff);
            byte g = (byte) ((pixel >> 16) & 0xff);
            byte b = (byte) ((pixel >> 8) & 0xff);
            buffer.put(b);
            buffer.put(g);
            buffer.put(r);
        }
        for (int i = 0; i < pad; i++) {
            buffer.put((byte) 0);
        }

    }
    buffer.rewind();
    String name = filename + ".bmp";
    String image_path = System.getProperty("user.home") + File.separator + name;
    File image_file = new File(image_path);
    try {
        FileOutputStream fout = new FileOutputStream(image_file);
        fout.write(buffer.array());
        fout.flush();
        fout.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    long after = System.currentTimeMillis();
    System.out.println("File " + filename + " saved in " + (after - before) + " milliseconds");
    return name;
}
 
Example 17
Source File: CMap.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
CMapFormat12(ByteBuffer buffer, int offset, char[] xlat) {
    if (xlat != null) {
        throw new RuntimeException("xlat array for cmap fmt=12");
    }

    buffer.position(offset+12);
    numGroups = buffer.getInt() & INTMASK;
    // A map group record is three uint32's making for 12 bytes total
    if (buffer.remaining() < (12 * (long)numGroups)) {
        throw new RuntimeException("Format 12 table exceeded");
    }
    startCharCode = new long[numGroups];
    endCharCode = new long[numGroups];
    startGlyphID = new int[numGroups];
    buffer = buffer.slice();
    IntBuffer ibuffer = buffer.asIntBuffer();
    for (int i=0; i<numGroups; i++) {
        startCharCode[i] = ibuffer.get() & INTMASK;
        endCharCode[i] = ibuffer.get() & INTMASK;
        startGlyphID[i] = ibuffer.get() & INTMASK;
    }

    /* Finds the high bit by binary searching through the bits */
    int value = numGroups;

    if (value >= 1 << 16) {
        value >>= 16;
        highBit += 16;
    }

    if (value >= 1 << 8) {
        value >>= 8;
        highBit += 8;
    }

    if (value >= 1 << 4) {
        value >>= 4;
        highBit += 4;
    }

    if (value >= 1 << 2) {
        value >>= 2;
        highBit += 2;
    }

    if (value >= 1 << 1) {
        value >>= 1;
        highBit += 1;
    }

    power = 1 << highBit;
    extra = numGroups - power;
}
 
Example 18
Source File: NativeInt32Array.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Int32ArrayData createArrayData(final ByteBuffer nb, final int start, final int length) {
    return new Int32ArrayData(nb.asIntBuffer(), start, length);
}
 
Example 19
Source File: GLIntImage.java    From tribaltrouble with GNU General Public License v2.0 4 votes vote down vote up
public GLIntImage(int width, int height, ByteBuffer pixel_data, int format) {
	super(width, height, pixel_data, format);
	pixels = pixel_data.asIntBuffer();
}
 
Example 20
Source File: CursorLoader.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Get a cursor based on a image reference on the classpath. The image 
 * is assumed to be a set/strip of cursor animation frames running from top to 
 * bottom.
 * 
 * @param ref The reference to the image to be loaded
 * @param x The x-coordinate of the cursor hotspot (left -> right)
 * @param y The y-coordinate of the cursor hotspot (bottom -> top)
 * @param width The x width of the cursor
 * @param height The y height of the cursor
 * @param cursorDelays image delays between changing frames in animation
 * 					
 * @return The created cursor
 * @throws IOException Indicates a failure to load the image
 * @throws LWJGLException Indicates a failure to create the hardware cursor
 */
public Cursor getAnimatedCursor(String ref,int x,int y, int width, int height, int[] cursorDelays) throws IOException, LWJGLException {
	IntBuffer cursorDelaysBuffer = ByteBuffer.allocateDirect(cursorDelays.length*4).order(ByteOrder.nativeOrder()).asIntBuffer();
	for (int i=0;i<cursorDelays.length;i++) {
		cursorDelaysBuffer.put(cursorDelays[i]);
	}
	cursorDelaysBuffer.flip();

	LoadableImageData imageData = new TGAImageData();
	ByteBuffer buf = imageData.loadImage(ResourceLoader.getResourceAsStream(ref), false, null);
				
	return new Cursor(width, height, x, y, cursorDelays.length, buf.asIntBuffer(), cursorDelaysBuffer);
}