Example usage for org.apache.commons.lang.math LongRange getMinimumLong

List of usage examples for org.apache.commons.lang.math LongRange getMinimumLong

Introduction

In this page you can find the example usage for org.apache.commons.lang.math LongRange getMinimumLong.

Prototype

public long getMinimumLong() 

Source Link

Document

Gets the minimum number in this range as a long.

Usage

From source file:SparkKMer.java

public static void main(String[] args) throws Exception {
    //Setup/*from  w  w w  . java2s. c  om*/
    SparkConf sparkConf = new SparkConf().setAppName("SparkKMer");
    JavaSparkContext jsc = new JavaSparkContext(sparkConf);
    //Agrument parsing
    if (args.length < 2) {
        System.err.println("Usage: SparkKMer <accession> <kmer-length>");
        System.exit(1);
    }
    final String acc = args[0];
    final int KMER_LENGTH = Integer.parseInt(args[1]);

    //Check accession and split
    ReadCollection run = gov.nih.nlm.ncbi.ngs.NGS.openReadCollection(acc);
    long numreads = run.getReadCount();

    //Slice the job
    int chunk = 20000; /** amount of reads per 1 map operation **/
    int slices = (int) (numreads / chunk / 1);
    if (slices == 0)
        slices = 1;
    List<LongRange> sub = new ArrayList<LongRange>();
    for (long first = 1; first <= numreads;) {
        long last = first + chunk - 1;
        if (last > numreads)
            last = numreads;
        sub.add(new LongRange(first, last));
        first = last + 1;
    }
    System.err.println("Prepared ranges: \n" + sub);

    JavaRDD<LongRange> jobs = jsc.parallelize(sub, slices);
    //Map
    //
    JavaRDD<String> kmers = jobs.flatMap(new FlatMapFunction<LongRange, String>() {
        ReadCollection run = null;

        @Override
        public Iterable<String> call(LongRange s) {
            //Executes on task nodes
            List<String> ret = new ArrayList<String>();
            try {
                long first = s.getMinimumLong();
                long last = s.getMaximumLong();
                if (run == null) {
                    run = gov.nih.nlm.ncbi.ngs.NGS.openReadCollection(acc);
                }
                ReadIterator it = run.getReadRange(first, last - first + 1, Read.all);
                while (it.nextRead()) {
                    //iterate through fragments
                    while (it.nextFragment()) {
                        String bases = it.getFragmentBases();
                        //iterate through kmers
                        for (int i = 0; i < bases.length() - KMER_LENGTH; i++) {
                            ret.add(bases.substring(i, i + KMER_LENGTH));
                        }
                    }
                }
            } catch (ErrorMsg x) {
                System.err.println(x.toString());
                x.printStackTrace();
            }
            return ret;
        }
    });
    //Initiate kmer counting;
    JavaPairRDD<String, Integer> kmer_ones = kmers.mapToPair(new PairFunction<String, String, Integer>() {
        @Override
        public Tuple2<String, Integer> call(String s) {
            return new Tuple2<String, Integer>(s, 1);
        }
    });
    //Reduce counts
    JavaPairRDD<String, Integer> counts = kmer_ones.reduceByKey(new Function2<Integer, Integer, Integer>() {
        @Override
        public Integer call(Integer i1, Integer i2) {
            return i1 + i2;
        }
    });
    //Collect the output
    List<Tuple2<String, Integer>> output = counts.collect();
    for (Tuple2<String, Integer> tuple : output) {
        System.out.println(tuple._1() + ": " + tuple._2());
    }
    jsc.stop();
}

From source file:com.hipu.bdb.util.FileUtils.java

public static LongRange expandRange(LongRange range1, LongRange range2) {
    return new LongRange(Math.min(range1.getMinimumLong(), range2.getMinimumLong()),
            Math.max(range1.getMaximumLong(), range2.getMaximumLong()));

}

From source file:com.hipu.bdb.util.FileUtils.java

/**
 * Retrieve a number of lines from the file around the given 
 * position, as when paging forward or backward through a file. 
 * /* w  w  w . java  2  s. c o  m*/
 * @param file File to retrieve lines
 * @param position offset to anchor lines
 * @param signedDesiredLineCount lines requested; if negative, 
 *        want this number of lines ending with a line containing
 *        the position; if positive, want this number of lines,
 *        all starting at or after position. 
 * @param lines List<String> to insert found lines
 * @param lineEstimate int estimate of line size, 0 means use default
 *        of 128
 * @return LongRange indicating the file offsets corresponding to 
 *         the beginning of the first line returned, and the point
 *         after the end of the last line returned
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public static LongRange pagedLines(File file, long position, int signedDesiredLineCount, List<String> lines,
        int lineEstimate) throws IOException {
    // consider negative positions as from end of file; -1 = last byte
    if (position < 0) {
        position = file.length() + position;
    }

    // calculate a reasonably sized chunk likely to have all desired lines
    if (lineEstimate == 0) {
        lineEstimate = 128;
    }
    int desiredLineCount = Math.abs(signedDesiredLineCount);
    long startPosition;
    long fileEnd = file.length();
    int bufferSize = (desiredLineCount + 5) * lineEstimate;
    if (signedDesiredLineCount > 0) {
        // reading forward; include previous char in case line-end
        startPosition = position - 1;
    } else {
        // reading backward
        startPosition = position - bufferSize + (2 * lineEstimate);
    }
    if (startPosition < 0) {
        startPosition = 0;
    }
    if (startPosition + bufferSize > fileEnd) {
        bufferSize = (int) (fileEnd - startPosition);
    }

    // read that reasonable chunk
    FileInputStream fis = new FileInputStream(file);
    fis.getChannel().position(startPosition);
    byte[] buf = new byte[bufferSize];
    IOUtils.closeQuietly(fis);

    // find all line starts fully in buffer
    // (positions after a line-end, per line-end definition in 
    // BufferedReader.readLine)
    LinkedList<Integer> lineStarts = new LinkedList<Integer>();
    if (startPosition == 0) {
        lineStarts.add(0);
    }
    boolean atLineEnd = false;
    boolean eatLF = false;
    int i;
    for (i = 0; i < bufferSize; i++) {
        if ((char) buf[i] == '\n' && eatLF) {
            eatLF = false;
            continue;
        }
        if (atLineEnd) {
            atLineEnd = false;
            lineStarts.add(i);
            if (signedDesiredLineCount < 0 && startPosition + i > position) {
                // reached next line past position, read no more
                break;
            }
        }
        if ((char) buf[i] == '\r') {
            atLineEnd = true;
            eatLF = true;
            continue;
        }
        if ((char) buf[i] == '\n') {
            atLineEnd = true;
        }
    }
    if (startPosition + i == fileEnd) {
        // add phantom lineStart after end
        lineStarts.add(bufferSize);
    }
    int foundFullLines = lineStarts.size() - 1;

    // if found no lines
    if (foundFullLines < 1) {
        if (signedDesiredLineCount > 0) {
            if (startPosition + bufferSize == fileEnd) {
                // nothing more to read: return nothing
                return new LongRange(fileEnd, fileEnd);
            } else {
                // retry with larger lineEstimate
                return pagedLines(file, position, signedDesiredLineCount, lines,
                        Math.max(bufferSize, lineEstimate));
            }

        } else {
            // try again with much larger line estimate
            // TODO: fail gracefully before growing to multi-MB buffers
            return pagedLines(file, position, signedDesiredLineCount, lines, bufferSize);
        }
    }

    // trim unneeded lines
    while (signedDesiredLineCount > 0 && startPosition + lineStarts.getFirst() < position) {
        // discard lines starting before desired position
        lineStarts.removeFirst();
    }
    while (lineStarts.size() > desiredLineCount + 1) {
        if (signedDesiredLineCount < 0 && (startPosition + lineStarts.get(1) <= position)) {
            // discard from front until reach line containing target position
            lineStarts.removeFirst();
        } else {
            lineStarts.removeLast();
        }
    }
    int firstLine = lineStarts.getFirst();
    int partialLine = lineStarts.getLast();
    LongRange range = new LongRange(startPosition + firstLine, startPosition + partialLine);
    List<String> foundLines = IOUtils
            .readLines(new ByteArrayInputStream(buf, firstLine, partialLine - firstLine));

    if (foundFullLines < desiredLineCount && signedDesiredLineCount < 0 && startPosition > 0) {
        // if needed and reading backward, read more lines from earlier
        range = expandRange(range, pagedLines(file, range.getMinimumLong() - 1,
                signedDesiredLineCount + foundFullLines, lines, bufferSize / foundFullLines));

    }

    lines.addAll(foundLines);

    if (signedDesiredLineCount < 0 && range.getMaximumLong() < position) {
        // did not get line containining start position
        range = expandRange(range, pagedLines(file, partialLine, 1, lines, bufferSize / foundFullLines));
    }

    if (signedDesiredLineCount > 0 && foundFullLines < desiredLineCount && range.getMaximumLong() < fileEnd) {
        // need more forward lines
        range = expandRange(range, pagedLines(file, range.getMaximumLong(), desiredLineCount - foundFullLines,
                lines, bufferSize / foundFullLines));
    }

    return range;
}

From source file:de.tor.tribes.ui.algo.TimeFrameVisualizer.java

private void updateSize() {
    LongRange startRange = mTimeFrame.getStartRange();
    LongRange arriveRange = mTimeFrame.getArriveRange();
    long minValue = startRange.getMinimumLong();
    long maxValue = arriveRange.getMaximumLong();
    Dimension size = new Dimension(Math.round(
            (maxValue - minValue + 240 * (int) DateUtils.MILLIS_PER_MINUTE) / DateUtils.MILLIS_PER_MINUTE),
            getHeight());//w ww  .  j  a v  a  2  s.  c  o  m
    if (size.getWidth() < mParent.getWidth()) {
        size = mParent.getSize();
    }
    setMinimumSize(size);
    setMaximumSize(size);
    setPreferredSize(size);
    mParent.getViewport().setViewSize(size);
}

From source file:de.tor.tribes.ui.algo.TimeFrameVisualizer.java

private void renderRange(LongRange pRange, LongRange pStartRange, LongRange pArriveRange, boolean pIsStartRange,
        boolean pIsArriveRange, Graphics2D pG2D, TimeSpan pSpanForRange, HashMap<String, Object> pPopupInfo) {
    int rangeStart = 0;
    int rangeWidth = 0;

    if (pRange.overlapsRange(pStartRange)) {
        //start range rendering
        long startDelta = pStartRange.getMinimumLong();
        rangeStart = Math.round((pRange.getMinimumLong() - startDelta) / DateUtils.MILLIS_PER_MINUTE);
        // int rangeEnd = Math.round((pRange.getMaximumLong() - startDelta) / DateUtils.MILLIS_PER_MINUTE);
        rangeWidth = Math//from w ww.  j a va2 s.  c  o  m
                .round((pRange.getMaximumLong() - pRange.getMinimumLong()) / DateUtils.MILLIS_PER_MINUTE);
    } else if (pRange.overlapsRange(pArriveRange)) {
        //end range rendering
        long startDelta = pStartRange.getMinimumLong();
        rangeStart = Math.round((pRange.getMinimumLong() - startDelta) / DateUtils.MILLIS_PER_MINUTE);
        // int rangeEnd = Math.round((pRange.getMaximumLong() - arriveDelta) / DateUtils.MILLIS_PER_MINUTE);
        rangeWidth = Math
                .round((pRange.getMaximumLong() - pRange.getMinimumLong()) / DateUtils.MILLIS_PER_MINUTE);
    }
    //correct small widths
    if (rangeWidth == 0) {
        rangeWidth = 5;
    }

    long max = Math.round(
            (pArriveRange.getMaximumLong() - pStartRange.getMinimumLong()) / DateUtils.MILLIS_PER_MINUTE);

    if (rangeStart > max) {
        return;
    }

    SimpleDateFormat f = new SimpleDateFormat("dd.MM.yy HH:mm:ss");
    String labelString = "";
    if (pSpanForRange != null) {
        labelString = pSpanForRange.toString();
    } else {
        labelString = f.format(new Date(pRange.getMinimumLong())) + " bis "
                + f.format(new Date(pRange.getMaximumLong()));
    }
    Rectangle2D labelBounds = pG2D.getFontMetrics().getStringBounds(labelString, pG2D);
    if (pIsStartRange) {
        pG2D.setColor(Color.RED);
        pG2D.fillRect(rangeStart, 20, rangeWidth, 20);
        pG2D.setColor(Color.BLACK);
        pG2D.drawRect(rangeStart, 20, rangeWidth, 20);
        pG2D.setColor(Color.RED);
        pG2D.setFont(pG2D.getFont().deriveFont(Font.BOLD, 14.0f));
        pG2D.drawString(labelString, rangeStart, (int) labelBounds.getHeight());
    } else if (pIsArriveRange) {
        pG2D.setColor(Color.GREEN.darker());
        pG2D.fillRect(rangeStart, 20, rangeWidth, 20);
        pG2D.setColor(Color.BLACK);
        pG2D.drawRect(rangeStart, 20, rangeWidth, 20);
        pG2D.setColor(Color.GREEN.darker());
        pG2D.setFont(pG2D.getFont().deriveFont(Font.BOLD, 14.0f));
        pG2D.drawString(labelString, rangeStart, (int) labelBounds.getHeight() + 40);
    } else {
        pG2D.fillRect(rangeStart, 20, rangeWidth, 20);
        pG2D.setColor(Color.BLACK);
        pG2D.drawRect(rangeStart, 20, rangeWidth, 20);
        Point loc = getMousePosition();
        if (loc != null && new Rectangle(rangeStart, 20, rangeWidth, 20).contains(loc)) {
            pPopupInfo.put("popup.location", loc);
            pPopupInfo.put("popup.label", labelString);
            pPopupInfo.put("active.range", pRange);
            pPopupInfo.put("span.for.range", pSpanForRange);

        }
    }
}

From source file:de.tor.tribes.ui.algo.TimeFrameVisualizer.java

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    if (mTimeFrame == null) {
        renderNoInfoView(g);/*  w  w w .j a  va  2  s.com*/
    } else {
        updateSize();
        LongRange startRange = mTimeFrame.getStartRange();
        LongRange arriveRange = mTimeFrame.getArriveRange();
        HashMap<LongRange, TimeSpan> startRanges = mTimeFrame
                .startTimespansToRangesMap(AnyTribe.getSingleton());
        HashMap<LongRange, TimeSpan> arriveRanges = mTimeFrame.arriveTimespansToRangesMap(null);
        long minValue = startRange.getMinimumLong();
        long maxValue = arriveRange.getMaximumLong();
        Graphics2D g2d = (Graphics2D) g;
        g2d.setPaint(new TexturePaint(STROKED, new Rectangle(0, 0, 3, 3)));
        g2d.fillRect(0, 0, getWidth(), getHeight());

        //draw frame around the entire range
        renderRange(new LongRange(startRange.getMinimumLong(), arriveRange.getMaximumLong()), startRange,
                arriveRange, false, false, g2d, null, popupInfo);
        g2d.setColor(Constants.DS_BACK);
        popupInfo.clear();
        //fill start range
        renderRange(startRange, startRange, arriveRange, true, false, g2d, null, popupInfo);
        //fill arrive range
        renderRange(arriveRange, startRange, arriveRange, false, true, g2d, null, popupInfo);
        Paint p = g2d.getPaint();
        Iterator<LongRange> rangeKeys = startRanges.keySet().iterator();
        while (rangeKeys.hasNext()) {
            LongRange currentRange = rangeKeys.next();

            TimeSpan spanForRange = startRanges.get(currentRange);
            if (spanForRange != null) {
                if (spanForRange.isValidAtEveryDay()) {
                    g2d.setPaint(new TexturePaint(DAILY_START_FRAME_FILL, new Rectangle(0, 0, 3, 3)));
                } else if (spanForRange.isValidAtExactTime()) {
                    g2d.setPaint(new TexturePaint(EXACT_START_FRAME_FILL, new Rectangle(0, 0, 3, 3)));
                } else {
                    g2d.setPaint(new TexturePaint(ONE_DAY_START_FRAME_FILL, new Rectangle(0, 0, 3, 3)));
                }
            }
            renderRange(currentRange, startRange, arriveRange, false, false, g2d, spanForRange, popupInfo);
        }

        Composite c = g2d.getComposite();
        rangeKeys = arriveRanges.keySet().iterator();
        while (rangeKeys.hasNext()) {
            LongRange currentRange = rangeKeys.next();
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f));
            g2d.setPaint(new TexturePaint(ARRIVE_FRAME_FILL, new Rectangle(0, 0, 3, 3)));
            TimeSpan spanForRange = arriveRanges.get(currentRange);
            renderRange(currentRange, startRange, arriveRange, false, false, g2d, spanForRange, popupInfo);
        }
        g2d.setComposite(c);
        g2d.setPaint(p);
        renderDayMarkers(minValue, maxValue, g2d);
        renderPopup(popupInfo, g2d);
    }
}

From source file:net.sourceforge.subsonic.controller.StreamController.java

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

    TransferStatus status = null;//  w ww. j  a va  2s .c o  m
    PlaylistInputStream in = null;
    Player player = playerService.getPlayer(request, response, false, true);
    User user = securityService.getUserByName(player.getUsername());

    try {

        if (!user.isStreamRole()) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN,
                    "Streaming is forbidden for user " + user.getUsername());
            return null;
        }

        // If "playlist" request parameter is set, this is a Podcast request. In that case, create a separate
        // playlist (in order to support multiple parallel Podcast streams).
        String playlistName = request.getParameter("playlist");
        boolean isPodcast = playlistName != null;
        if (isPodcast) {
            Playlist playlist = new Playlist();
            playlistService.loadPlaylist(playlist, playlistName);
            player.setPlaylist(playlist);
            Util.setContentLength(response, playlist.length());
            LOG.info("Incoming Podcast request for playlist " + playlistName);
        }

        String contentType = StringUtil.getMimeType(request.getParameter("suffix"));
        response.setContentType(contentType);

        String preferredTargetFormat = request.getParameter("format");
        Integer maxBitRate = ServletRequestUtils.getIntParameter(request, "maxBitRate");
        if (Integer.valueOf(0).equals(maxBitRate)) {
            maxBitRate = null;
        }

        VideoTranscodingSettings videoTranscodingSettings = null;

        // Is this a request for a single file (typically from the embedded Flash player)?
        // In that case, create a separate playlist (in order to support multiple parallel streams).
        // Also, enable partial download (HTTP byte range).
        MediaFile file = getSingleFile(request);
        boolean isSingleFile = file != null;
        LongRange range = null;

        if (isSingleFile) {
            Playlist playlist = new Playlist();
            playlist.addFiles(true, file);
            player.setPlaylist(playlist);

            if (!file.isVideo()) {
                response.setIntHeader("ETag", file.getId());
                response.setHeader("Accept-Ranges", "bytes");
            }

            TranscodingService.Parameters parameters = transcodingService.getParameters(file, player,
                    maxBitRate, preferredTargetFormat, videoTranscodingSettings);
            long fileLength = getFileLength(parameters);
            boolean isConversion = parameters.isDownsample() || parameters.isTranscode();
            boolean estimateContentLength = ServletRequestUtils.getBooleanParameter(request,
                    "estimateContentLength", false);

            range = getRange(request, file);
            if (range != null) {
                LOG.info("Got range: " + range);
                response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
                Util.setContentLength(response, fileLength - range.getMinimumLong());
                long firstBytePos = range.getMinimumLong();
                long lastBytePos = fileLength - 1;
                response.setHeader("Content-Range",
                        "bytes " + firstBytePos + "-" + lastBytePos + "/" + fileLength);
            } else if (!isConversion || estimateContentLength) {
                Util.setContentLength(response, fileLength);
            }

            String transcodedSuffix = transcodingService.getSuffix(player, file, preferredTargetFormat);
            response.setContentType(StringUtil.getMimeType(transcodedSuffix));

            if (file.isVideo()) {
                videoTranscodingSettings = createVideoTranscodingSettings(file, request);
            }
        }

        if (request.getMethod().equals("HEAD")) {
            return null;
        }

        // Terminate any other streams to this player.
        if (!isPodcast && !isSingleFile) {
            for (TransferStatus streamStatus : statusService.getStreamStatusesForPlayer(player)) {
                if (streamStatus.isActive()) {
                    streamStatus.terminate();
                }
            }
        }

        status = statusService.createStreamStatus(player);

        in = new PlaylistInputStream(player, status, maxBitRate, preferredTargetFormat,
                videoTranscodingSettings, transcodingService, audioScrobblerService, mediaFileService,
                searchService);
        OutputStream out = RangeOutputStream.wrap(response.getOutputStream(), range);

        // Enabled SHOUTcast, if requested.
        boolean isShoutCastRequested = "1".equals(request.getHeader("icy-metadata"));
        if (isShoutCastRequested && !isSingleFile) {
            response.setHeader("icy-metaint", "" + ShoutCastOutputStream.META_DATA_INTERVAL);
            response.setHeader("icy-notice1", "This stream is served using Subsonic");
            response.setHeader("icy-notice2", "Subsonic - Free media streamer - subsonic.org");
            response.setHeader("icy-name", "Subsonic");
            response.setHeader("icy-genre", "Mixed");
            response.setHeader("icy-url", "http://subsonic.org/");
            out = new ShoutCastOutputStream(out, player.getPlaylist(), settingsService);
        }

        final int BUFFER_SIZE = 2048;
        byte[] buf = new byte[BUFFER_SIZE];

        while (true) {

            // Check if stream has been terminated.
            if (status.terminated()) {
                return null;
            }

            if (player.getPlaylist().getStatus() == Playlist.Status.STOPPED) {
                if (isPodcast || isSingleFile) {
                    break;
                } else {
                    sendDummy(buf, out);
                }
            } else {

                int n = in.read(buf);
                if (n == -1) {
                    if (isPodcast || isSingleFile) {
                        break;
                    } else {
                        sendDummy(buf, out);
                    }
                } else {
                    out.write(buf, 0, n);
                }
            }
        }

    } finally {
        if (status != null) {
            securityService.updateUserByteCounts(user, status.getBytesTransfered(), 0L, 0L);
            statusService.removeStreamStatus(status);
        }
        IOUtils.closeQuietly(in);
    }
    return null;
}

From source file:net.sourceforge.subsonic.util.StringUtilTestCase.java

License:asdf

private void doTestParseRange(long expectedFrom, long expectedTo, String range) {
    LongRange actual = StringUtil.parseRange(range);
    assertEquals("Error in parseRange().", expectedFrom, actual.getMinimumLong());
    assertEquals("Error in parseRange().", expectedTo, actual.getMaximumLong());
}

From source file:org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsRequest.java

/**
 * <p>/*from ww  w. j a v  a2  s. c o  m*/
 * The request from clients to get a report of Applications matching the
 * giving application types in the cluster from the
 * <code>ResourceManager</code>.
 * </p>
 *
 * @see ApplicationClientProtocol#getApplications(GetApplicationsRequest)
 *
 * <p>Setting any of the parameters to null, would just disable that
 * filter</p>
 *
 * @param scope {@link ApplicationsRequestScope} to filter by
 * @param users list of users to filter by
 * @param queues list of scheduler queues to filter by
 * @param applicationTypes types of applications
 * @param applicationTags application tags to filter by
 * @param applicationStates application states to filter by
 * @param startRange range of application start times to filter by
 * @param finishRange range of application finish times to filter by
 * @param limit number of applications to limit to
 * @return {@link GetApplicationsRequest} to be used with
 * {@link ApplicationClientProtocol#getApplications(GetApplicationsRequest)}
 */
@Public
@Stable
public static GetApplicationsRequest newInstance(ApplicationsRequestScope scope, Set<String> users,
        Set<String> queues, Set<String> applicationTypes, Set<String> applicationTags,
        EnumSet<YarnApplicationState> applicationStates, LongRange startRange, LongRange finishRange,
        Long limit) {
    GetApplicationsRequest request = Records.newRecord(GetApplicationsRequest.class);
    if (scope != null) {
        request.setScope(scope);
    }
    request.setUsers(users);
    request.setQueues(queues);
    request.setApplicationTypes(applicationTypes);
    request.setApplicationTags(applicationTags);
    request.setApplicationStates(applicationStates);
    if (startRange != null) {
        request.setStartRange(startRange.getMinimumLong(), startRange.getMaximumLong());
    }
    if (finishRange != null) {
        request.setFinishRange(finishRange.getMinimumLong(), finishRange.getMaximumLong());
    }
    if (limit != null) {
        request.setLimit(limit);
    }
    return request;
}

From source file:org.archive.util.FileUtils.java

/**
 * Retrieve a number of lines from the file around the given 
 * position, as when paging forward or backward through a file. 
 * //from  www .j  a  v  a 2 s  . c o m
 * @param file File to retrieve lines
 * @param position offset to anchor lines
 * @param signedDesiredLineCount lines requested; if negative, 
 *        want this number of lines ending with a line containing
 *        the position; if positive, want this number of lines,
 *        all starting at or after position. 
 * @param lines List<String> to insert found lines
 * @param lineEstimate int estimate of line size, 0 means use default
 *        of 128
 * @return LongRange indicating the file offsets corresponding to 
 *         the beginning of the first line returned, and the point
 *         after the end of the last line returned
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public static LongRange pagedLines(File file, long position, int signedDesiredLineCount, List<String> lines,
        int lineEstimate) throws IOException {
    // consider negative positions as from end of file; -1 = last byte
    if (position < 0) {
        position = file.length() + position;
    }

    // calculate a reasonably sized chunk likely to have all desired lines
    if (lineEstimate == 0) {
        lineEstimate = 128;
    }
    int desiredLineCount = Math.abs(signedDesiredLineCount);
    long startPosition;
    long fileEnd = file.length();
    int bufferSize = (desiredLineCount + 5) * lineEstimate;
    if (signedDesiredLineCount > 0) {
        // reading forward; include previous char in case line-end
        startPosition = position - 1;
    } else {
        // reading backward
        startPosition = position - bufferSize + (2 * lineEstimate);
    }
    if (startPosition < 0) {
        startPosition = 0;
    }
    if (startPosition + bufferSize > fileEnd) {
        bufferSize = (int) (fileEnd - startPosition);
    }

    // read that reasonable chunk
    FileInputStream fis = new FileInputStream(file);
    fis.getChannel().position(startPosition);
    byte[] buf = new byte[bufferSize];
    ArchiveUtils.readFully(fis, buf);
    IOUtils.closeQuietly(fis);

    // find all line starts fully in buffer
    // (positions after a line-end, per line-end definition in 
    // BufferedReader.readLine)
    LinkedList<Integer> lineStarts = new LinkedList<Integer>();
    if (startPosition == 0) {
        lineStarts.add(0);
    }
    boolean atLineEnd = false;
    boolean eatLF = false;
    int i;
    for (i = 0; i < bufferSize; i++) {
        if ((char) buf[i] == '\n' && eatLF) {
            eatLF = false;
            continue;
        }
        if (atLineEnd) {
            atLineEnd = false;
            lineStarts.add(i);
            if (signedDesiredLineCount < 0 && startPosition + i > position) {
                // reached next line past position, read no more
                break;
            }
        }
        if ((char) buf[i] == '\r') {
            atLineEnd = true;
            eatLF = true;
            continue;
        }
        if ((char) buf[i] == '\n') {
            atLineEnd = true;
        }
    }
    if (startPosition + i == fileEnd) {
        // add phantom lineStart after end
        lineStarts.add(bufferSize);
    }
    int foundFullLines = lineStarts.size() - 1;

    // if found no lines
    if (foundFullLines < 1) {
        if (signedDesiredLineCount > 0) {
            if (startPosition + bufferSize == fileEnd) {
                // nothing more to read: return nothing
                return new LongRange(fileEnd, fileEnd);
            } else {
                // retry with larger lineEstimate
                return pagedLines(file, position, signedDesiredLineCount, lines,
                        Math.max(bufferSize, lineEstimate));
            }

        } else {
            // try again with much larger line estimate
            // TODO: fail gracefully before growing to multi-MB buffers
            return pagedLines(file, position, signedDesiredLineCount, lines, bufferSize);
        }
    }

    // trim unneeded lines
    while (signedDesiredLineCount > 0 && startPosition + lineStarts.getFirst() < position) {
        // discard lines starting before desired position
        lineStarts.removeFirst();
    }
    while (lineStarts.size() > desiredLineCount + 1) {
        if (signedDesiredLineCount < 0 && (startPosition + lineStarts.get(1) <= position)) {
            // discard from front until reach line containing target position
            lineStarts.removeFirst();
        } else {
            lineStarts.removeLast();
        }
    }
    int firstLine = lineStarts.getFirst();
    int partialLine = lineStarts.getLast();
    LongRange range = new LongRange(startPosition + firstLine, startPosition + partialLine);
    List<String> foundLines = IOUtils
            .readLines(new ByteArrayInputStream(buf, firstLine, partialLine - firstLine));

    if (foundFullLines < desiredLineCount && signedDesiredLineCount < 0 && startPosition > 0) {
        // if needed and reading backward, read more lines from earlier
        range = expandRange(range, pagedLines(file, range.getMinimumLong() - 1,
                signedDesiredLineCount + foundFullLines, lines, bufferSize / foundFullLines));

    }

    lines.addAll(foundLines);

    if (signedDesiredLineCount < 0 && range.getMaximumLong() < position) {
        // did not get line containining start position
        range = expandRange(range, pagedLines(file, partialLine, 1, lines, bufferSize / foundFullLines));
    }

    if (signedDesiredLineCount > 0 && foundFullLines < desiredLineCount && range.getMaximumLong() < fileEnd) {
        // need more forward lines
        range = expandRange(range, pagedLines(file, range.getMaximumLong(), desiredLineCount - foundFullLines,
                lines, bufferSize / foundFullLines));
    }

    return range;
}