All Downloads are FREE. Search and download functionalities are using the official Maven repository.

studio.raptor.sqlparser.fast.util.IOUtils Maven / Gradle / Ivy

/*
 * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
 * and the EPL 1.0 (http://h2database.com/html/license.html).
 * Initial Developer: H2 Group
 */
package studio.raptor.sqlparser.fast.util;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import studio.raptor.sqlparser.fast.engine.Constants;
import studio.raptor.sqlparser.fast.engine.SysProperties;
import studio.raptor.sqlparser.fast.message.ParseException;

/**
 * This utility class contains input/output functions.
 */
public class IOUtils {

  private IOUtils() {
    // utility class
  }

  /**
   * Copy all data from the input stream to the output stream. Both streams
   * are kept open.
   *
   * @param in the input stream
   * @param out the output stream (null if writing is not required)
   * @return the number of bytes copied
   */
  public static long copy(InputStream in, OutputStream out)
      throws IOException {
    return copy(in, out, Long.MAX_VALUE);
  }

  /**
   * Copy all data from the input stream to the output stream. Both streams
   * are kept open.
   *
   * @param in the input stream
   * @param out the output stream (null if writing is not required)
   * @param length the maximum number of bytes to copy
   * @return the number of bytes copied
   */
  public static long copy(InputStream in, OutputStream out, long length)
      throws IOException {
    try {
      long copied = 0;
      int len = (int) Math.min(length, Constants.IO_BUFFER_SIZE);
      byte[] buffer = new byte[len];
      while (length > 0) {
        len = in.read(buffer, 0, len);
        if (len < 0) {
          break;
        }
        if (out != null) {
          out.write(buffer, 0, len);
        }
        copied += len;
        length -= len;
        len = (int) Math.min(length, Constants.IO_BUFFER_SIZE);
      }
      return copied;
    } catch (Exception e) {
      throw ParseException.convertToIOException(e);
    }
  }


  /**
   * Read a number of bytes from an input stream and close the stream.
   *
   * @param in the input stream
   * @param length the maximum number of bytes to read, or -1 to read until the end of file
   * @return the bytes read
   */
  public static byte[] readBytesAndClose(InputStream in, int length)
      throws IOException {
    try {
      if (length <= 0) {
        length = Integer.MAX_VALUE;
      }
      int block = Math.min(Constants.IO_BUFFER_SIZE, length);
      ByteArrayOutputStream out = new ByteArrayOutputStream(block);
      copy(in, out, length);
      return out.toByteArray();
    } catch (Exception e) {
      throw ParseException.convertToIOException(e);
    } finally {
      in.close();
    }
  }

  /**
   * Try to read the given number of bytes to the buffer. This method reads
   * until the maximum number of bytes have been read or until the end of
   * file.
   *
   * @param in the input stream
   * @param buffer the output buffer
   * @param max the number of bytes to read at most
   * @return the number of bytes read, 0 meaning EOF
   */
  public static int readFully(InputStream in, byte[] buffer, int max)
      throws IOException {
    try {
      int result = 0, len = Math.min(max, buffer.length);
      while (len > 0) {
        int l = in.read(buffer, result, len);
        if (l < 0) {
          break;
        }
        result += l;
        len -= l;
      }
      return result;
    } catch (Exception e) {
      throw ParseException.convertToIOException(e);
    }
  }

  /**
   * Try to read the given number of characters to the buffer. This method
   * reads until the maximum number of characters have been read or until the
   * end of file.
   *
   * @param in the reader
   * @param buffer the output buffer
   * @param max the number of characters to read at most
   * @return the number of characters read, 0 meaning EOF
   */
  public static int readFully(Reader in, char[] buffer, int max)
      throws IOException {
    try {
      int result = 0, len = Math.min(max, buffer.length);
      while (len > 0) {
        int l = in.read(buffer, result, len);
        if (l < 0) {
          break;
        }
        result += l;
        len -= l;
      }
      return result;
    } catch (Exception e) {
      throw ParseException.convertToIOException(e);
    }
  }


  /**
   * Create a buffered writer to write to an output stream using the UTF-8
   * format. If the output stream is null, this method returns null.
   *
   * @param out the output stream or null
   * @return the writer
   */
  public static Writer getBufferedWriter(OutputStream out) {
    return out == null ? null : new BufferedWriter(
        new OutputStreamWriter(out, Constants.UTF8));
  }


  /**
   * Trace input or output operations if enabled.
   *
   * @param method the method from where this method was called
   * @param fileName the file name
   * @param o the object to append to the message
   */
  public static void trace(String method, String fileName, Object o) {
    if (SysProperties.TRACE_IO) {
      System.out.println("IOUtils." + method + " " + fileName + " " + o);
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy