/** {@inheritDoc} */ @Override public long length() { return is.length(); }
/** * Constructor. * * @param is Base input stream. * @param start Start position. * @param maxLen Maximum stream length. * @throws IOException In case of exception. */ public GridGgfsRangeInputStream(GridGgfsInputStream is, long start, long maxLen) throws IOException { if (is == null) throw new IllegalArgumentException("Input stream cannot be null."); if (start < 0) throw new IllegalArgumentException("Start position cannot be negative."); if (start >= is.length()) throw new IllegalArgumentException("Start position cannot be greater that file length."); if (maxLen < 0) throw new IllegalArgumentException("Length cannot be negative."); if (start + maxLen > is.length()) throw new IllegalArgumentException("Sum of start position and length cannot be greater than file length."); this.is = is; this.start = start; this.maxLen = maxLen; is.seek(start); }
/** {@inheritDoc} */ @Override public GridGgfsFileRange resolveRecords(GridGgfs ggfs, GridGgfsInputStream stream, GridGgfsFileRange suggestedRecord) throws GridException, IOException { long suggestedEnd = suggestedRecord.start() + suggestedRecord.length(); long startRem = suggestedRecord.start() % recLen; long endRem = suggestedEnd % recLen; long start = Math.min(suggestedRecord.start() + (startRem != 0 ? (recLen - startRem) : 0), stream.length()); long end = Math.min(suggestedEnd + (endRem != 0 ? (recLen - endRem) : 0), stream.length()); assert end >= start; return start != end ? new GridGgfsFileRange(suggestedRecord.path(), start, end - start) : null; }