/** * Get the # of boolean values in this record * * @return number of boolean values contained in the record * @throws IOException */ public int getNumEntries(IRandomAccess is) throws IOException { long oldPos = is.getFilePointer(); try { is.seek(offset+4); return is.readInt(); } finally { is.seek(oldPos); } }
/** Read bytes from the stream into the given array. */ @Override public int read(byte[] array) throws IOException { int rtn = raf.read(array); if (rtn == 0 && raf.getFilePointer() >= raf.length() - 1) rtn = -1; return rtn; }
/** * Get the boolean value at the given index * * @param is the stream for the IM3 file * @param index the zero-based index of the entry to retrieve * @return the value stored in the indexed slot of the record * @throws IOException */ public boolean getEntry(IRandomAccess is, int index) throws IOException { long oldPos = is.getFilePointer(); try { is.seek(offset+8+index); return (is.readByte() != 0); } finally { is.seek(oldPos); } }
/** Advances the current offset by the given number of bytes. */ public void skipBytes(int skip) throws IOException { outputFile.seek(outputFile.getFilePointer() + skip); }
IRandomAccess is = Location.getHandle(getCurrentFile(), false); try { is.setOrder(ByteOrder.LITTLE_ENDIAN); final ContainerRecord dataSet = dataSets.get(getSeries()); for (IM3Record subRec:dataSet.parseChunks(is)){ if (subRec.name.equals(FIELD_DATA)) { is.seek(subRec.offset+4); int width = is.readInt(); int height = is.readInt(); int channels = is.readInt(); final byte [] result = new byte [width * height * channels * 2]; is.read(result); return result; is.close();
is.setOrder(ByteOrder.LITTLE_ENDIAN); final int cookie = is.readInt(); if (cookie != COOKIE) { throw new FormatException(String.format("Expected file cookie of %d, but got %d.", COOKIE, cookie)); long fileLength = is.length(); while (is.getFilePointer() < fileLength) { final IM3Record rec = parseRecord(is); if (rec == null) { if (is.getFilePointer() > fileLength-16) break; final int chunkLength = is.readInt(); final int unknown = is.readInt(); final int unknown1 = is.readInt(); final int nRecords = is.readInt(); } else { if (rec instanceof ContainerRecord) { is.close();
/** * Get the floating-point value at the given index * * @param is the stream for the IM3 file * @param index the zero-based index of the entry to retrieve * @return the value stored in the indexed slot of the record * @throws IOException */ public float getEntry(IRandomAccess is, int index) throws IOException { long oldPos = is.getFilePointer(); try { is.seek(offset); if (is.readInt() == 0) return is.readFloat(); is.seek(offset+8+index*4); return is.readFloat(); } finally { is.seek(oldPos); } } /**
/** Returns true if the given filename is a Zip file. */ public static boolean isZipFile(String file) throws IOException { if (!file.toLowerCase().endsWith(".zip")) return false; IRandomAccess handle = getHandle(file); byte[] b = new byte[2]; if (handle.length() >= 2) { handle.read(b); } handle.close(); return new String(b, Constants.ENCODING).equals("PK"); }
/** * Return all entries as an array * @param is handle to file * @return an array of the stored values * @throws IOException */ public float [] getEntries(IRandomAccess is) throws IOException { final long oldPos = is.getFilePointer(); try { float [] values = new float[getNumEntries(is)]; is.seek(offset+8); for (int index=0; index < values.length; index++) { values[index] = is.readFloat(); } return values; } finally { is.seek(oldPos); } } /* (non-Javadoc)
/** * Parse an IM3 record at the current file pointer location * * @param is random access stream, pointing at the record's start * (the length-quadword of the record's tag name) * @return an IM3Record or subclass depending on the record's type * @throws IOException on file misparsing leading to overrun and other */ private static IM3Record parseRecord(IRandomAccess is) throws IOException { final String name = parseString(is); if (name == null) return null; final int recLength = is.readInt()-8; final int recType = is.readInt(); final long offset = is.getFilePointer(); is.skipBytes(recLength); switch(recType) { case REC_CONTAINER: return new ContainerRecord(name, recType, offset, recLength); case REC_STRING: return new StringIM3Record(name, recType, offset, recLength); case REC_INT: return new IntIM3Record(name, recType, offset, recLength); case REC_FLOAT: return new FloatIM3Record(name, recType, offset, recLength); case REC_BOOLEAN: return new BooleanIM3Record(name, recType, offset, recLength); } return new IM3Record(name, recType, offset, recLength); } /* (non-Javadoc)
/** * Seeks to the given offset within the stream. * * @param pos the offset to which to seek * @throws IOException if the seek is not successful */ public void seek(long pos) throws IOException { raf.seek(pos); }
/** Closes the streams. */ @Override public void close() throws IOException { if (LOGGER.isTraceEnabled()) { LOGGER.trace("RandomAccessInputStream {} CLOSE", hashCode()); } if (Location.getMappedFile(file) != null) return; if (raf != null) raf.close(); raf = null; markedPos = -1; }
/** * Parse a string from the IM3 file at the current file pointer loc * * @param is stream to read from * @return parsed string or null for string of zero length * @throws IOException */ static protected String parseString(IRandomAccess is) throws IOException { final int nameLength = is.readInt(); if (nameLength == 0) return EMPTY_STRING; final byte [] buf = new byte [nameLength]; is.read(buf); return new String(buf, loci.common.Constants.ENCODING); }
/** * Write a summary of each field in the IM3 file to the writer * @throws IOException */ public void writeSummary() throws IOException { IRandomAccess is = Location.getHandle(getCurrentFile(), false); try { is.setOrder(ByteOrder.LITTLE_ENDIAN); for (IM3Record rec: records) { rec.writeSummary(is, ""); } } finally { is.close(); } } /**
/** * Write a summary of the contents of the record * * @param is * @throws IOException */ public void writeSummary(IRandomAccess is, String indentation) throws IOException { is.seek(offset); LOGGER.info(indentation + toString()); for (int i=0; (i<length) && (i < 256); i+= 32) { StringBuilder msg = new StringBuilder(indentation + String.format("%02x:", i)); for (int j=i;(j < length) &&(j < i+32); j++) { msg.append(String.format(" %02x", is.readByte())); } LOGGER.info(msg.toString()); } } /* (non-Javadoc)
/** Sets the endianness of the stream. */ public void order(boolean little) { if (raf != null) { raf.setOrder(little ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN); } }
/** * @return the length of the file * @throws IOException if the length cannot be retrieved */ public long length() throws IOException { return outputFile.length(); }
/** * @return the current (absolute) file pointer. * @throws IOException if the current pointer cannot be retrieved */ public long getFilePointer() throws IOException { return raf.getFilePointer(); }
/** * Read n bytes from the stream into the given buffer at the specified offset. */ public int read(ByteBuffer buf, int offset, int n) throws IOException { return raf.read(buf, offset, n); }