/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.examples;

import alluxio.AlluxioURI;
import alluxio.Configuration;
import alluxio.PropertyKey;
import alluxio.client.AlluxioStorageType;
import alluxio.client.ReadType;
import alluxio.client.WriteType;
import alluxio.client.file.FileOutStream;
import alluxio.client.file.FileSystem;
import alluxio.client.file.options.CreateFileOptions;
import alluxio.client.file.options.OpenFileOptions;
import alluxio.exception.AlluxioException;
import alluxio.exception.FileAlreadyExistsException;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.concurrent.Callable;

/**
 * Basic example of using the {@link FileSystem} for writing to and reading from files.
 * <p>
 * This class is different from {@link alluxio.examples.BasicOperations} in the way writes happen.
 * Over there {@link java.nio.ByteBuffer} is used directly, where as here byte data is done via
 * input/output streams.
 * </p>
 * <p>
 * This example also let users play around with how to work with files a bit more. The
 * {@link AlluxioStorageType} is something that can be set, as well as ability to
 * delete file if exists.
 * </p>
 */
public final class BasicNonByteBufferOperations implements Callable<Boolean> {
  private final AlluxioURI mFilePath;
  private final ReadType mReadType;
  private final WriteType mWriteType;
  private final boolean mDeleteIfExists;
  private final int mLength;

  /**
   * @param filePath the path for the files
   * @param readType the {@link ReadType}
   * @param writeType the {@link WriteType}
   * @param deleteIfExists delete files if they already exist
   * @param length the number of files
   */
  public BasicNonByteBufferOperations(AlluxioURI filePath, ReadType readType, WriteType writeType,
      boolean deleteIfExists, int length) {
    mFilePath = filePath;
    mWriteType = writeType;
    mReadType = readType;
    mDeleteIfExists = deleteIfExists;
    mLength = length;
  }

  @Override
  public Boolean call() throws Exception {
    FileSystem alluxioClient = FileSystem.Factory.get();
    write(alluxioClient);
    return read(alluxioClient);
  }

  private void write(FileSystem alluxioClient) throws IOException, AlluxioException {
    FileOutStream fileOutStream = createFile(alluxioClient, mFilePath, mDeleteIfExists);
    try (DataOutputStream os = new DataOutputStream(fileOutStream)) {
      os.writeInt(mLength);
      for (int i = 0; i < mLength; i++) {
        os.writeInt(i);
      }
    }
  }

  private FileOutStream createFile(FileSystem fileSystem, AlluxioURI filePath,
      boolean deleteIfExists) throws IOException, AlluxioException {
    CreateFileOptions options = CreateFileOptions.defaults().setWriteType(mWriteType);
    if (!fileSystem.exists(filePath)) {
      // file doesn't exist yet, so create it
      return fileSystem.createFile(filePath, options);
    } else if (deleteIfExists) {
      // file exists, so delete it and recreate
      fileSystem.delete(filePath);
      return fileSystem.createFile(filePath, options);
    }
    // file exists and deleteIfExists is false
    throw new FileAlreadyExistsException("File exists and deleteIfExists is false");
  }

  private boolean read(FileSystem alluxioClient) throws IOException, AlluxioException {
    OpenFileOptions options = OpenFileOptions.defaults().setReadType(mReadType);

    try (DataInputStream input = new DataInputStream(alluxioClient.openFile(mFilePath, options))) {
      int length = input.readInt();
      for (int i = 0; i < length; i++) {
        if (input.readInt() != i) {
          return false;
        }
      }
    }
    return true;
  }
}

@@ -587,24 +587,50 @@
 figuration;%0A
+import alluxio.Constants;%0A
 import allux
@@ -1034,16 +1034,140 @@
 ception;
+%0Aimport alluxio.util.CommonUtils;%0Aimport alluxio.util.FormatUtils;%0A%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -1912,16 +1912,101 @@
 lean%3E %7B%0A
+  private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);%0A%0A
   privat
@@ -3057,24 +3057,75 @@
 eIfExists);%0A
+    long startTimeMs = CommonUtils.getCurrentMs();%0A
     try (Dat
@@ -3290,16 +3290,108 @@
 %7D%0A    %7D%0A
+    LOG.info(FormatUtils.formatTimeTakenMs(startTimeMs, %22writeFile to file %22 + mFilePath));%0A
   %7D%0A%0A  p
@@ -4246,16 +4246,91 @@
 dType);%0A
+    boolean pass = true;%0A    long startTimeMs = CommonUtils.getCurrentMs();
 %0A    try
@@ -4549,22 +4549,22 @@
     
-return
+pass =
  false;%0A
     
@@ -4559,16 +4559,33 @@
  false;%0A
+          break;%0A
         
@@ -4608,19 +4608,107 @@
     
-return true
+LOG.info(FormatUtils.formatTimeTakenMs(startTimeMs, %22readFile file %22 + mFilePath));%0A    return pass
 ;%0A  

Make output consistent