/*
 * Licensed to the University of California, Berkeley under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package tachyon.worker.block.io;

import java.io.Closeable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;

import tachyon.TachyonURI;

/**
 * Base class for handling block I/O. Block handlers for different under file systems can be
 * implemented by extending this class. It is not thread safe, the caller must guarantee thread
 * safe. This class is internal and subject to changes.
 */
public abstract class BlockHandler implements Closeable {

  /**
   * Create a block handler according to path scheme
   *
   * @param path the block path
   * @return the handler of the block
   * @throws IOException
   * @throws IllegalArgumentException
   */
  public static BlockHandler get(String path) throws IOException, IllegalArgumentException {
    if (path.startsWith(TachyonURI.SEPARATOR) || path.startsWith("file://")) {
      return new BlockHandlerLocal(path);
    }
    throw new IllegalArgumentException("Unsupported block file path: " + path);
  }

  /**
   * Append data to the block from a byte array
   *
   * @param blockOffset starting position of the block file
   * @param buf the data buffer
   * @param offset the offset of the buffer
   * @param length the length of the data
   * @return the size of data that was written
   * @throws IOException
   */
  public int append(long blockOffset, byte[] buf, int offset, int length) throws IOException {
    return append(blockOffset, ByteBuffer.wrap(buf, offset, length));
  }

  /**
   * Appends data to the block from a ByteBuffer
   *
   * @param blockOffset starting position of the block file
   * @param srcBuf ByteBuffer that data is stored in
   * @return the size of data that was written
   * @throws IOException
   */
  public abstract int append(long blockOffset, ByteBuffer srcBuf) throws IOException;

  /**
   * Deletes the block
   *
   * @return true if success, otherwise false
   * @throws IOException
   */
  public abstract boolean delete() throws IOException;

  /**
   * Gets channel used to access block
   *
   * @return the channel bounded with the block file
   */
  public abstract ByteChannel getChannel();

  /**
   * Gets the length of the block
   *
   * @return the length of the block
   * @throws IOException
   */
  public abstract long getLength() throws IOException;

  /**
   * Reads data from block
   *
   * @param offset the offset from starting of the block file
   * @param length the length of data to read, -1 represents reading the rest of the block
   * @return ByteBuffer the data that was read
   * @throws IOException
   */
  public abstract ByteBuffer read(long offset, int length) throws IOException;
}

@@ -824,34 +824,8 @@
 o;%0A%0A
-import java.io.Closeable;%0A
 impo
@@ -901,16 +901,24 @@
 hannels.
+Readable
 ByteChan
@@ -927,854 +927,179 @@
 l;%0A%0A
-import tachyon.TachyonURI;%0A%0A/**%0A * Base class for handling block I/O. Block handlers for different under file systems can be%0A * implemented by extending this class. It is not thread safe, the caller must guarantee thread%0A * safe. This class is internal and subject to chan
+/**%0A * A reader interface to access the data of a block stored in managed stora
 ge
-s
 .%0A *
-/%0Apublic abstract class BlockHandler implements Closeable %7B%0A%0A  /**%0A   * Create a block handler according to path scheme%0A   *%0A   * @param path the block path%0A   * @return the handler of the block%0A   * @throws IOException%0A   * @throws IllegalArgumentException%0A  
+ %3Cp%3E%0A * This class does not provide thread-safety.%0A
  */%0A
-  
 public 
-static BlockHandler get(String path) throws IOException, IllegalArgumentException %7B%0A    if (path.startsWith(TachyonURI.SEPARATOR) %7C%7C path.startsWith(%22file://%22)) %7B%0A      return new BlockHandlerLocal(path);%0A    %7D%0A    throw new IllegalArgumentException(%22Unsupported block file path: %22 + path);%0A  %7D
+interface BlockHandler %7B
 %0A%0A  
@@ -1111,22 +1111,23 @@
   * 
-Append
+Reads
  data 
-to
+from
  the
@@ -1136,940 +1136,243 @@
 lock
- from a byte array%0A   *%0A   * @param blockOffset starting position of the block file%0A   * @param buf the data buffer%0A   * @param offset the offset of the buffer%0A   * @param length the length of the data%0A   * @return the size of data that was written%0A   * @throws IOException%0A   */%0A  public int append(long blockOffset, byte%5B%5D buf, int offset, int length) throws IOException %7B
+.%0A   *%0A   * @param offset the offset from starting of the block file%0A   * @param length the length of data to read
 %0A   
- 
+* @
 return 
-append(blockOffset, ByteBuffer.wrap(buf, offset, length));%0A  %7D%0A%0A  /**%0A   * Appends data to the block from a ByteBuffer%0A   *%0A   * @param blockOffset starting position of the block file%0A   * @param srcBuf ByteBuffer that data is stored in%0A   * @return the size of data that was written%0A   * @throws IOException%0A   */%0A  public abstract int append(long blockOffset, ByteBuffer srcBuf) throws IOException;%0A%0A  /**%0A   * Deletes the block%0A   *%0A   * @return true if success, otherwise false%0A   * @throws IOException%0A   */%0A  public abstract boolean delete(
+ByteBuffer the data that was read%0A   * @throws IOException%0A   */%0A  ByteBuffer read(long offset, long length
 ) th
@@ -1405,177 +1405,39 @@
   * 
-Gets channel used to access block%0A   *%0A   * @return the channel bounded with the block file%0A   */%0A  public abstract ByteChannel getChannel();%0A%0A  /**%0A   * Gets the length
+Returns a readable byte channel
  of 
@@ -1441,24 +1441,25 @@
 of the block
+.
 %0A   *%0A   * @
@@ -1469,465 +1469,55 @@
 urn 
-the length of the block%0A   * @throws IOException%0A   */%0A  public abstract long getLength() throws IOException;%0A%0A  /**%0A   * Reads data from block%0A   *%0A   * @param offset the offset from starting of the block file%0A   * @param length the length of data to read, -1 represents reading the rest of the block%0A   * @return ByteBuffer the data that was read%0A   * @throws IOException%0A   */%0A  public abstract ByteBuffer read(long offset, int length) throws IOException
+channel%0A   */%0A  ReadableByteChannel getChannel()
 ;%0A%7D
-%0A

Modify BlockHandler