Example usage for java.nio ByteBuffer asLongBuffer

List of usage examples for java.nio ByteBuffer asLongBuffer

Introduction

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

Prototype

public abstract LongBuffer asLongBuffer();

Source Link

Document

Returns a long buffer which is based on the remaining content of this byte buffer.

Usage

From source file:MainClass.java

private static void createFile() throws Exception {
    long[] primes = new long[] { 1, 2, 3, 5, 7 };
    File aFile = new File("C:/primes.bin");
    FileOutputStream outputFile = new FileOutputStream(aFile);
    FileChannel file = outputFile.getChannel();
    final int BUFFERSIZE = 100;
    ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE);
    LongBuffer longBuf = buf.asLongBuffer();
    int primesWritten = 0;
    while (primesWritten < primes.length) {
        longBuf.put(primes, primesWritten, min(longBuf.capacity(), primes.length - primesWritten));
        buf.limit(8 * longBuf.position());

        file.write(buf);/*from w w  w .  j  av  a2 s .  c  om*/
        primesWritten += longBuf.position();
        longBuf.clear();
        buf.clear();
    }
    System.out.println("File written is " + file.size() + "bytes.");
    outputFile.close();
}

From source file:Main.java

public static LongBuffer newLongBuffer(int numLongs) {
    ByteBuffer buffer = ByteBuffer.allocate(numLongs * 8);
    buffer.order(ByteOrder.nativeOrder());
    return buffer.asLongBuffer();
}

From source file:Main.java

public static LongBuffer newLongBuffer(int numLongs) {
    ByteBuffer buffer = ByteBuffer.allocateDirect(numLongs * 8);
    buffer.order(ByteOrder.nativeOrder());
    return buffer.asLongBuffer();
}

From source file:ome.io.bioformats.BfPixelsWrapper.java

/**
 * cgb - stolen from ImportLibrary - slightly modified
 *
 * Examines a byte array to see if it needs to be byte swapped and modifies
 * the byte array directly.//  w  ww .j av  a 2s .c  o  m
 * @param bytes The byte array to check and modify if required.
 * @return the <i>byteArray</i> either swapped or not for convenience.
 * @throws IOException if there is an error read from the file.
 * @throws FormatException if there is an error during metadata parsing.
 */
public byte[] swapIfRequired(byte[] bytes) throws FormatException, IOException {
    // We've got nothing to do if the samples are only 8-bits wide.
    if (pixelSize == 1)
        return bytes;

    boolean isLittleEndian = reader.isLittleEndian();
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    int length;
    if (isLittleEndian) {
        if (pixelSize == 2) { // short/ushort
            ShortBuffer buf = buffer.asShortBuffer();
            length = buffer.limit() / 2;
            for (int i = 0; i < length; i++) {
                buf.put(i, DataTools.swap(buf.get(i)));
            }
        } else if (pixelSize == 4) { // int/uint/float
            IntBuffer buf = buffer.asIntBuffer();
            length = buffer.limit() / 4;
            for (int i = 0; i < length; i++) {
                buf.put(i, DataTools.swap(buf.get(i)));
            }
        } else if (pixelSize == 8) // long/double
        {
            LongBuffer buf = buffer.asLongBuffer();
            length = buffer.limit() / 8;
            for (int i = 0; i < length; i++) {
                buf.put(i, DataTools.swap(buf.get(i)));
            }
        } else {
            throw new FormatException(String.format("Unsupported sample bit width: %d", pixelSize));
        }
    }
    // We've got a big-endian file with a big-endian byte array.
    bytes = buffer.array();
    return bytes;
}

From source file:org.bimserver.utils.BinUtils.java

public static byte[] longToByteArray(long inLong) {
    byte[] bArray = new byte[8];
    ByteBuffer bBuffer = ByteBuffer.wrap(bArray);
    LongBuffer lBuffer = bBuffer.asLongBuffer();
    lBuffer.put(inLong);//from w w w . j  a  va 2s.  c o  m
    return bArray;
}

From source file:org.jclouds.kinetic.strategy.internal.KineticStorageStrategyImpl.java

private Blob createBlobFromByteSource(final String container, final String key, final ByteSource byteSource) {
    BlobBuilder builder = blobBuilders.get();
    builder.name(key);//from ww w. ja va2  s. c o  m
    File file = getFileForBlobKey(container, key);
    try {
        String cacheControl = null;
        String contentDisposition = null;
        String contentEncoding = null;
        String contentLanguage = null;
        String contentType = null;
        HashCode hashCode = null;
        Date expires = null;
        ImmutableMap.Builder<String, String> userMetadata = ImmutableMap.builder();

        UserDefinedFileAttributeView view = getUserDefinedFileAttributeView(file.toPath());
        if (view != null) {
            Set<String> attributes = ImmutableSet.copyOf(view.list());

            cacheControl = readStringAttributeIfPresent(view, attributes, XATTR_CACHE_CONTROL);
            contentDisposition = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_DISPOSITION);
            contentEncoding = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_ENCODING);
            contentLanguage = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_LANGUAGE);
            contentType = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_TYPE);
            if (contentType == null && autoDetectContentType) {
                contentType = probeContentType(file.toPath());
            }
            if (attributes.contains(XATTR_CONTENT_MD5)) {
                ByteBuffer buf = ByteBuffer.allocate(view.size(XATTR_CONTENT_MD5));
                view.read(XATTR_CONTENT_MD5, buf);
                hashCode = HashCode.fromBytes(buf.array());
            }
            if (attributes.contains(XATTR_EXPIRES)) {
                ByteBuffer buf = ByteBuffer.allocate(view.size(XATTR_EXPIRES));
                view.read(XATTR_EXPIRES, buf);
                buf.flip();
                expires = new Date(buf.asLongBuffer().get());
            }
            for (String attribute : attributes) {
                if (!attribute.startsWith(XATTR_USER_METADATA_PREFIX)) {
                    continue;
                }
                String value = readStringAttributeIfPresent(view, attributes, attribute);
                userMetadata.put(attribute.substring(XATTR_USER_METADATA_PREFIX.length()), value);
            }

            builder.payload(byteSource).cacheControl(cacheControl).contentDisposition(contentDisposition)
                    .contentEncoding(contentEncoding).contentLanguage(contentLanguage)
                    .contentLength(byteSource.size()).contentMD5(hashCode).contentType(contentType)
                    .expires(expires).userMetadata(userMetadata.build());
        } else {
            builder.payload(byteSource).contentLength(byteSource.size())
                    .contentMD5(byteSource.hash(Hashing.md5()).asBytes());
        }
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
    Blob blob = builder.build();
    blob.getMetadata().setContainer(container);
    blob.getMetadata().setLastModified(new Date(file.lastModified()));
    blob.getMetadata().setSize(file.length());
    if (blob.getPayload().getContentMetadata().getContentMD5() != null)
        blob.getMetadata()
                .setETag(base16().lowerCase().encode(blob.getPayload().getContentMetadata().getContentMD5()));
    return blob;
}