/** * Array with * * <p><name> <creationTime> <modificationTime> <dirFlag> <fileSize> * * <p>See {@link #lookupEntryInIndex(int)} */ private SqueakObject makeDirectoryEntryArray(File file) { // bah, Java doesn't provide the creation time. If it is // really necessary, it may be possible to use java-posix or // jtux. String name = file.getName(); long creationTime = file.lastModified(); long modificationTime = file.lastModified(); boolean dirFlag = file.isDirectory(); long fileSize = file.length(); try { Object[] array = { fHandler.makeStString(name), fHandler.squeakSeconds(creationTime), fHandler.squeakSeconds(modificationTime), fHandler.squeakBool(dirFlag), fHandler.pos32BitIntFor(fileSize) }; return fHandler.squeakArray(array); } catch (ArrayIndexOutOfBoundsException e) { System.out.println( "Error in FileSystemPrimitives.makeDirectoryEntryArray: name = '" + name + "'"); throw e; } }
/** * primGetPosition: id "Get the receiver's current file position. 2/12/96 sw" <primitive: 152> ^ * self primitiveFailed! */ Object getPosition(int argCount) { if (argCount != 1) throw fHandler.primitiveFailed(); RandomAccessFile file = lookupFile(); try { return fHandler.pos32BitIntFor(file.getFilePointer()); } catch (IOException e) { e.printStackTrace(); } throw fHandler.primitiveFailed(); }
/** * 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(); }
/** * 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(); }