Example usage for java.nio ByteBuffer asCharBuffer

List of usage examples for java.nio ByteBuffer asCharBuffer

Introduction

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

Prototype

public abstract CharBuffer asCharBuffer();

Source Link

Document

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

Usage

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

private void writeIFD(TaggedImage img) throws IOException {
    char numEntries = (char) ((firstIFD_ ? ENTRIES_PER_IFD + 4 : ENTRIES_PER_IFD));
    if (img.tags.has("Summary")) {
        img.tags.remove("Summary");
    }//ww w. ja v a  2s. c  o m
    String mdString = img.tags.toString() + " ";

    //2 bytes for number of directory entries, 12 bytes per directory entry, 4 byte offset of next IFD
    //6 bytes for bits per sample if RGB, 16 bytes for x and y resolution, 1 byte per character of MD string
    //number of bytes for pixels
    int totalBytes = 2 + numEntries * 12 + 4 + (rgb_ ? 6 : 0) + 16 + mdString.length() + bytesPerImagePixels_;
    int IFDandBitDepthBytes = 2 + numEntries * 12 + 4 + (rgb_ ? 6 : 0);

    ByteBuffer ifdBuffer = ByteBuffer.allocate(IFDandBitDepthBytes).order(BYTE_ORDER);
    CharBuffer charView = ifdBuffer.asCharBuffer();

    long tagDataOffset = filePosition_ + 2 + numEntries * 12 + 4;
    nextIFDOffsetLocation_ = filePosition_ + 2 + numEntries * 12;

    bufferPosition_ = 0;
    charView.put(bufferPosition_, numEntries);
    bufferPosition_ += 2;
    writeIFDEntry(ifdBuffer, charView, WIDTH, (char) 4, 1, imageWidth_);
    writeIFDEntry(ifdBuffer, charView, HEIGHT, (char) 4, 1, imageHeight_);
    writeIFDEntry(ifdBuffer, charView, BITS_PER_SAMPLE, (char) 3, rgb_ ? 3 : 1,
            rgb_ ? tagDataOffset : byteDepth_ * 8);
    if (rgb_) {
        tagDataOffset += 6;
    }
    writeIFDEntry(ifdBuffer, charView, COMPRESSION, (char) 3, 1, 1);
    writeIFDEntry(ifdBuffer, charView, PHOTOMETRIC_INTERPRETATION, (char) 3, 1, rgb_ ? 2 : 1);

    if (firstIFD_) {
        omeDescriptionTagPosition_ = filePosition_ + bufferPosition_;
        writeIFDEntry(ifdBuffer, charView, IMAGE_DESCRIPTION, (char) 2, 0, 0);
        ijDescriptionTagPosition_ = filePosition_ + bufferPosition_;
        writeIFDEntry(ifdBuffer, charView, IMAGE_DESCRIPTION, (char) 2, 0, 0);
    }

    writeIFDEntry(ifdBuffer, charView, STRIP_OFFSETS, (char) 4, 1, tagDataOffset);
    tagDataOffset += bytesPerImagePixels_;
    writeIFDEntry(ifdBuffer, charView, SAMPLES_PER_PIXEL, (char) 3, 1, (rgb_ ? 3 : 1));
    writeIFDEntry(ifdBuffer, charView, ROWS_PER_STRIP, (char) 3, 1, imageHeight_);
    writeIFDEntry(ifdBuffer, charView, STRIP_BYTE_COUNTS, (char) 4, 1, bytesPerImagePixels_);
    writeIFDEntry(ifdBuffer, charView, X_RESOLUTION, (char) 5, 1, tagDataOffset);
    tagDataOffset += 8;
    writeIFDEntry(ifdBuffer, charView, Y_RESOLUTION, (char) 5, 1, tagDataOffset);
    tagDataOffset += 8;
    writeIFDEntry(ifdBuffer, charView, RESOLUTION_UNIT, (char) 3, 1, 3);
    if (firstIFD_) {
        ijMetadataCountsTagPosition_ = filePosition_ + bufferPosition_;
        writeIFDEntry(ifdBuffer, charView, IJ_METADATA_BYTE_COUNTS, (char) 4, 0, 0);
        ijMetadataTagPosition_ = filePosition_ + bufferPosition_;
        writeIFDEntry(ifdBuffer, charView, IJ_METADATA, (char) 1, 0, 0);
    }
    writeIFDEntry(ifdBuffer, charView, MM_METADATA, (char) 2, mdString.length(), tagDataOffset);
    tagDataOffset += mdString.length();
    //NextIFDOffset
    ifdBuffer.putInt(bufferPosition_, (int) tagDataOffset);
    bufferPosition_ += 4;

    if (rgb_) {
        charView.put(bufferPosition_ / 2, (char) (byteDepth_ * 8));
        charView.put(bufferPosition_ / 2 + 1, (char) (byteDepth_ * 8));
        charView.put(bufferPosition_ / 2 + 2, (char) (byteDepth_ * 8));
    }
    buffers_.add(ifdBuffer);
    buffers_.add(getPixelBuffer(img));
    buffers_.add(getResolutionValuesBuffer());
    buffers_.add(ByteBuffer.wrap(getBytesFromString(mdString)));

    filePosition_ += totalBytes;
    firstIFD_ = false;
}

From source file:org.apache.mnemonic.collections.DurableHashMapNGTest.java

@BeforeClass
public void setUp() throws Exception {
    rand = Utils.createRandom();/*w  ww.jav a  2s  .c  om*/
    unsafe = Utils.getUnsafe();
    m_act = new NonVolatileMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"),
            1024 * 1024 * 1024, "./pobj_hashmaps.dat", true);
    cKEYCAPACITY = m_act.handlerCapacity();
    m_act.setBufferReclaimer(new Reclaim<ByteBuffer>() {
        @Override
        public boolean reclaim(ByteBuffer mres, Long sz) {
            System.out.println(String.format("Reclaim Memory Buffer: %X  Size: %s",
                    System.identityHashCode(mres), null == sz ? "NULL" : sz.toString()));
            System.out.println(" String buffer " + mres.asCharBuffer().toString());
            return false;
        }
    });
    m_act.setChunkReclaimer(new Reclaim<Long>() {
        @Override
        public boolean reclaim(Long mres, Long sz) {
            System.out.println(String.format("Reclaim Memory Chunk: %X  Size: %s",
                    System.identityHashCode(mres), null == sz ? "NULL" : sz.toString()));
            return false;
        }
    });

    for (long i = 0; i < cKEYCAPACITY; ++i) {
        m_act.setHandler(i, 0L);
    }
}

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

private void writeBlankIFD() throws IOException {
    //      boolean blankPixelsAlreadyWritten = blankPixelsOffset_ != -1;
    boolean blankPixelsAlreadyWritten = false;

    char numEntries = (char) (((firstIFD_ && omeTiff_) ? ENTRIES_PER_IFD + 2 : ENTRIES_PER_IFD)
            + (firstIFD_ ? 2 : 0));//from  ww w. j  a  v a  2 s. c o m

    String mdString = "NULL ";

    //2 bytes for number of directory entries, 12 bytes per directory entry, 4 byte offset of next IFD
    //6 bytes for bits per sample if RGB, 16 bytes for x and y resolution, 1 byte per character of MD string
    //number of bytes for pixels
    int totalBytes = 2 + numEntries * 12 + 4 + (rgb_ ? 6 : 0) + 16 + mdString.length()
            + (blankPixelsAlreadyWritten ? 0 : bytesPerImagePixels_);
    int IFDandBitDepthBytes = 2 + numEntries * 12 + 4 + (rgb_ ? 6 : 0);

    ByteBuffer ifdBuffer = ByteBuffer.allocate(IFDandBitDepthBytes).order(BYTE_ORDER);
    CharBuffer charView = ifdBuffer.asCharBuffer();

    long tagDataOffset = filePosition_ + 2 + numEntries * 12 + 4;
    nextIFDOffsetLocation_ = filePosition_ + 2 + numEntries * 12;

    bufferPosition_ = 0;
    charView.put(bufferPosition_, numEntries);
    bufferPosition_ += 2;
    writeIFDEntry(ifdBuffer, charView, WIDTH, (char) 4, 1, imageWidth_);
    writeIFDEntry(ifdBuffer, charView, HEIGHT, (char) 4, 1, imageHeight_);
    writeIFDEntry(ifdBuffer, charView, BITS_PER_SAMPLE, (char) 3, rgb_ ? 3 : 1,
            rgb_ ? tagDataOffset : byteDepth_ * 8);
    if (rgb_) {
        tagDataOffset += 6;
    }
    writeIFDEntry(ifdBuffer, charView, COMPRESSION, (char) 3, 1, 1);
    writeIFDEntry(ifdBuffer, charView, PHOTOMETRIC_INTERPRETATION, (char) 3, 1, rgb_ ? 2 : 1);

    if (firstIFD_ && omeTiff_) {
        omeDescriptionTagPosition_ = filePosition_ + bufferPosition_;
        writeIFDEntry(ifdBuffer, charView, IMAGE_DESCRIPTION, (char) 2, 0, 0);
    }
    if (firstIFD_) {
        ijDescriptionTagPosition_ = filePosition_ + bufferPosition_;
        writeIFDEntry(ifdBuffer, charView, IMAGE_DESCRIPTION, (char) 2, 0, 0);
    }

    if (!blankPixelsAlreadyWritten) { //Write blank pixels
        writeIFDEntry(ifdBuffer, charView, STRIP_OFFSETS, (char) 4, 1, tagDataOffset);
        blankPixelsOffset_ = tagDataOffset;
        tagDataOffset += bytesPerImagePixels_;
    } else {
        writeIFDEntry(ifdBuffer, charView, STRIP_OFFSETS, (char) 4, 1, blankPixelsOffset_);
    }

    writeIFDEntry(ifdBuffer, charView, SAMPLES_PER_PIXEL, (char) 3, 1, (rgb_ ? 3 : 1));
    writeIFDEntry(ifdBuffer, charView, ROWS_PER_STRIP, (char) 3, 1, imageHeight_);
    writeIFDEntry(ifdBuffer, charView, STRIP_BYTE_COUNTS, (char) 4, 1, bytesPerImagePixels_);
    writeIFDEntry(ifdBuffer, charView, X_RESOLUTION, (char) 5, 1, tagDataOffset);
    tagDataOffset += 8;
    writeIFDEntry(ifdBuffer, charView, Y_RESOLUTION, (char) 5, 1, tagDataOffset);
    tagDataOffset += 8;
    writeIFDEntry(ifdBuffer, charView, RESOLUTION_UNIT, (char) 3, 1, 3);
    if (firstIFD_) {
        ijMetadataCountsTagPosition_ = filePosition_ + bufferPosition_;
        writeIFDEntry(ifdBuffer, charView, IJ_METADATA_BYTE_COUNTS, (char) 4, 0, 0);
        ijMetadataTagPosition_ = filePosition_ + bufferPosition_;
        writeIFDEntry(ifdBuffer, charView, IJ_METADATA, (char) 1, 0, 0);
    }
    writeIFDEntry(ifdBuffer, charView, MM_METADATA, (char) 2, mdString.length(), tagDataOffset);
    tagDataOffset += mdString.length();
    //NextIFDOffset
    ifdBuffer.putInt(bufferPosition_, (int) tagDataOffset);
    bufferPosition_ += 4;

    if (rgb_) {
        charView.put(bufferPosition_ / 2, (char) (byteDepth_ * 8));
        charView.put(bufferPosition_ / 2 + 1, (char) (byteDepth_ * 8));
        charView.put(bufferPosition_ / 2 + 2, (char) (byteDepth_ * 8));
    }
    buffers_.add(ifdBuffer);
    if (!blankPixelsAlreadyWritten) {
        buffers_.add(ByteBuffer.wrap(new byte[bytesPerImagePixels_]));
    }
    buffers_.add(getResolutionValuesBuffer());
    buffers_.add(ByteBuffer.wrap(getBytesFromString(mdString)));

    filePosition_ += totalBytes;
    firstIFD_ = false;
}

From source file:gephi.spade.panel.fcsFile.java

/**
 * readASCIIData ---/*w  ww  . j a v  a 2 s  .  com*/
 * <p>
 * Reads ASCII values in list mode in the DATA segment and updates eventList
 * with the integer values of the values.
 * </p>
 *
 * @param data
 *            <code>ByteBuffer</code> containing the DATA segment of the
 *            underlying file.
 */
private void readASCIIData(ByteBuffer data) {
    /**
     * Calculate the number of characters in each event of the flow file
     */
    // Initialize the number of characters in each event to 0
    int numCharsPerEvent = 0;

    // Loop through all the parameters adding the number of characters in
    // each parameter
    for (int i = 0; i < parameters; i++) {
        numCharsPerEvent += channelBits[i];
    }

    // Allocate the eventList
    eventList = new double[parameters][totalEvents];

    // Convert the byte buffer into a char buffer - doesn't get any easier
    CharBuffer cb = data.asCharBuffer();

    // Initialize the current character to 0
    int currChar = 0;

    for (int i = 0; i < totalEvents; i++) {
        for (int j = 0; j < parameters; j++) {
            try {
                // Store the value into the array
                eventList[j][i] = Integer
                        .parseInt(cb.subSequence(currChar, currChar + channelBits[i]).toString());
            } catch (NumberFormatException nfe) {
                eventList[j][i] = 0;
            }

            // Increment the current character
            currChar += channelBits[i];
        }
    }
}