001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.fs;
019
020import java.io.IOException;
021import java.nio.ByteBuffer;
022import org.apache.hadoop.classification.InterfaceAudience;
023import org.apache.hadoop.classification.InterfaceStability;
024
025/**
026 * Implementers of this interface provide a read API that writes to a
027 * ByteBuffer, not a byte[].
028 */
029@InterfaceAudience.Public
030@InterfaceStability.Evolving
031public interface ByteBufferReadable {
032  /**
033   * Reads up to buf.remaining() bytes into buf. Callers should use
034   * buf.limit(..) to control the size of the desired read.
035   * <p/>
036   * After a successful call, buf.position() will be advanced by the number 
037   * of bytes read and buf.limit() should be unchanged.
038   * <p/>
039   * In the case of an exception, the values of buf.position() and buf.limit()
040   * are undefined, and callers should be prepared to recover from this
041   * eventuality.
042   * <p/>
043   * Many implementations will throw {@link UnsupportedOperationException}, so
044   * callers that are not confident in support for this method from the
045   * underlying filesystem should be prepared to handle that exception.
046   * <p/>
047   * Implementations should treat 0-length requests as legitimate, and must not
048   * signal an error upon their receipt.
049   *
050   * @param buf
051   *          the ByteBuffer to receive the results of the read operation.
052   * @return the number of bytes read, possibly zero, or -1 if 
053   *         reach end-of-stream
054   * @throws IOException
055   *           if there is some error performing the read
056   */
057  public int read(ByteBuffer buf) throws IOException;
058}