001    /*
002     * Copyright (C) 2009 The Guava Authors
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package com.google.common.io;
018    
019    import com.google.common.annotations.Beta;
020    
021    import java.io.IOException;
022    
023    /**
024     * A callback interface to process bytes from a stream.
025     *
026     * <p>{@link #processBytes} will be called for each line that is read, and
027     * should return {@code false} when you want to stop processing.
028     *
029     * @author Chris Nokleberg
030     * @since 1.0
031     */
032    @Beta
033    public interface ByteProcessor<T> {
034      /**
035       * This method will be called for each chunk of bytes in an
036       * input stream. The implementation should process the bytes
037       * from {@code buf[off]} through {@code buf[off + len - 1]}
038       * (inclusive).
039       *
040       * @param buf the byte array containing the data to process
041       * @param off the initial offset into the array
042       * @param len the length of data to be processed
043       * @return true to continue processing, false to stop
044       */
045      boolean processBytes(byte[] buf, int off, int len) throws IOException;
046    
047      /** Return the result of processing all the bytes. */
048      T getResult();
049    }