Package anvil.util

Source Code of anvil.util.StreamUtils

/*
* $Id: StreamUtils.java,v 1.8 2002/09/16 08:05:07 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.util;

import anvil.core.Any;
import anvil.core.AnyBinary;
import anvil.core.Array;
import anvil.core.Serialization;
import anvil.core.UnserializationException;
import anvil.script.Context;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.util.Enumeration;

/**
* NetUtils
*
* @author: Jaripekka Salminen
*/
public class StreamUtils
{

  public static final int TYPE_STRING = 0;
  public static final int TYPE_LINES  = 1;
  public static final int TYPE_BINARY = 2;
  public static final int TYPE_DATA   = 3;

  /**
   *
   */
  public static Any fileGet(Context context, String fileName, int returnType)
      throws IOException, UnserializationException
  {
    File file = new File(fileName);
    context.checkRead(file.getPath());
   
    switch (returnType) {
    case StreamUtils.TYPE_STRING:
      return readerToAnyString(new BufferedReader(new FileReader(file)));

    case StreamUtils.TYPE_LINES:
      return readerToAnyStringArray(new BufferedReader(new FileReader(file)));

    case StreamUtils.TYPE_BINARY:
      return streamToAnyByteArray(new FileInputStream(file));

    case StreamUtils.TYPE_DATA:
      InputStream inputStream = new FileInputStream(file);
      Any data = Serialization.unserialize(context, inputStream);
      inputStream.close();
      return data;

    default:
      return Any.NULL;
    }
  }

  /**
   *
   */
  public static Any readerToAnyString(BufferedReader reader) throws IOException
  {
    StringBuffer buf = new StringBuffer();
    String line;
    while (true) {
      line = reader.readLine();
      if (line == null) {
        break;
      }
      buf.append(line);
      buf.append('\n');
    }
    reader.close();
    return Any.create(buf.toString());
  }

  /**
   *
   */
  public static Any readerToAnyStringArray(BufferedReader reader) throws IOException
  {
    Array array = new Array();
    String line;
    while (true) {
      line = reader.readLine();
      if (line == null) {
        break;
      }
      array.append(Any.create(line));
    }
    reader.close();
    return array;
  }

  /**
   *
   */
  public static Any streamToAnyByteArray(InputStream inputStream) throws IOException
  {
    int available = inputStream.available();
    byte byteBuf[] = new byte[available];
    int nbytes = inputStream.read(byteBuf);
    inputStream.close();
    return new AnyBinary(byteBuf);
  }

  /**
   *
   */
  public static Any linesToWriter(Any lines, BufferedWriter writer) throws IOException {
    Enumeration enum = lines.enumeration();
    while(enum.hasMoreElements()) {
      writer.write(enum.nextElement().toString());
      writer.write('\n');
    }
    writer.close();
    return Any.TRUE;
  }
}
TOP

Related Classes of anvil.util.StreamUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.