use of loci.common.RandomAccessInputStream in project bioformats by openmicroscopy.
the class FormatReader method isThisType.
// -- IFormatReader API methods --
/**
* Checks if a file matches the type of this format reader.
* Checks filename suffixes against those known for this format.
* If the suffix check is inconclusive and the open parameter is true,
* the file is opened and tested with
* {@link #isThisType(RandomAccessInputStream)}.
*
* @param open If true, and the file extension is insufficient to determine
* the file type, the (existing) file is opened for further analysis.
*/
@Override
public boolean isThisType(String name, boolean open) {
// if file extension ID is insufficient and we can't open the file, give up
if (!suffixSufficient && !open)
return false;
if (suffixNecessary || suffixSufficient) {
// it's worth checking the file extension
boolean suffixMatch = super.isThisType(name);
// if suffix match is required but it doesn't match, failure
if (suffixNecessary && !suffixMatch)
return false;
// if suffix matches and that's all we need, green light it
if (suffixMatch && suffixSufficient)
return true;
}
// not allowed to open any files
if (!open)
return false;
try {
RandomAccessInputStream stream = new RandomAccessInputStream(name);
boolean isThisType = isThisType(stream);
stream.close();
return isThisType;
} catch (IOException exc) {
LOGGER.debug("", exc);
return false;
}
}
use of loci.common.RandomAccessInputStream in project bioformats by openmicroscopy.
the class FormatReader method isThisType.
/* @see IFormatReader#isThisType(byte[]) */
@Override
public boolean isThisType(byte[] block) {
try {
RandomAccessInputStream stream = new RandomAccessInputStream(block);
boolean isThisType = isThisType(stream);
stream.close();
return isThisType;
} catch (IOException e) {
LOGGER.debug("", e);
}
return false;
}
use of loci.common.RandomAccessInputStream in project bioformats by openmicroscopy.
the class TestTools method mapFile.
/**
* Map the given file into memory.
*
* @return true if the mapping was successful.
*/
public static boolean mapFile(String id) throws IOException {
RandomAccessInputStream stream = new RandomAccessInputStream(id);
Runtime rt = Runtime.getRuntime();
long maxMem = rt.freeMemory();
long length = stream.length();
if (length < Integer.MAX_VALUE && length < maxMem) {
stream.close();
FileInputStream fis = new FileInputStream(id);
FileChannel channel = fis.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
ByteArrayHandle handle = new ByteArrayHandle(buf);
Location.mapFile(id, handle);
fis.close();
return true;
}
stream.close();
return false;
}
use of loci.common.RandomAccessInputStream in project bioformats by openmicroscopy.
the class WritePrecompressedPlanes method main.
public static void main(String[] args) throws FormatException, IOException {
// print usage if no args specified
if (args.length == 0) {
System.out.println("Usage: java WritePrecompressedPlanes " + "[input file 1] ... [input file n] [output file]");
System.exit(0);
}
// first n - 1 args are the input files
String[] inputFiles = new String[args.length - 1];
System.arraycopy(args, 0, inputFiles, 0, inputFiles.length);
// last arg is the output file
String outputFile = args[args.length - 1];
// open up one of the input files so that we can read the metadata
// this assumes that the dimensions of the input files are the same
ImageReader reader = new ImageReader();
reader.setId(inputFiles[0]);
int pixelType = reader.getPixelType();
// write the pixel data to the output file
RandomAccessOutputStream out = new RandomAccessOutputStream(outputFile);
TiffSaver saver = new TiffSaver(out, outputFile);
saver.setWritingSequentially(true);
saver.setLittleEndian(reader.isLittleEndian());
saver.setBigTiff(false);
saver.writeHeader();
for (int i = 0; i < inputFiles.length; i++) {
RandomAccessInputStream in = new RandomAccessInputStream(inputFiles[i]);
byte[] buf = new byte[(int) in.length()];
in.readFully(buf);
in.close();
IFD ifd = new IFD();
ifd.put(IFD.IMAGE_WIDTH, reader.getSizeX());
ifd.put(IFD.IMAGE_LENGTH, reader.getSizeY());
ifd.put(IFD.LITTLE_ENDIAN, reader.isLittleEndian());
ifd.put(IFD.SAMPLE_FORMAT, FormatTools.isSigned(pixelType) ? 2 : FormatTools.isFloatingPoint(pixelType) ? 3 : 1);
ifd.put(IFD.PLANAR_CONFIGURATION, 1);
ifd.put(IFD.REUSE, out.length());
out.seek(out.length());
// this is very important
// the data is already compressed in a single chunk, so setting the
// number of rows per strip to something smaller than the full height
// will require us to re-compress the data
ifd.put(IFD.ROWS_PER_STRIP, reader.getSizeY());
saver.writeImage(buf, ifd, i, pixelType, 0, 0, reader.getSizeX(), reader.getSizeY(), i == inputFiles.length - 1, reader.getRGBChannelCount(), true);
}
reader.close();
out.close();
// reset the TIFF file's compression flag
// you cannot do this before the pixel data is written, otherwise
// the pixels will be re-compressed
saver = new TiffSaver(outputFile);
for (int i = 0; i < inputFiles.length; i++) {
RandomAccessInputStream in = new RandomAccessInputStream(outputFile);
saver.overwriteLastIFDOffset(in);
saver.overwriteIFDValue(in, i, IFD.COMPRESSION, TiffCompression.JPEG.getCode());
in.close();
}
saver.getStream().close();
}
use of loci.common.RandomAccessInputStream in project bioformats by openmicroscopy.
the class IOTester method testSequential.
/**
* Searches for the divider tag using repeated readChar() calls.
*/
public long testSequential(String filename) throws IOException {
LOGGER.info("Searching for divider tag sequentially...");
long start = System.currentTimeMillis();
RandomAccessInputStream in = new RandomAccessInputStream(filename);
int matchIndex = 0;
char matchChar = TAG.charAt(0);
long inputLen = in.length();
for (long i = 0; i < inputLen; i++) {
char c = in.readChar();
if (c == matchChar) {
matchIndex++;
if (matchIndex == TAG.length()) {
break;
} else {
matchChar = TAG.charAt(matchIndex);
}
} else {
matchIndex = 0;
matchChar = TAG.charAt(0);
}
}
long offset = in.getFilePointer();
in.close();
long end = System.currentTimeMillis();
LOGGER.info("Search result: {} -- in {} ms", offset, end - start);
return offset;
}
Aggregations