예제 #1
0
  /**
   * primWrite: id from: byteArray startingAt: startIndex count: count "Write into the receiver's
   * file from the given area of storage, starting at the given index, as many as count bytes;
   * return the number of bytes actually written. 2/12/96 sw"
   *
   * <p><primitive: 158>
   *
   * <p>closed ifTrue: [^ self halt: 'Write error: File not open']. rwmode ifFalse: [^ self halt:
   * 'Error-attempt to write to a read-only file.']. self halt: 'File write error'! !
   */
  Object fileWrite(int argCount) {
    if (argCount != 4) throw fHandler.primitiveFailed();

    RandomAccessFile file = lookupFile(3);
    SqueakObject byteArray = fHandler.stackNonInteger(2);
    int startIndex = fHandler.stackInteger(1) - 1; // zero based
    int count = fHandler.stackInteger(0);

    try {
      int written = 0;
      for (int index = startIndex; index < startIndex + count; index++) {
        file.write(byteArray.getByte(index));
        written++;
      }

      return fHandler.pos32BitIntFor(written);
    } catch (IOException e) {
      e.printStackTrace();
    }

    throw fHandler.primitiveFailed();
  }
예제 #2
0
  /**
   * primRead: id into: byteArray startingAt: startIndex count: count "read from the receiver's file
   * into the given area of storage, starting at the given index, as many as count bytes; return the
   * number of bytes actually read. 2/12/96 sw"
   *
   * <p><primitive: 154>
   *
   * <p>self halt: 'error reading file'!
   */
  Object readIntoStartingAtCount(int argCount) {
    if (argCount != 4) throw fHandler.primitiveFailed();

    SqueakObject byteArray = fHandler.stackNonInteger(2);
    int startIndex = fHandler.stackInteger(1) - 1;
    int count = fHandler.stackInteger(0);

    RandomAccessFile file = lookupFile(3);

    byte[] buffer = new byte[count];
    try {
      int read = file.read(buffer, 0, count);

      for (int index = 0; index < read; index++) {
        byteArray.setByte(startIndex + index, buffer[index]); // FIXME: this code cheats!
      }

      return fHandler.pos32BitIntFor(read);
    } catch (IOException e) {
      e.printStackTrace();
    }

    throw fHandler.primitiveFailed();
  }
예제 #3
0
  /**
   * lookupEntryIn: pathName index: index "Look up the index-th entry of the directory with the
   * given path (starting from the root of the file hierarchy) and return an array containing:
   *
   * <p><name> <creationTime> <modificationTime> <dirFlag> <fileSize>
   *
   * <p>The creation and modification times are in seconds since the start of the Smalltalk time
   * epoch. DirFlag is true if the entry is a directory. FileSize the file size in bytes or zero for
   * directories. The primitive returns nil when index is past the end of the directory. It fails if
   * the given pathName is bad."
   *
   * <p><primitive: 162> self primitiveFailed.!
   */
  Object lookupEntryInIndex(int argCount) {
    if (argCount != 2) throw fHandler.primitiveFailed();

    SqueakObject fullPath = fHandler.stackNonInteger(1);
    int index = fHandler.stackInteger(0) - 1;

    if (index < 0) throw fHandler.primitiveFailed();

    String filename = fullPath.asString();
    if (filename.trim().length() == 0) filename = "/";

    File directory = new File(fullPath.asString());
    if (!directory.exists()) throw fHandler.primitiveFailed();

    File[] paths = directory.listFiles();
    if (index < paths.length) return makeDirectoryEntryArray(paths[index]);

    return fHandler.squeakNil();
  }