Example usage for java.nio ByteBuffer putDouble

List of usage examples for java.nio ByteBuffer putDouble

Introduction

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

Prototype

public abstract ByteBuffer putDouble(int index, double value);

Source Link

Document

Writes the given double to the specified index of this buffer.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer bbuf = ByteBuffer.allocate(10);
    int capacity = bbuf.capacity(); // 10
    System.out.println(capacity);
    bbuf.putDouble(2, 123);

    System.out.println(Arrays.toString(bbuf.array()));
}

From source file:edu.mbl.jif.imaging.mmtiff.MultipageTiffWriter.java

/**
 * writes channel LUTs and display ranges for composite mode Could also be
 * expanded to write ROIs, file info, slice labels, and overlays
 *//*from  w  w  w. j av  a  2s  .  c om*/
private void writeImageJMetadata(int numChannels, String summaryComment) throws IOException {
    String info = summaryMDString_;
    if (summaryComment != null && summaryComment.length() > 0) {
        info = "Acquisition comments: \n" + summaryComment + "\n\n\n" + summaryMDString_;
    }
    //size entry (4 bytes) + 4 bytes file info size + 4 bytes for channel display 
    //ranges length + 4 bytes per channel LUT
    int mdByteCountsBufferSize = 4 + 4 + 4 + 4 * numChannels;
    int bufferPosition = 0;

    ByteBuffer mdByteCountsBuffer = ByteBuffer.allocate(mdByteCountsBufferSize).order(BYTE_ORDER);

    //nTypes is number actually written among: fileInfo, slice labels, display ranges, channel LUTS,
    //slice labels, ROI, overlay, and # of extra metadata entries
    int nTypes = 3; //file info, display ranges, and channel LUTs
    int mdBufferSize = 4 + nTypes * 8;

    //Header size: 4 bytes for magic number + 8 bytes for label (int) and count (int) of each type
    mdByteCountsBuffer.putInt(bufferPosition, 4 + nTypes * 8);
    bufferPosition += 4;

    //2 bytes per a character of file info
    mdByteCountsBuffer.putInt(bufferPosition, 2 * info.length());
    bufferPosition += 4;
    mdBufferSize += info.length() * 2;

    //display ranges written as array of doubles (min, max, min, max, etc)
    mdByteCountsBuffer.putInt(bufferPosition, numChannels * 2 * 8);
    bufferPosition += 4;
    mdBufferSize += numChannels * 2 * 8;

    for (int i = 0; i < numChannels; i++) {
        //768 bytes per LUT
        mdByteCountsBuffer.putInt(bufferPosition, 768);
        bufferPosition += 4;
        mdBufferSize += 768;
    }

    //Header (1) File info (1) display ranges (1) LUTS (1 per channel)
    int numMDEntries = 3 + numChannels;
    ByteBuffer ifdCountAndValueBuffer = ByteBuffer.allocate(8).order(BYTE_ORDER);
    ifdCountAndValueBuffer.putInt(0, numMDEntries);
    ifdCountAndValueBuffer.putInt(4, (int) filePosition_);
    fileChannel_.write(ifdCountAndValueBuffer, ijMetadataCountsTagPosition_ + 4);

    fileChannel_.write(mdByteCountsBuffer, filePosition_);
    filePosition_ += mdByteCountsBufferSize;

    //Write metadata types and counts
    ByteBuffer mdBuffer = ByteBuffer.allocate(mdBufferSize).order(BYTE_ORDER);
    bufferPosition = 0;

    //All the ints declared below are non public field in TiffDecoder
    final int ijMagicNumber = 0x494a494a;
    mdBuffer.putInt(bufferPosition, ijMagicNumber);
    bufferPosition += 4;

    //Write ints for each IJ metadata field and its count
    final int fileInfo = 0x696e666f;
    mdBuffer.putInt(bufferPosition, fileInfo);
    bufferPosition += 4;
    mdBuffer.putInt(bufferPosition, 1);
    bufferPosition += 4;

    final int displayRanges = 0x72616e67;
    mdBuffer.putInt(bufferPosition, displayRanges);
    bufferPosition += 4;
    mdBuffer.putInt(bufferPosition, 1);
    bufferPosition += 4;

    final int luts = 0x6c757473;
    mdBuffer.putInt(bufferPosition, luts);
    bufferPosition += 4;
    mdBuffer.putInt(bufferPosition, numChannels);
    bufferPosition += 4;

    //write actual metadata
    //FileInfo
    for (char c : info.toCharArray()) {
        mdBuffer.putChar(bufferPosition, c);
        bufferPosition += 2;
    }
    try {
        JSONArray channels = masterMPTiffStorage_.getDisplayAndComments().getJSONArray("Channels");
        JSONObject channelSetting;
        for (int i = 0; i < numChannels; i++) {
            channelSetting = channels.getJSONObject(i);
            //Display Ranges: For each channel, write min then max
            mdBuffer.putDouble(bufferPosition, channelSetting.getInt("Min"));
            bufferPosition += 8;
            mdBuffer.putDouble(bufferPosition, channelSetting.getInt("Max"));
            bufferPosition += 8;
        }

        //LUTs
        for (int i = 0; i < numChannels; i++) {
            channelSetting = channels.getJSONObject(i);
            LUT lut = ImageUtils.makeLUT(new Color(channelSetting.getInt("Color")),
                    channelSetting.getDouble("Gamma"));
            for (byte b : lut.getBytes()) {
                mdBuffer.put(bufferPosition, b);
                bufferPosition++;
            }
        }
    } catch (JSONException ex) {
        ReportingUtils.logError(
                "Problem with displayAndComments: Couldn't write ImageJ display settings as a result");
    }

    ifdCountAndValueBuffer = ByteBuffer.allocate(8).order(BYTE_ORDER);
    ifdCountAndValueBuffer.putInt(0, mdBufferSize);
    ifdCountAndValueBuffer.putInt(4, (int) filePosition_);
    fileChannel_.write(ifdCountAndValueBuffer, ijMetadataTagPosition_ + 4);

    fileChannel_.write(mdBuffer, filePosition_);
    filePosition_ += mdBufferSize;
}