BinaryFileReadStream.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 Hoot.Runtime.Names.Primitive;
import Hoot.Collections.CharacterString;
import Hoot.Runtime.Functions.Exceptional.Handler;
import Smalltalk.Magnitudes.Ordinal;
import Smalltalk.Collections.ReadableString;
import Smalltalk.Collections.CollectedReadably;
import Smalltalk.Streams.*;

public class BinaryFileReadStream extends FileReadStream<Integer>
{

  public static Metaclass type() { return (Metaclass)Metaclass.$class; }
  @Override public Metaclass $class() { return (Metaclass)Metaclass.$class; }
  public static class Metaclass extends FileReadStream.Metaclass
  {
    static final BinaryFileReadStream.Metaclass $class = new BinaryFileReadStream.Metaclass();
    public Metaclass() {
      this(BinaryFileReadStream.Metaclass.class);
    }

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

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


    /**
     * @return 
     */
    @Override public   BinaryFileReadStream read(final ReadableString fileName)
    {
      java.lang.String exitID = "BinaryFileReadStreamMetatype>>read";
      Frame f0 = new Frame(exitID);
      return f0.evaluate(() -> {
      return (BinaryFileReadStream)f0.exit(exitID, super.read(fileName));
      });
    }

    /**
     * @return 
     */
    public   BinaryFileReadStream on(final RandomAccessFile primitiveFile)
    {
      java.lang.String exitID = "BinaryFileReadStreamMetatype>>on";
      Frame f0 = new Frame(exitID);
      return f0.evaluate(() -> {
      return (BinaryFileReadStream)f0.exit(exitID, new BinaryFileReadStream(primitiveFile));
      });
    }
  }



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

  /**
   * @return 
   */
  public static   BinaryFileReadStream read(final String fileName)
  {
    java.lang.String exitID = "BinaryFileReadStream>>$read";
    Frame f0 = new Frame(exitID);
    return (BinaryFileReadStream)FileReadStream.type().read_type(fileName, Symbol.from("binary"));
  }

  /**
   * @return 
   */
  public   ByteArray contents()
  {
    java.lang.String exitID = "BinaryFileReadStream>>contents";
    Frame f0 = new Frame(exitID);
    Integer pose = this.position();
    this.reset();

    ByteArray result = this.nextBytes(this.length());
    this.position(pose);
    return (ByteArray)result;
  }

  /**
   * @return 
   */
  public   Boolean isText()
  {
    java.lang.String exitID = "BinaryFileReadStream>>isText";
    Frame f0 = new Frame(exitID);
    return (Boolean)False.literal();
  }

  /**
   * @return 
   */
  public   Boolean isBinary()
  {
    java.lang.String exitID = "BinaryFileReadStream>>isBinary";
    Frame f0 = new Frame(exitID);
    return (Boolean)True.literal();
  }

  /**
   * @return 
   */
  public   Symbol externalType()
  {
    java.lang.String exitID = "BinaryFileReadStream>>externalType";
    Frame f0 = new Frame(exitID);
    return (Symbol)Symbol.from("binary");
  }

  /**
   * @return 
   */
  public   Integer next()
  {
    java.lang.String exitID = "BinaryFileReadStream>>next";
    Frame f0 = new Frame(exitID);
    return (Integer)Closure.with(f2 -> {
      return SmallInteger.from(((int)file.readUnsignedByte()));
    }, "").defaultIfCurtailed(SmallInteger.type().zero());
  }

  /**
   * @return 
   */
  public   String nextLine()
  {
    java.lang.String exitID = "BinaryFileReadStream>>nextLine";
    Frame f0 = new Frame(exitID);
    return (String)String.emptyString();
  }

  /**
   * @return 
   */
  public   ByteArray next(final Ordinal count)
  {
    java.lang.String exitID = "BinaryFileReadStream>>next";
    Frame f0 = new Frame(exitID);
    return (ByteArray)this.nextBytes(((Integer)count));
  }

  /**
   * @return 
   */
  public   ByteArray upTo(final Integer target)
  {
    java.lang.String exitID = "BinaryFileReadStream>>upTo";
    Frame f0 = new Frame(exitID);
    return f0.evaluate(() -> {
    String results = String.emptyString();
    Predicate.with(f2 -> {
      return this.atEnd();
    }, "").whileFalse(Closure.with(f2 -> {
      
    Integer v = this.next();
    (target.equals(v)).ifTrue(Closure.with(f3 -> {
      return (ByteArray)f0.exit(exitID, ByteArray.type().withAll(results.primitiveString().getBytes()));
    }, ""));
    results.add(v.asCharacter());
    }, ""));
    return (ByteArray)f0.exit(exitID, ByteArray.type().withAll(results.primitiveString().getBytes()));
    });
  }
}