Menu

class: FileStream

[267:14] (extern: com.lehman.aussomserver.connectors.AussomFileStream) extends: object

FileStream is an Aussom Server object which provides support for reading and writing files in a streaming manner. Use a FileStream when a file is too large to keep fully in memory or when the work can be processed one chunk at a time. The same Aussom Server file security model that applies to the static 'file' functions also applies to FileStream. The file name passed to open() must reside in the 'app_data' directory for read or write access, or in the 'public' directory for read-only access (using a "public:" prefix). Attempts to write to a public resource are rejected. The file resource is re-validated through the Aussom Server security model on every call that performs file I/O. A FileStream is opened in one of three modes: "r" for reading, "w" for writing (truncates the file), or "a" for appending. The same stream cannot be used for both reading and writing at the same time. Both text and binary chunks are supported on any open stream, and chunk size is set per call. The typical usage pattern is: fs = new FileStream(); fs.open("app_data/big.txt", "r"); while (!fs.eof()) { chunk = fs.read(8192); // process chunk ... } fs.close();

Methods

  • open (string Name, string Mode = "r")

    Opens the provided file for reading or writing in the given mode. Any previously opened stream on this object is closed first. The file name is validated against the Aussom Server file security model.

    • @p Name is a string with the file name to open.
    • @p Mode is a string with the mode: "r" to read, "w" to write (truncates), or "a" to append. (default "r")
    • @r This FileStream object.
  • read (int MaxBytes = 4096)

    Reads up to MaxBytes bytes from the stream and returns them as a UTF-8 string. Returns an empty string when the end of file has been reached. Use eof() to detect end of file before calling read.

    • @p MaxBytes is an int with the maximum number of bytes to read in this call. (default 4096)
    • @r A string with the chunk that was read.
  • readBinary (int MaxBytes = 4096)

    Reads up to MaxBytes bytes from the stream and returns them in a new Buffer. The returned Buffer's size is the number of bytes actually read, which may be less than MaxBytes near the end of the file. The Buffer's size will be 0 when the end of file has been reached.

    • @p MaxBytes is an int with the maximum number of bytes to read in this call. (default 4096)
    • @r A Buffer with the chunk that was read.
  • _readBinary (int MaxBytes, object Buff)

    Wrapped function for passing in a new Buffer object to read into.

    • @p MaxBytes is an int with the maximum number of bytes to read.
    • @p Buff is a Buffer object to read data into.
    • @r The provided Buffer object with the bytes that were read.
  • write (string Data)

    Writes the provided string to the stream as UTF-8 bytes.

    • @p Data is a string with the text to write.
    • @r This FileStream object.
  • writeBinary (object Data)

    Writes the contents of the provided Buffer to the stream. The full byte array of the Buffer is written.

    • @p Data is a Buffer object with the bytes to write.
    • @r This FileStream object.
  • eof ()

    Checks whether the read stream has reached the end of the file. Only valid on a stream opened in mode "r".

    • @r A bool with true if the end of file has been reached.
  • flush ()

    Flushes any buffered writes to the underlying file. Only valid on a stream opened in mode "w" or "a".

    • @r This FileStream object.
  • close ()

    Closes the stream and releases the underlying file handle. Safe to call more than once.

    • @r This FileStream object.
  • isOpen ()

    Checks whether the stream currently has an underlying file handle open.

    • @r A bool with true if the stream is open.
  • getName ()

    Gets the requested name of the file the stream is operating on. This is the name as supplied to open(), not the resolved full filesystem path.

    • @r A string with the file name, or an empty string if the stream has not been opened.
  • getMode ()

    Gets the mode the stream was opened in.

    • @r A string with one of "r", "w", "a", or an empty string if the stream has not been opened.

class: file

[39:21] static (extern: com.lehman.aussomserver.connectors.AussomFile) extends: object

Aussom file is a static class with a bunch of functions for manipulating files. It has some basic read/write files, along with list, rm, rename, delete, and many others. It's important to understand the Aussom Server security model with respect to file operations. There are only two directories that you can use for file read operations, 'public', and 'app_data'. The 'app_data' is the main directory for reading and writing data. This is the place where your app can peform any read/writes and is the preferred directory to use. One note, the 'app_data' directory can't contain any .aus code files, Aussom Server won't include any code files from that path for security reasons. The second directory is the 'public' directory. This may be a different directory name if you have set it to something else in the config. This is the directory that public resources are served. You can perform read operations in that directory, but no writes. Also, when reading from the directory you have to prefix a "public:" to the path string to let Aussom Server know you are reading from the public directory and not the default 'app_data' one.

Methods

  • write (string name, string data, bool append = false)

    Writes a text file with to the provided file name with the provided data.

    • @p name is a string with the file name to write.
    • @p data is a string with the text data to write.
    • @p append is an optional flag with true to append and false to not.
    • @r A boolean with true if successful.
  • read (string name)

    Reads a text file with the provided file name.

    • @p name is a string with the file name to read.
    • @r A string with the contents of the file.
  • writeBinary (string name, object data)

    Writes a binary file with the provided file name and Buffer object.

    • @p name is a string with the file name to write.
    • @p data is a Buffer object with the data to write.
    • @r A boolean with true if successful.
  • readBinary (string name)

    Reads a binary file with the provided file name.

    • @p name is a string with the file name to read.
    • @r A Buffer object with the data from the file.
  • _readBinary (string name, object buff)

    Wrapped function for passing in a new Buffer object to read into.

    • @p name is a string with the file name to read.
    • @param buff is a Buffer object to read data into.
    • @r A Buffer object with the results.
  • ls (string dir)

    Produces a list of files and directories in the provided directory name string. This function returns a list of map objects with many details for each file or directory.

    • @p dir is a string with the directory to list.
    • @r A list of map objects with the directory contents.
  • rm (string name)

    Removes the file or directory with the name provided.

    • @p name is a string with the file or directory to remove.
    • @r A boolean with true for success.
  • exists (string name)

    Checks to see if the provided file or directory exists.

    • @p name is a string with the file or directory to check.
    • @r A boolean with true for exists and false for not.
  • isFile (string name)

    Checks to see if the provided name is a file.

    • @p name is a string to check if it's a file.
    • @r A boolean with true if it's a file.
  • isDir (string name)

    Checks to see if a provided name is a directory.

    • @p name is a string to check if it's a directory.
    • @r A boolean with true if it's a directory.
  • canExecute (string name)

    Checks to see if the provided name can be executed.

    • @p name is a string to check if it can be executed.
    • @r A boolean with true if it can be executed.
  • canRead (string name)

    Checks to see if the provided name can be read.

    • @p name is a string to check if it can be read.
    • @r A boolean with true if it can be read.
  • canWrite (string name)

    Checks to see if the provided name can be written.

    • @p name is a string to check if it can be written to.
    • @r A boolean with true if it can be written to.
  • getAbsPath (string name)

    Gets the absolute path with the provided file name.

    • @p name is a string with a file name.
    • @r A string with the absolute path.
  • getCanonicalPath (string name)

    Gets the canonical path with the provided file name.

    • @p name is a string with a file name.
    • @r A string with canonical path.
  • getName (string name)

    Gets the name of the file or path with the provide file or path name.

    • @p name is a string with a file or path name.
    • @r A string with the file or path name.
  • getParent (string name)

    Gets the parent directory of the provided file or directory.

    • @p name is a string with a file or directory.
    • @r A string with the parent directory.
  • isAbsolute (string name)

    Checks if the provided path name is absolute.

    • @p name is a string with the path to check.
    • @r A boolean with true if it's absolute.
  • isHidden (string name)

    Checks to see if the provided path is hidden.

    • @p name is a string with the path to check.
    • @r A boolean with true if the path is hidden.
  • lastModified (string name)

    Gets the last modified date for the provided path.

    • @p name is a string with the path.
    • @r An int with the seconds since epoch of the last modified date.
  • length (string name)

    Gets the length of the path in bytes.

    • @p name is a string of the file name.
    • @r An int with the number of bytes.
  • mkdir (string name)

    Makes a directory with the provided name.

    • @p name is a string with the directory name to create.
    • @r A boolean with true.
  • mkdirs (string name)

    Makes nested directories with the provided path.

    • @p name is a string with the path of directories to create.
    • @r A boolean with true.
  • rename (string name, string newName)

    Attempts to rename the provided file to a new file name.

    • @p name is a string with the source file name.
    • @p newName is a string with the destination file name.
    • @r A boolean with true.
  • setExecutable (string name)

    Sets the provided file executable.

    • @p name is a string with the file name to set.
    • @r A boolean with true.
  • setReadable (string name)

    Sets the provided file as readable.

    • @p name is a string with the file name to set.
    • @r A boolean with true.
  • setWritable (string name)

    Sets the provided file to writable.

    • @p name is a string with the file name to set.
    • @r A boolean with true.