Class FileIo

java.lang.Object
com.codename1.backend.FileIo

public final class FileIo extends Object

The file-system calls a static handler needs, isolated so that everything else in StaticFiles -- ranges, conditional requests, MIME types, the containment check -- is pure Java and shared by every target. That logic is the part worth getting right once.

There is one of these per target: the translated one goes to open/fstat/sendfile directly, the Java SE one to the JDK's channels.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    openBeneath cannot answer here; fall back to open plus a resolved-path check.
    static final int
    openRead: the file is there and could not be opened.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    close(int fd)
     
    static int
    A new spool file in dir (null for the system temporary directory), already unlinked, so closing the descriptor is all the cleanup it needs.
    static boolean
    True when sendFile(int, int, long, long) is a kernel copy rather than a read/write loop.
    static int
    openBeneath(String root, String relative)
    A descriptor for relative under root, or -1.
    static int
     
    static int
    pread(int fd, byte[] buffer, int offset, int length, long position)
    Reads at position without moving the file's own position.
    static int
    read(int fd, byte[] buffer, int offset, int length)
     
    static String
    The canonical path, symlinks followed.
    static long
    sendFile(int socketFd, int fileFd, long offset, long count)
    Sends bytes from a file straight to a socket, without them entering this process where the platform allows it.
    static int
    stat(int fd, long[] out)
    Fills out[0]=size, out[1]=modified-time-millis, out[2]=1 for a directory.
    static int
    statFresh(int fd, long[] out)
    The same call, because statImpl is a live fstat on the open descriptor and so is already current.
    static boolean
    write(int fd, byte[] buffer, int offset, int length)
    Writes the whole slice at the file's position; false on any failure.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • BENEATH_UNSUPPORTED

      public static final int BENEATH_UNSUPPORTED
      openBeneath cannot answer here; fall back to open plus a resolved-path check.
      See Also:
    • OPEN_FAILED

      public static final int OPEN_FAILED
      openRead: the file is there and could not be opened. See openReadImpl.
      See Also:
  • Method Details

    • openBeneath

      public static int openBeneath(String root, String relative)

      A descriptor for relative under root, or -1. Never a file outside root, and never by checking afterwards: the kernel refuses the escape while it resolves, so there is no window between the open and the check for a symlink to move through.

      Returns BENEATH_UNSUPPORTED where the platform has no such call, so the caller can fall back rather than treat it as a missing file.

    • openRead

      public static int openRead(String path)
    • stat

      public static int stat(int fd, long[] out)
      Fills out[0]=size, out[1]=modified-time-millis, out[2]=1 for a directory. Taken from the OPEN DESCRIPTOR rather than the path: stat-then-open lets the file change in between, which is how a length header ends up disagreeing with the body.
    • statFresh

      public static int statFresh(int fd, long[] out)

      The same call, because statImpl is a live fstat on the open descriptor and so is already current.

      It exists so the two runtimes present one API: the Java SE implementation answers stat() from an open-time snapshot that asset serving needs, and therefore cannot answer whether the file has changed since. See the javadoc there.

    • sendFile

      public static long sendFile(int socketFd, int fileFd, long offset, long count)
      Sends bytes from a file straight to a socket, without them entering this process where the platform allows it. Returns how many moved, which may be fewer than asked; the caller loops.
    • hasSendFile

      public static boolean hasSendFile()
      True when sendFile(int, int, long, long) is a kernel copy rather than a read/write loop.
    • read

      public static int read(int fd, byte[] buffer, int offset, int length)
    • realPath

      public static String realPath(String path)
      The canonical path, symlinks followed. The static handler proves a resolved file is inside the document root with this: a check on the request string alone is defeated by an encoded traversal or by a symlink out of the tree.
    • close

      public static void close(int fd)
    • createTemp

      public static int createTemp(String dir)
      A new spool file in dir (null for the system temporary directory), already unlinked, so closing the descriptor is all the cleanup it needs. The descriptor, or -1.
    • write

      public static boolean write(int fd, byte[] buffer, int offset, int length)
      Writes the whole slice at the file's position; false on any failure.
    • pread

      public static int pread(int fd, byte[] buffer, int offset, int length, long position)
      Reads at position without moving the file's own position. The count, 0 at the end, or -1.