Search in sources :

Example 41 with ByteBuffer

use of java.nio.ByteBuffer in project titan by thinkaurelius.

the class HBaseStoreManager method getStartKey.

/**
     * This method generates the second argument to
     * {@link HBaseAdmin#createTable(HTableDescriptor, byte[], byte[], int)}.
     * <p/>
     * From the {@code createTable} javadoc:
     * "The start key specified will become the end key of the first region of
     * the table, and the end key specified will become the start key of the
     * last region of the table (the first region has a null start key and
     * the last region has a null end key)"
     * <p/>
     * To summarize, the {@code createTable} argument called "startKey" is
     * actually the end key of the first region.
     */
private byte[] getStartKey(int regionCount) {
    ByteBuffer regionWidth = ByteBuffer.allocate(4);
    regionWidth.putInt((int) (((1L << 32) - 1L) / regionCount)).flip();
    return StaticArrayBuffer.of(regionWidth).getBytes(0, 4);
}
Also used : ByteBuffer(java.nio.ByteBuffer)

Example 42 with ByteBuffer

use of java.nio.ByteBuffer in project playn by threerings.

the class AndroidSurfaceGL method onSurfaceCreated.

@Override
public void onSurfaceCreated() {
    createTexture();
    if (cachedPixels != null) {
        try {
            AndroidGLContext actx = (AndroidGLContext) ctx;
            ByteBuffer pixelBuffer = ByteBuffer.allocate(texWidth * texHeight * 4);
            FileInputStream in = new FileInputStream(cachedPixels);
            in.read(pixelBuffer.array());
            in.close();
            int bufferTex = actx.createTexture(false, false, false);
            actx.gl.glTexImage2D(GL20.GL_TEXTURE_2D, 0, GL20.GL_RGBA, texWidth, texHeight, 0, GL20.GL_RGBA, GL20.GL_UNSIGNED_BYTE, pixelBuffer);
            // bind our surface framebuffer and render the saved texture data into it
            bindFramebuffer();
            ctx.quadShader(null).prepareTexture(bufferTex, Tint.NOOP_TINT).addQuad(StockInternalTransform.IDENTITY, 0, texHeight, texWidth, 0, 0, 0, 1, 1);
            // rebind the default frame buffer (which will flush the rendering operation)
            ctx.bindFramebuffer();
            ctx.destroyTexture(bufferTex);
            pixelBuffer = null;
            cachedPixels.delete();
            cachedPixels = null;
        } catch (IOException e) {
            PlayN.reportError("Error reading cached surface pixels from file.", e);
        }
    }
}
Also used : IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream) Tint(playn.core.Tint)

Example 43 with ByteBuffer

use of java.nio.ByteBuffer in project playn by threerings.

the class AndroidSurfaceGL method onSurfaceLost.

@Override
public void onSurfaceLost() {
    try {
        AndroidGLContext actx = (AndroidGLContext) ctx;
        bindFramebuffer();
        ByteBuffer pixelBuffer = ByteBuffer.allocate(texWidth * texHeight * 4);
        actx.gl.glReadPixels(0, 0, texWidth, texHeight, GL20.GL_RGBA, GL20.GL_UNSIGNED_BYTE, pixelBuffer);
        actx.checkGLError("glReadPixels");
        try {
            cachedPixels = new File(cacheDir, "surface-" + Integer.toHexString(hashCode()));
            FileOutputStream out = new FileOutputStream(cachedPixels);
            out.write(pixelBuffer.array());
            out.close();
        } catch (IOException e) {
            PlayN.reportError("IOException writing cached Surface to file.", e);
            cachedPixels = null;
        }
        pixelBuffer = null;
    } catch (OutOfMemoryError e) {
        PlayN.reportError("OutOfMemoryError reading cached Surface to buffer.", e);
        cachedPixels = null;
    }
    clearTexture();
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) File(java.io.File)

Example 44 with ByteBuffer

use of java.nio.ByteBuffer in project watson by totemo.

the class Screenshot method save.

// --------------------------------------------------------------------------
/**
   * Save a screenshot.
   * 
   * @param file the file to write.
   * @param width the screen width.
   * @param height the screen height.
   */
public static IChatComponent save(File file, int width, int height) {
    try {
        file.getParentFile().mkdirs();
        ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * 4);
        GL11.glReadBuffer(GL11.GL_FRONT);
        // GL11.glReadBuffer() unexpectedly sets an error state (invalid enum).
        GL11.glGetError();
        GL11.glReadPixels(0, 0, width, height, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                int i = (x + width * y) * 4;
                int r = buffer.get(i) & 0xFF;
                int g = buffer.get(i + 1) & 0xFF;
                int b = buffer.get(i + 2) & 0xFF;
                image.setRGB(x, (height - 1) - y, (0xFF << 24) | (r << 16) | (g << 8) | b);
            }
        }
        ImageIO.write(image, "png", file);
        ChatComponentText text = new ChatComponentText(file.getName());
        text.getChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.OPEN_FILE, file.getAbsolutePath()));
        text.getChatStyle().setUnderlined(Boolean.valueOf(true));
        return new ChatComponentTranslation("screenshot.success", new Object[] { text });
    } catch (Exception ex) {
        return new ChatComponentTranslation("screenshot.failure", new Object[] { ex.getMessage() });
    }
}
Also used : ChatComponentTranslation(net.minecraft.util.ChatComponentTranslation) ClickEvent(net.minecraft.event.ClickEvent) ByteBuffer(java.nio.ByteBuffer) ChatComponentText(net.minecraft.util.ChatComponentText) BufferedImage(java.awt.image.BufferedImage)

Example 45 with ByteBuffer

use of java.nio.ByteBuffer in project titan by thinkaurelius.

the class WriteByteBuffer method require.

private void require(int size) {
    if (buffer.capacity() - buffer.position() < size) {
        //Need to resize
        //extra capacity as buffer
        int newcapacity = buffer.position() + size + buffer.capacity();
        Preconditions.checkArgument(newcapacity <= MAX_BUFFER_CAPACITY, "Capacity exceeds max buffer capacity: %s", MAX_BUFFER_CAPACITY);
        ByteBuffer newBuffer = ByteBuffer.allocate(newcapacity);
        buffer.flip();
        newBuffer.put(buffer);
        buffer = newBuffer;
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBuffer (java.nio.ByteBuffer)8895 Test (org.junit.Test)1965 IOException (java.io.IOException)1016 ArrayList (java.util.ArrayList)449 File (java.io.File)224 FileChannel (java.nio.channels.FileChannel)214 MappedByteBuffer (java.nio.MappedByteBuffer)195 HashMap (java.util.HashMap)177 CharBuffer (java.nio.CharBuffer)153 ByteArrayOutputStream (java.io.ByteArrayOutputStream)145 InetSocketAddress (java.net.InetSocketAddress)139 Random (java.util.Random)127 InputStream (java.io.InputStream)124 Map (java.util.Map)119 FileInputStream (java.io.FileInputStream)116 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)99 Test (org.testng.annotations.Test)96 IntBuffer (java.nio.IntBuffer)95 SocketChannel (java.nio.channels.SocketChannel)94 FileOutputStream (java.io.FileOutputStream)93