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.Tests;
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.File;
import Hoot.Streams.*;
public class FileStream extends TestableClosure
{
protected static final String FileReport = String.from("%s %s: %s");
protected static final String Sample = String.from("a quick brown fox jumped over a lazy dog");
/**
* @return
*/
@Override public void runTest()
{
java.lang.String exitID = "FileStream>>runTest";
Frame f0 = new Frame(exitID);
File testFolder = new File("target");
Boolean.from(testFolder.exists()).ifFalse(Closure.with(f2 -> {
testFolder.mkdir();
}, ""));
String fileName = String.from("target/stream.test.txt");
this.writeText(fileName);
this.log().cr();
this.log().printLine(FileReport.formatWith(String.from("wrote into"), fileName, Sample.printString()));
this.log().printLine(FileReport.formatWith(String.from("reads from"), fileName, String.from("")));
String fileOut = String.from("target/parsed.text.txt");
this.rewriteText_to(fileName, fileOut);
this.writeBinary(fileName);
String text = this.readBinary(fileName);
this.log().print(String.from("as binary "));
this.log().printLine(text.printString());
this.log().printLine();
}
/**
* @return
*/
public void writeText(final String fileName)
{
java.lang.String exitID = "FileStream>>writeText";
Frame f0 = new Frame(exitID);
TextFileWriteStream oStream = TextFileWriteStream.write(fileName);
{
TextFileWriteStream prime = (TextFileWriteStream)oStream;
prime.nextPutAll(Sample);
prime.cr();
};
oStream.close();
}
/**
* @return
*/
public void rewriteText_to(final String fileName, final String fileOut)
{
java.lang.String exitID = "FileStream>>rewriteText_to";
Frame f0 = new Frame(exitID);
TextFileReadStream iStream = TextFileReadStream.read(fileName);
TextFileWriteStream newStream = TextFileWriteStream.write(fileOut);
Predicate.with(f2 -> {
return iStream.atEnd();
}, "").whileFalse(Closure.with(f2 -> {
String line = iStream.upTo(((Character)Character.from(' ')));
{
TextFileWriteStream prime = (TextFileWriteStream)newStream;
prime.nextPutAll(line);
prime.cr();
};
this.log().printLine(line);
}, ""));
iStream.close();
newStream.close();
}
/**
* @return
*/
public void writeBinary(final String fileName)
{
java.lang.String exitID = "FileStream>>writeBinary";
Frame f0 = new Frame(exitID);
BinaryFileWriteStream oStream = BinaryFileWriteStream.write(fileName);
oStream.nextPutAll(Sample.primitiveString());
oStream.close();
}
/**
* @return
*/
public String readBinary(final String fileName)
{
java.lang.String exitID = "FileStream>>readBinary";
Frame f0 = new Frame(exitID);
BinaryFileReadStream iStream = BinaryFileReadStream.read(fileName);
ByteArray result = iStream.contents();
iStream.close();
return (String)String.from(new java.lang.String(result.primitiveContents()));
}
}