Example usage for java.nio ByteBuffer allocate

List of usage examples for java.nio ByteBuffer allocate

Introduction

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

Prototype

public static ByteBuffer allocate(int capacity) 

Source Link

Document

Creates a byte buffer based on a newly allocated byte array.

Usage

From source file:haxball.util.Serializer.java

public static byte[] intToByteArray(int value) {
    return ByteBuffer.allocate(4).putInt(value).array();
}

From source file:com.oneguy.recognize.Util.java

public static ByteBuffer doubleSize(ByteBuffer buffer) {
    if (buffer == null) {
        return null;
    }//from  ww w .j ava 2s  .c om
    byte[] content = new byte[buffer.position()];
    buffer.flip();
    buffer.get(content);
    ByteBuffer newBuffer = ByteBuffer.allocate(buffer.capacity() * 2);
    newBuffer.put(content);
    return newBuffer;
}

From source file:Main.java

/**
 * Decode/unescape a portion of a URL, to use with the query part ensure {@code plusAsBlank} is true.
 *
 * @param content     the portion to decode
 * @param charset     the charset to use
 * @param plusAsBlank if {@code true}, then convert '+' to space (e.g. for www-url-form-encoded content), otherwise leave as is.
 * @return encoded string//from w w w  .j a v  a 2  s .  co  m
 */
private static String urldecode(final String content, final Charset charset, final boolean plusAsBlank) {
    if (content == null) {
        return null;
    }
    ByteBuffer bb = ByteBuffer.allocate(content.length());
    CharBuffer cb = CharBuffer.wrap(content);
    while (cb.hasRemaining()) {
        char c = cb.get();
        if (c == '%' && cb.remaining() >= 2) {
            char uc = cb.get();
            char lc = cb.get();
            int u = Character.digit(uc, 16);
            int l = Character.digit(lc, 16);
            if (u != -1 && l != -1) {
                bb.put((byte) ((u << 4) + l));
            } else {
                bb.put((byte) '%');
                bb.put((byte) uc);
                bb.put((byte) lc);
            }
        } else if (plusAsBlank && c == '+') {
            bb.put((byte) ' ');
        } else {
            bb.put((byte) c);
        }
    }
    bb.flip();
    return charset.decode(bb).toString();
}

From source file:haxball.util.Serializer.java

public static byte[] floatToByteArray(float value) {
    return ByteBuffer.allocate(4).putFloat(value).array();
}

From source file:com.astamuse.asta4d.web.util.SecureIdGenerator.java

public static String createEncryptedURLSafeId() {
    try {/*w ww  .  j  a  v  a  2 s . com*/
        byte[] idBytes = IdGenerator.createIdBytes();

        ByteBuffer bb = ByteBuffer.allocate(idBytes.length + 4);
        bb.put(idBytes);

        // add random salt
        bb.putInt(sr.nextInt());

        MessageDigest crypt = MessageDigest.getInstance("SHA-1");
        return Base64.encodeBase64URLSafeString(crypt.digest(bb.array()));

    } catch (NoSuchAlgorithmException e) {
        // impossible
        throw new RuntimeException(e);
    }
}

From source file:com.smartitengineering.cms.api.impl.Utils.java

public static String readStringInUTF8(DataInput in) throws IOException, UnsupportedEncodingException {
    int allocationBlockSize = 2000;
    int capacity = allocationBlockSize;
    int length = 0;
    ByteBuffer buffer = ByteBuffer.allocate(allocationBlockSize);
    boolean notEof = true;
    while (notEof) {
        try {/*from w ww .  j  a v  a2  s.  c  o  m*/
            buffer.put(in.readByte());
            if (++length >= capacity) {
                capacity += allocationBlockSize;
                buffer.limit(capacity);
            }
        } catch (EOFException ex) {
            notEof = false;
        }
    }
    String string = StringUtils.newStringUtf8(Arrays.copyOf(buffer.array(), length));
    return string;
}

From source file:Main.java

/**
 * /*from   w  w  w. j a  v  a 2  s . c o m*/
 * @return Bitmap's RGBA byte array
 */
public static byte[] getImageRGBA(Bitmap inputBitmap) {
    Config config = inputBitmap.getConfig();
    ByteBuffer buffer;

    Bitmap bitmap;
    /**
     * if bitmap size is not 32*32 create scaled bitmap
     */

    if (inputBitmap.getWidth() != 32 || inputBitmap.getHeight() != 32) {
        Log.d(TAG, "bitmap resized to 32x32");
        bitmap = Bitmap.createScaledBitmap(inputBitmap, 32, 32, false);
    } else {
        bitmap = inputBitmap;
    }
    /**
     * if bitmap is not ARGB_8888 format, copy bitmap with ARGB_8888 format
     */
    if (!config.equals(Bitmap.Config.ARGB_8888)) {
        Bitmap bitmapARBG = bitmap.copy(Bitmap.Config.ARGB_8888, false);
        buffer = ByteBuffer.allocate(bitmapARBG.getByteCount());
        bitmapARBG.copyPixelsToBuffer(buffer);
        bitmapARBG.recycle();
    } else {
        buffer = ByteBuffer.allocate(bitmap.getByteCount());
        bitmap.copyPixelsToBuffer(buffer);
    }
    return buffer.array();
}

From source file:Main.java

/**
 * NOTE: Arithmetic operations in primitive types may lead to arithmetic overflow. To retain
 * precision, BigDecimal objects are used.
 *
 * @param rgb/*  w ww  .  j  av  a 2s  .c om*/
 * @return
 */
public static int[] convertRGB_8_8_8_To_HSB_32_32_32(int[] rgb) {
    int[] hsb = new int[3];
    int maxChroma = Math.max(Math.max(rgb[0], rgb[1]), rgb[2]);
    int minChroma = Math.min(Math.min(rgb[0], rgb[1]), rgb[2]);
    int diff = maxChroma - minChroma;

    // Hue
    BigDecimal hue;
    if (diff == 0) {
        hue = BigDecimal.ZERO;
    } else if (maxChroma == rgb[0]) {
        float tmp = (rgb[1] - rgb[2]) / (float) diff;
        if (tmp < 0) {
            tmp += 6 * Math.ceil(-tmp / 6.0);
        } else {
            tmp -= 6 * Math.floor(tmp / 6.0);
        }
        hue = BigDecimal.valueOf(tmp);
    } else if (maxChroma == rgb[1]) {
        hue = BigDecimal.valueOf((rgb[2] - rgb[0]) / (float) diff + 2);
    } else {
        hue = BigDecimal.valueOf((rgb[0] - rgb[1]) / (float) diff + 4);
    }
    // [0, 360] -> [0, 0xffffffff]
    hue = hue.multiply(BigDecimal.valueOf(0xffffffffL));
    hue = hue.divide(BigDecimal.valueOf(6), RoundingMode.FLOOR);
    hsb[0] = ByteBuffer.allocate(8).putLong(hue.longValue()).getInt(4);

    // Saturation
    if (maxChroma == 0) {
        hsb[1] = 0;
    } else {
        // [0, 1] -> [0, 0xffffffff]
        BigDecimal sat = BigDecimal.valueOf(diff);
        sat = sat.multiply(BigDecimal.valueOf(0xffffffffL));
        sat = sat.divide(BigDecimal.valueOf(maxChroma), RoundingMode.FLOOR);
        hsb[1] = ByteBuffer.allocate(8).putLong(sat.longValue()).getInt(4);
    }

    // Brightness
    // [0, 255] -> [0, 0xffffffff]
    BigDecimal brightness = BigDecimal.valueOf(maxChroma);
    brightness = brightness.multiply(BigDecimal.valueOf(0xffffffffL));
    brightness = brightness.divide(BigDecimal.valueOf(0xffL), RoundingMode.FLOOR);
    hsb[2] = ByteBuffer.allocate(8).putLong(brightness.longValue()).getInt(4);

    return hsb;
}

From source file:Main.java

public final static ByteBuffer borrowByteBufferSmall() {
    if (byteBuffersPoolSmall.isEmpty()) {
        return ByteBuffer.allocate(MAX_LINE_BYTES_SMALL);
    } else {/*from www.  j av  a 2s  . c o m*/
        return byteBuffersPoolSmall.remove(0);
    }
}

From source file:Main.java

public final static ByteBuffer borrowByteBufferLarge() {
    if (byteBuffersPoolLarge.isEmpty()) {
        return ByteBuffer.allocate(MAX_LINE_BYTES_LARGE);
    } else {/*from  w w  w  . j ava  2 s . c  om*/
        return byteBuffersPoolLarge.remove(0);
    }
}