Skip to content

Commit

Permalink
Improve File Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
daumayr authored and smarr committed Feb 11, 2018
1 parent 0611fdb commit da8107d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
12 changes: 6 additions & 6 deletions core-lib/Files.ns
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class Files usingVmMirror: vmMirror usingPlatform: platform = Value (
(* This code was derived from Newspeak File classes, which is why the Sun Microsystems copyright and BSD license below applies.
Original Newspeak version of classes:
https://bitbucket.org/newspeaklanguage/newspeak/src/38a47c705f1a1ab3359f2a58b79e8c728bfb218f/Files.ns
https://bitbucket.org/newspeaklanguage/newspeak/src/38a47c705f1a1ab3359f2a58b79e8c728bfb218f/Win32Files.ns
Original Newspeak version of classes:
https://bitbucket.org/newspeaklanguage/newspeak/src/38a47c705f1a1ab3359f2a58b79e8c728bfb218f/Files.ns
https://bitbucket.org/newspeaklanguage/newspeak/src/38a47c705f1a1ab3359f2a58b79e8c728bfb218f/Win32Files.ns

Copyright (c) 1995-2006 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
Copyright 2008-2009 Gilad Bracha and other contributors.
Expand Down Expand Up @@ -92,8 +92,8 @@ DAMAGE.>> *)
^ (vmMirror fileCreateFileDescriptorFor: obj) mode: m.
)

public new = ( self error: 'Please use for:mode: instead of new!\n'
+ '\talternatively the open method of a FilePath object can be used.'
public new = ( self error: 'Please use for:mode: instead of new!\n'
+ '\talternatively the open method of a FilePath object can be used.'
)
)

Expand Down Expand Up @@ -406,7 +406,7 @@ DAMAGE.>> *)
(* FilePaths are FilePatterns without wildcard characters,
which means they match individual files, not sets of files. *)
public class FilePath = FilePattern ()(

(* concatenation *)
public , extension <FilePattern> ^ <FilePattern> = (
^ extension extendPath: self (* ) *)
Expand Down
47 changes: 35 additions & 12 deletions src/som/vmobjects/SFileDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import som.interpreter.nodes.dispatch.BlockDispatchNode;
import som.primitives.PathPrims;
import som.vm.Symbols;
import som.vm.constants.Classes;
import som.vmobjects.SArray.SMutableArray;

Expand All @@ -19,10 +18,11 @@ public class SFileDescriptor extends SObjectWithClass {

public static final int BUFFER_SIZE = 32 * 1024;

private boolean open = false;
private SArray buffer;
private SSymbol mode;
private int bufferSize = BUFFER_SIZE;
private boolean open = false;
private SArray buffer;
private SSymbol mode;
private int bufferSize = BUFFER_SIZE;
private AccessModes access;

private RandomAccessFile raf;
private final File f;
Expand All @@ -41,13 +41,13 @@ public void openFile(final SBlock fail, final BlockDispatchNode dispatchHandler)
buffer = new SMutableArray(storage, Classes.arrayClass);

try {
if (mode == Symbols.symbolFor("read")) {
raf = new RandomAccessFile(f, "r");
} else if (mode == Symbols.symbolFor("readwrite")) {
raf = new RandomAccessFile(f, "rw");
} else {
dispatchHandler.executeDispatch(new Object[] {fail, "invalid access mode"});
}
this.access = AccessModes.valueOf(mode.getString().toUpperCase());
} catch (Exception e) {
dispatchHandler.executeDispatch(new Object[] {fail, "invalid access mode: " + mode});
}

try {
raf = new RandomAccessFile(f, access.getMode());
} catch (FileNotFoundException e) {
dispatchHandler.executeDispatch(new Object[] {fail, e.toString()});
}
Expand All @@ -71,6 +71,11 @@ public int read(final long position, final SBlock fail,
return 0;
}

if (access == AccessModes.WRITE) {
fail.getMethod().invoke(new Object[] {fail, "Opened in write only"});
return 0;
}

long[] storage = (long[]) buffer.getStoragePlain();
byte[] buff = new byte[bufferSize];
int bytes = 0;
Expand Down Expand Up @@ -100,6 +105,11 @@ public void write(final int nBytes, final long position, final SBlock fail,
return;
}

if (access == AccessModes.READ) {
fail.getMethod().invoke(new Object[] {fail, "Opened in read only"});
return;
}

long[] storage = (long[]) buffer.getStoragePlain();
byte[] buff = new byte[bufferSize];

Expand Down Expand Up @@ -157,4 +167,17 @@ public void setBufferSize(final int bufferSize) {
public void setMode(final SSymbol mode) {
this.mode = mode;
}

private enum AccessModes {
READ("r"), WRITE("rw"), READWRITE("rw");
String mode;

AccessModes(final String mode) {
this.mode = mode;
}

String getMode() {
return mode;
}
}
}

0 comments on commit da8107d

Please # to comment.