Example usage for java.nio ByteBuffer getChar

List of usage examples for java.nio ByteBuffer getChar

Introduction

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

Prototype

public abstract char getChar(int index);

Source Link

Document

Returns the char at the specified index.

Usage

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    bb.asCharBuffer().put("java2s.com");
    char c = bb.getChar(1);
    System.out.print(c + " ");

}

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

public static boolean isMMMultipageTiff(String directory) throws IOException {
    File dir = new File(directory);
    File[] children = dir.listFiles();
    File testFile = null;/* w  w  w.  j a v  a  2 s  .  com*/
    for (File child : children) {
        if (child.isDirectory()) {
            File[] grandchildren = child.listFiles();
            for (File grandchild : grandchildren) {
                if (grandchild.getName().endsWith(".tif")) {
                    testFile = grandchild;
                    break;
                }
            }
        } else if (child.getName().endsWith(".tif") || child.getName().endsWith(".TIF")) {
            testFile = child;
            break;
        }
    }
    if (testFile == null) {
        throw new IOException("Unexpected file structure: is this an MM dataset?");
    }
    RandomAccessFile ra;
    try {
        ra = new RandomAccessFile(testFile, "r");
    } catch (FileNotFoundException ex) {
        ReportingUtils.logError(ex);
        return false;
    }
    FileChannel channel = ra.getChannel();
    ByteBuffer tiffHeader = ByteBuffer.allocate(36);
    ByteOrder bo;
    channel.read(tiffHeader, 0);
    char zeroOne = tiffHeader.getChar(0);
    if (zeroOne == 0x4949) {
        bo = ByteOrder.LITTLE_ENDIAN;
    } else if (zeroOne == 0x4d4d) {
        bo = ByteOrder.BIG_ENDIAN;
    } else {
        throw new IOException("Error reading Tiff header");
    }
    tiffHeader.order(bo);
    int summaryMDHeader = tiffHeader.getInt(32);
    channel.close();
    ra.close();
    if (summaryMDHeader == MultipageTiffWriter.SUMMARY_MD_HEADER) {
        return true;
    }
    return false;
}

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

private IFDEntry readDirectoryEntry(int offset, ByteBuffer buffer) throws IOException {
    char tag = buffer.getChar(offset);
    char type = buffer.getChar(offset + 2);
    long count = unsignInt(buffer.getInt(offset + 4));
    long value;/*from w ww .ja  v  a 2  s.c o  m*/
    if (type == 3 && count == 1) {
        value = buffer.getChar(offset + 8);
    } else {
        value = unsignInt(buffer.getInt(offset + 8));
    }
    return (new IFDEntry(tag, type, count, value));
}

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

private IFDData readIFD(long byteOffset) throws IOException {
    ByteBuffer buff = readIntoBuffer(byteOffset, 2);
    int numEntries = buff.getChar(0);

    ByteBuffer entries = readIntoBuffer(byteOffset + 2, numEntries * 12 + 4).order(byteOrder_);
    IFDData data = new IFDData();
    for (int i = 0; i < numEntries; i++) {
        IFDEntry entry = readDirectoryEntry(i * 12, entries);
        if (entry.tag == MM_METADATA) {
            data.mdOffset = entry.value;
            data.mdLength = entry.count;
        } else if (entry.tag == STRIP_OFFSETS) {
            data.pixelOffset = entry.value;
        } else if (entry.tag == STRIP_BYTE_COUNTS) {
            data.bytesPerImage = entry.value;
        }//from  www. j a  va2s.  c  o  m
    }
    data.nextIFD = unsignInt(entries.getInt(numEntries * 12));
    data.nextIFDOffsetLocation = byteOffset + 2 + numEntries * 12;
    return data;
}

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

private long readHeader() throws IOException {
    ByteBuffer tiffHeader = ByteBuffer.allocate(8);
    fileChannel_.read(tiffHeader, 0);/*from   w  ww  . j  av  a2s  .  c om*/
    char zeroOne = tiffHeader.getChar(0);
    if (zeroOne == 0x4949) {
        byteOrder_ = ByteOrder.LITTLE_ENDIAN;
    } else if (zeroOne == 0x4d4d) {
        byteOrder_ = ByteOrder.BIG_ENDIAN;
    } else {
        throw new IOException("Error reading Tiff header");
    }
    tiffHeader.order(byteOrder_);
    short twoThree = tiffHeader.getShort(2);
    if (twoThree != 42) {
        throw new IOException("Tiff identifier code incorrect");
    }
    return unsignInt(tiffHeader.getInt(4));
}