Example usage for java.nio ByteBuffer array

List of usage examples for java.nio ByteBuffer array

Introduction

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

Prototype

public final byte[] array() 

Source Link

Document

Returns the byte array which this buffer is based on, if there is one.

Usage

From source file:Main.java

public static byte[] makeFontBitmap(String font, String code, int size, float[] arrayOfPos) {
    Canvas c = new Canvas();
    Paint p = new Paint();
    float density = context.getResources().getDisplayMetrics().density;
    //        Log.v(TAG, String.format("makeFontBitmap called(Java): font=%s code=%s density=%f", font, code, density));
    p.setTextSize((float) size * density);
    p.setAntiAlias(true);/*from   w  w  w.j a  va  2 s  . c  om*/

    Rect textBounds = new Rect();
    p.getTextBounds(code, 0, code.length(), textBounds);
    //        Log.v(TAG, String.format("makeFontBitmap textBounds: %d,%d,%d,%d", textBounds.left, textBounds.top, textBounds.right, textBounds.bottom));

    Rect textBoundsAxA = new Rect();
    String axa = String.format("A%sA", code);
    p.getTextBounds(axa, 0, axa.length(), textBoundsAxA);
    Rect textBoundsAA = new Rect();
    String aa = "AA";
    p.getTextBounds(aa, 0, aa.length(), textBoundsAA);

    // cache.distDelta = Vec2(0, 0);
    arrayOfPos[0] = textBounds.left;
    arrayOfPos[1] = textBounds.top;

    // cache.srcWidth = Vec2(16, 16);
    arrayOfPos[2] = textBounds.width();
    arrayOfPos[3] = textBounds.height();

    // cache.step = 16;
    //      arrayOfPos[4] = textBounds.width() + 1;
    arrayOfPos[4] = textBoundsAxA.width() - textBoundsAA.width();

    if (textBounds.width() == 0 || textBounds.height() == 0) {
        Log.v(TAG, "makeFontBitmap: empty");
        return null;
    }

    Bitmap b = Bitmap.createBitmap(textBounds.width(), textBounds.height(), Bitmap.Config.ARGB_8888);
    c.setBitmap(b);

    Rect r = new Rect(0, 0, textBounds.width(), textBounds.height());
    //      p.setColor(Color.RED);
    p.setARGB(0, 0, 0, 0);
    c.drawRect(r, p);
    p.setARGB(255, 255, 255, 255);

    //        Log.v(TAG, "makeFontBitmap: drawText");
    c.drawText(code, -textBounds.left, -textBounds.top, p);
    //        Log.v(TAG, String.format("makeFontBitmap: w=%.2f h=%.2f", arrayOfPos[2], arrayOfPos[3]));

    ByteBuffer buf = ByteBuffer.allocate(textBounds.width() * textBounds.height() * 4);
    //        Log.v(TAG, String.format("makeFontBitmap: b.getRowBytes() %d", b.getRowBytes()));
    buf.position(0);
    b.copyPixelsToBuffer(buf);
    //        Log.v(TAG, String.format("makeFontBitmap results: capacity=%d", buf.capacity()));

    return buf.array();
}

From source file:com.icloud.framework.core.util.FBUtilities.java

public static void writeByteArray(ByteBuffer bytes, DataOutput out) throws IOException {
    out.writeInt(bytes.remaining());//from ww  w.j  a  v  a  2s.  c o  m
    out.write(bytes.array(), bytes.position() + bytes.arrayOffset(), bytes.remaining());
}

From source file:edu.berkeley.sparrow.examples.ProtoFrontendAsync.java

public static List<TTaskSpec> generateJob(int numTasks, int benchmarkId, int benchmarkIterations) {
    // Pack task parameters
    ByteBuffer message = ByteBuffer.allocate(8);
    message.putInt(benchmarkId);// www .ja v a 2s .  c  o m
    message.putInt(benchmarkIterations);

    List<TTaskSpec> out = new ArrayList<TTaskSpec>();
    for (int taskId = 0; taskId < numTasks; taskId++) {
        TTaskSpec spec = new TTaskSpec();
        spec.setTaskId(Integer.toString(taskId));
        spec.setMessage(message.array());
        out.add(spec);
    }
    return out;
}

From source file:topoos.APIAccess.mime.HttpMultipart.java

/**
 * Encode./*  w ww  .j a va 2s .  c o m*/
 *
 * @param charset the charset
 * @param string the string
 * @return the byte array buffer
 */
private static ByteArrayBuffer encode(final Charset charset, final String string) {
    ByteBuffer encoded = charset.encode(CharBuffer.wrap(string));
    ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining());
    bab.append(encoded.array(), encoded.position(), encoded.remaining());
    return bab;
}

From source file:io.github.dsheirer.record.wave.WaveWriter.java

public static String toString(ByteBuffer buffer) {
    StringBuilder sb = new StringBuilder();

    byte[] bytes = buffer.array();

    for (byte b : bytes) {
        sb.append(String.format("%02X ", b));
        sb.append(" ");
    }// w w w  .j ava  2 s .c  om

    return sb.toString();
}

From source file:com.codelanx.codelanxlib.util.auth.UUIDFetcher.java

/**
 * Converts a {@link UUID} into bytes/* ww w. j  a  va  2 s  .  c  om*/
 * 
 * @since 0.0.1
 * @version 0.0.1
 * 
 * @param uuid The {@link UUID} to convert
 * @return The new byte array
 */
public static byte[] toBytes(UUID uuid) {
    ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
    byteBuffer.putLong(uuid.getMostSignificantBits());
    byteBuffer.putLong(uuid.getLeastSignificantBits());
    return byteBuffer.array();
}

From source file:io.stallion.utils.GeneralUtils.java

/**
 * Turn the given long id into a random base32 string token. This can be used for generating unique, secret strings
 * for accessing data, such as a web page only viewable by a secret string. By using the long id, of the
 * underlying object we guarantee uniqueness, by adding on  random characters, we make the URL nigh
 * impossible to guess.// ww w .j av  a  2  s. c om
 *
 * @param id - a long id that will be convered to base32 and used as the first part of the string
 * @param length - the number of random base32 characters to add to the end of the string
 * @return
 */
public static String tokenForId(Long id, int length) {
    ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
    buffer.putLong(id);
    return StringUtils.stripStart(new Base32().encodeAsString(buffer.array()), "A").replace("=", "")
            .toLowerCase() + "8" + randomTokenBase32(length);
}

From source file:org.lable.rfc3881.auditlogger.adapter.hbase.HBaseAdapter.java

static byte[] columnQualifierSuffixFor(Identifiable identifiable) {
    List<String> parts = identifiable.identifyingStack();
    // Account for the separator bytes.
    int targetLength = parts.size() - 1;
    for (String part : parts) {
        if (part != null) {
            targetLength += part.length();
        }//from  w w w .  j  a v  a 2  s  .  c  o m
    }

    ByteBuffer buffer = ByteBuffer.allocate(targetLength);
    boolean first = true;
    for (String part : parts) {
        if (!first) {
            buffer.put(NULL_BYTE);
        } else {
            first = false;
        }

        if (part != null) {
            buffer.put(toBytes(part));
        }
    }

    return buffer.array();
}

From source file:com.mozilla.bagheera.util.IdUtil.java

/**
 * Takes a given id and prefixes it with a byte character and the date in a non-random fashion
 * This method expects id to be something like a UUID consisting only of hex characters.  If id 
 * is something else this will produce unpredictable results.
 * @param id/*from  w ww .ja  va 2s.com*/
 * @param d
 * @return
 * @throws IOException
 */
public static byte[] nonRandByteBucketizeId(String id, Date d) throws IOException {
    if (StringUtils.isBlank(id)) {
        throw new IllegalArgumentException("id cannot be null or empty");
    }
    if (d == null) {
        throw new IllegalArgumentException("date cannot be null");
    }

    // bucket byte + SDF bytes + id bytes
    ByteBuffer buf = ByteBuffer.allocate(9 + id.length());
    int bucket = 0;
    if (id.length() >= 2) {
        // Munge two hex characters into the range of a single byte
        bucket = Integer.parseInt(id.substring(0, 2), 16) - 128;
    } else {
        bucket = Integer.parseInt(id, 16) - 128;
    }
    buf.put((byte) bucket);
    buf.put(SDF.format(d).getBytes());
    buf.put(id.getBytes());

    return buf.array();
}

From source file:com.twitter.distributedlog.DLMTestUtil.java

public static LogRecord getLogRecordInstance(long txId, int size) {
    ByteBuffer buf = ByteBuffer.allocate(size);
    return new LogRecord(txId, buf.array());
}