/*
 * Copyright Matt Palmer 2009-2011, All rights reserved.
 *
 */


package net.domesdaybook.searcher;

import java.io.IOException;
import net.domesdaybook.reader.Reader;

/**
 *
 * @author matt
 */
public abstract class AbstractSearcher implements Searcher {

    /**
     * @inheritDoc
     */
    @Override
    public long searchForwards(final Reader reader, final long fromPosition) 
            throws IOException {
        return searchForwards(reader, fromPosition, reader.length() - 1);
    }

    
    /**
     * @inheritDoc
     */
    @Override
    public long searchForwards(final Reader reader) 
            throws IOException {
        return searchForwards(reader, 0, reader.length() - 1);
    }

    
    /**
     * @inheritDoc
     */
    @Override
    public int searchForwards(final byte[] bytes, final int fromPosition) {
        return searchForwards(bytes, fromPosition, bytes.length - 1);
    }

    
    /**
     * @inheritDoc
     */
    @Override
    public int searchForwards(final byte[] bytes) {
        return searchForwards(bytes, 0, bytes.length - 1);
    }


    /**
     * @inheritDoc
     */
    @Override
    public long searchBackwards(final Reader reader, final long fromPosition) 
            throws IOException {
        return searchBackwards(reader, fromPosition, 0);
    }

    
    /**
     * @inheritDoc
     */
    @Override
    public long searchBackwards(final Reader reader) 
            throws IOException {
        return searchBackwards(reader, reader.length() - 1, 0);
    }


    /**
     * @inheritDoc
     */
    @Override
    public int searchBackwards(final byte[] bytes, final int fromPosition) {
        return searchBackwards(bytes, fromPosition, 0);
    }

    
    /**
     * @inheritDoc
     */
    @Override
    public int searchBackwards(final byte[] bytes) {
        return searchBackwards(bytes, bytes.length - 1, 0);
    }
    
}

/*
 * Copyright Matt Palmer 2009-2011, All rights reserved.
 *
 */


package net.domesdaybook.searcher;

import java.io.IOException;
import net.domesdaybook.reader.Reader;

/**
 *
 * @author matt
 */
public abstract class AbstractSearcher implements Searcher {

    /**
     * @inheritDoc
     */
    @Override
    public long searchForwards(final Reader reader, final long fromPosition) 
            throws IOException {
        return searchForwards(reader, fromPosition, Long.MAX_VALUE);
    }

    
    /**
     * @inheritDoc
     */
    @Override
    public long searchForwards(final Reader reader) 
            throws IOException {
        return searchForwards(reader, 0, Long.MAX_VALUE);
    }

    
    /**
     * @inheritDoc
     */
    @Override
    public int searchForwards(final byte[] bytes, final int fromPosition) {
        return searchForwards(bytes, fromPosition, bytes.length - 1);
    }

    
    /**
     * @inheritDoc
     */
    @Override
    public int searchForwards(final byte[] bytes) {
        return searchForwards(bytes, 0, bytes.length - 1);
    }


    /**
     * @inheritDoc
     */
    @Override
    public long searchBackwards(final Reader reader, final long fromPosition) 
            throws IOException {
        return searchBackwards(reader, fromPosition, 0);
    }

    
    /**
     * @inheritDoc
     */
    @Override
    public long searchBackwards(final Reader reader) 
            throws IOException {
        return searchBackwards(reader, reader.length() - 1, 0);
    }


    /**
     * @inheritDoc
     */
    @Override
    public int searchBackwards(final byte[] bytes, final int fromPosition) {
        return searchBackwards(bytes, fromPosition, 0);
    }

    
    /**
     * @inheritDoc
     */
    @Override
    public int searchBackwards(final byte[] bytes) {
        return searchBackwards(bytes, bytes.length - 1, 0);
    }
    
}

Use Long.MAX_VALUE for final point of search forwards, avoiding calling reader.length(). This is because calling reader.length() on an InputStreamReader will force the entire stream to be read, in order to determine the length. It does not matter if the final search position is greater than the length of the actual stream, as the Windows read by the stream will simply run out before this point, and the search will terminate naturally.