FileStream.java

/**
* Copyright 2010,2021 Nikolas S Boyd.
Permission is granted to copy this work provided this copyright statement is retained in all copies.

*/
package Hoot.Streams;

import Hoot.Runtime.Functions.*;
import Hoot.Runtime.Faces.*;
import Hoot.Runtime.Values.*;
import Hoot.Runtime.Blocks.*;
import Smalltalk.Core.*;
import Smalltalk.Blocks.*;
import Smalltalk.Magnitudes.*;
import Hoot.Behaviors.*;
import Hoot.Behaviors.Nil;
import Hoot.Behaviors.Object;
import Hoot.Behaviors.True;
import Hoot.Behaviors.False;
import Hoot.Behaviors.Boolean;
import Hoot.Magnitudes.*;
import Hoot.Magnitudes.Integer;
import Hoot.Magnitudes.Character;
import Hoot.Magnitudes.Float;
import Hoot.Magnitudes.Double;
import Hoot.Collections.*;
import Hoot.Collections.String;
import java.io.*;
import Hoot.Magnitudes.Integer;
import Smalltalk.Streams.*;
import Smalltalk.Core.Subject;
import Smalltalk.Magnitudes.Ordinal;

public abstract class FileStream<ElementType extends Object> extends Object implements StreamedSequence<ElementType>
{

  public static Metaclass type() { return (Metaclass)Metaclass.$class; }
  @Override public Metaclass $class() { return (Metaclass)Metaclass.$class; }
  public static class Metaclass extends Object.Metaclass implements StreamedSequence.Metatype
  {
    static final FileStream.Metaclass $class = new FileStream.Metaclass();
    public Metaclass() {
      this(FileStream.Metaclass.class);
    }

    public Metaclass(java.lang.Class aClass) {
      super(aClass);
    }

    @Override public java.lang.Class outerClass() { return FileStream.class; }


    /**
     * @return 
     */
    public   TextFileReadStream read(final String fileName)
    {
      java.lang.String exitID = "FileStreamMetatype>>read";
      Frame f0 = new Frame(exitID);
      return f0.evaluate(() -> {
      return (TextFileReadStream)f0.exit(exitID, FileReadStream.type().read_type(fileName, Symbol.from("text")));
      });
    }

    /**
     * @return 
     */
    public   TextFileWriteStream write(final String fileName)
    {
      java.lang.String exitID = "FileStreamMetatype>>write";
      Frame f0 = new Frame(exitID);
      return f0.evaluate(() -> {
      return (TextFileWriteStream)f0.exit(exitID, this.write_mode(fileName, Symbol.from("create")));
      });
    }

    /**
     * @return 
     */
    public   TextFileWriteStream write_mode(final String fileName, final Symbol mode)
    {
      java.lang.String exitID = "FileStreamMetatype>>write_mode";
      Frame f0 = new Frame(exitID);
      return f0.evaluate(() -> {
      return (TextFileWriteStream)f0.exit(exitID, FileWriteStream.type().write_mode_check_type(fileName, mode, False.literal(), Symbol.from("text")));
      });
    }
  }


  protected RandomAccessFile file;

  /**
   * 
   */
  protected    FileStream(final RandomAccessFile primitiveFile)
  {
    java.lang.String exitID = "FileStream>>FileStream";
    Frame f0 = new Frame(exitID);
     file = primitiveFile;
  }

  /**
   * @return 
   */
  public   FileStream close()
  {
    java.lang.String exitID = "FileStream>>close";
    Frame f0 = new Frame(exitID);
    return f0.evaluate(() -> {
    Closure.with(f2 -> {
      file.close();
    return (FileStream)f0.exit(exitID, null);
    }, "").runQuiet();
    return (FileStream)this;
    });
  }

  /**
   * @return 
   */
  public   Boolean atEnd()
  {
    java.lang.String exitID = "FileStream>>atEnd";
    Frame f0 = new Frame(exitID);
    return (Boolean)Closure.with(f2 -> {
      
    long length = file.length();

    long position = file.getFilePointer();
    return Boolean.from((position == length));
    }, "").defaultIfCurtailed(True.literal());
  }

  /**
   * @return 
   */
  public   Boolean _isEmpty()
  {
    java.lang.String exitID = "FileStream>>_isEmpty";
    Frame f0 = new Frame(exitID);
    return (Boolean)Closure.with(f2 -> {
      
    long length = file.length();
    return Boolean.from((length == 0));
    }, "").defaultIfCurtailed(True.literal());
  }

  /**
   * @return 
   */
  public   Integer length()
  {
    java.lang.String exitID = "FileStream>>length";
    Frame f0 = new Frame(exitID);
    return (Integer)Closure.with(f2 -> {
      return LongInteger.from(file.length());
    }, "").defaultIfCurtailed(SmallInteger.from(0));
  }

  /**
   * @return 
   */
  public   Integer position()
  {
    java.lang.String exitID = "FileStream>>position";
    Frame f0 = new Frame(exitID);
    return (Integer)Closure.with(f2 -> {
      return LongInteger.from(file.getFilePointer());
    }, "").defaultIfCurtailed(SmallInteger.from(0));
  }

  /**
   * @return 
   */
  public   FileStream position(final Ordinal position)
  {
    java.lang.String exitID = "FileStream>>position";
    Frame f0 = new Frame(exitID);
    return f0.evaluate(() -> {
    Closure.with(f2 -> {
      file.seek(((Integer)position).primitiveLong());
    return (FileStream)f0.exit(exitID, null);
    }, "").runLoud();
    return (FileStream)this;
    });
  }

  /**
   * @return 
   */
  public   FileStream setToEnd()
  {
    java.lang.String exitID = "FileStream>>setToEnd";
    Frame f0 = new Frame(exitID);
    return f0.evaluate(() -> {
    Closure.with(f2 -> {
      file.seek(file.length());
    return (FileStream)f0.exit(exitID, null);
    }, "").runLoud();
    return (FileStream)this;
    });
  }

  /**
   * @return 
   */
  public   FileStream reset()
  {
    java.lang.String exitID = "FileStream>>reset";
    Frame f0 = new Frame(exitID);
    this.position(SmallInteger.from(0));
    return (FileStream)this;
  }

  /**
   * @return 
   */
  protected   ByteArray nextBytes(final Integer count)
  {
    java.lang.String exitID = "FileStream>>nextBytes";
    Frame f0 = new Frame(exitID);
    return f0.evaluate(() -> {
    ByteArray result = ByteArray.type().$new(count);
    Closure.with(f2 -> {
      file.readFully(result.primitiveContents());
    return (ByteArray)f0.exit(exitID, null);
    }, "").runLoud();
    return (ByteArray)f0.exit(exitID, result);
    });
  }

  /**
   * @return 
   */
  protected   String nextString(final Integer count)
  {
    java.lang.String exitID = "FileStream>>nextString";
    Frame f0 = new Frame(exitID);
    return (String)String.from(new java.lang.String(this.nextBytes(count).primitiveContents()));
  }
}