Skip to content

Instantly share code, notes, and snippets.

@brweber2
Created December 24, 2011 04:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brweber2/1516269 to your computer and use it in GitHub Desktop.
Save brweber2/1516269 to your computer and use it in GitHub Desktop.
file stream java
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java -Didea.launcher.port=7532 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA 11 CE.app/bin" -Dfile.encoding=UTF-8 -classpath "/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/deploy.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/dt.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/javaws.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/jce.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/jconsole.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/management-agent.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/plugin.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/sa-jdi.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/../Classes/alt-rt.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/../Classes/alt-string.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/../Classes/charsets.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/../Classes/classes.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/../Classes/jsse.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/../Classes/ui.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/apple_provider.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/dnsns.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/localedata.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/sunjce_provider.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/sunpkcs11.jar:/Users/brweber2/code/redline-smalltalk/target/classes:/Users/brweber2/.m2/repository/org/antlr/antlr/3.4/antlr-3.4.jar:/Users/brweber2/.m2/repository/org/antlr/antlr-runtime/3.4/antlr-runtime-3.4.jar:/Users/brweber2/.m2/repository/org/antlr/stringtemplate/3.2.1/stringtemplate-3.2.1.jar:/Users/brweber2/.m2/repository/antlr/antlr/2.7.7/antlr-2.7.7.jar:/Users/brweber2/.m2/repository/org/antlr/ST4/4.0.4/ST4-4.0.4.jar:/Users/brweber2/.m2/repository/asm/asm/3.2/asm-3.2.jar:/Users/brweber2/.m2/repository/asm/asm-util/3.2/asm-util-3.2.jar:/Users/brweber2/.m2/repository/asm/asm-tree/3.2/asm-tree-3.2.jar:/Users/brweber2/.m2/repository/commons-cli/commons-cli/1.2/commons-cli-1.2.jar:/Users/brweber2/.m2/repository/org/mortbay/jetty/jetty/6.1.25/jetty-6.1.25.jar:/Users/brweber2/.m2/repository/org/mortbay/jetty/jetty-util/6.1.25/jetty-util-6.1.25.jar:/Applications/IntelliJ IDEA 11 CE.app/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain sandbox.AsmDump
package asm.sandbox;
import java.util.*;
import org.objectweb.asm.*;
import org.objectweb.asm.attrs.*;
public class FileStreamDump implements Opcodes {
public static byte[] dump () throws Exception {
ClassWriter cw = new ClassWriter(0);
FieldVisitor fv;
MethodVisitor mv;
AnnotationVisitor av0;
cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, "sandbox/FileStream", null, "java/lang/Object", null);
{
fv = cw.visitField(ACC_PRIVATE + ACC_FINAL, "file", "Ljava/io/RandomAccessFile;", null, null);
fv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(Ljava/lang/String;Ljava/lang/String;)V", null, new String[] { "java/io/FileNotFoundException" });
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
mv.visitVarInsn(ALOAD, 0);
mv.visitTypeInsn(NEW, "java/io/RandomAccessFile");
mv.visitInsn(DUP);
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ALOAD, 2);
mv.visitMethodInsn(INVOKESPECIAL, "java/io/RandomAccessFile", "<init>", "(Ljava/lang/String;Ljava/lang/String;)V");
mv.visitFieldInsn(PUTFIELD, "sandbox/FileStream", "file", "Ljava/io/RandomAccessFile;");
mv.visitInsn(RETURN);
mv.visitMaxs(5, 3);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PROTECTED, "finalize", "()V", null, new String[] { "java/lang/Throwable" });
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "finalize", "()V");
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "sandbox/FileStream", "file", "Ljava/io/RandomAccessFile;");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/RandomAccessFile", "close", "()V");
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PRIVATE, "bufferStart", "()J", null, new String[] { "java/io/IOException" });
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "sandbox/FileStream", "file", "Ljava/io/RandomAccessFile;");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/RandomAccessFile", "getFilePointer", "()J");
mv.visitInsn(LRETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "copyFrom", "(II)[B", null, new String[] { "java/io/IOException" });
mv.visitCode();
mv.visitVarInsn(ILOAD, 2);
mv.visitVarInsn(ILOAD, 1);
mv.visitInsn(ISUB);
mv.visitVarInsn(ISTORE, 3);
mv.visitVarInsn(ILOAD, 3);
mv.visitIntInsn(NEWARRAY, T_BYTE);
mv.visitVarInsn(ASTORE, 4);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "sandbox/FileStream", "file", "Ljava/io/RandomAccessFile;");
mv.visitVarInsn(ALOAD, 4);
mv.visitVarInsn(ILOAD, 1);
mv.visitVarInsn(ILOAD, 3);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/RandomAccessFile", "readFully", "([BII)V");
mv.visitVarInsn(ALOAD, 4);
mv.visitInsn(ARETURN);
mv.visitMaxs(4, 5);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "next", "()Ljava/lang/Character;", null, new String[] { "java/io/IOException" });
mv.visitCode();
Label l0 = new Label();
Label l1 = new Label();
Label l2 = new Label();
mv.visitTryCatchBlock(l0, l1, l2, "java/io/EOFException");
mv.visitLabel(l0);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "sandbox/FileStream", "file", "Ljava/io/RandomAccessFile;");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/RandomAccessFile", "readChar", "()C");
mv.visitMethodInsn(INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;");
mv.visitLabel(l1);
mv.visitInsn(ARETURN);
mv.visitLabel(l2);
mv.visitVarInsn(ASTORE, 1);
mv.visitInsn(ACONST_NULL);
mv.visitInsn(ARETURN);
mv.visitMaxs(1, 2);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "nextPut", "(C)V", null, new String[] { "java/io/IOException" });
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "sandbox/FileStream", "file", "Ljava/io/RandomAccessFile;");
mv.visitVarInsn(ILOAD, 1);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/RandomAccessFile", "writeChar", "(I)V");
mv.visitInsn(RETURN);
mv.visitMaxs(2, 2);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "peek", "()Ljava/lang/Character;", null, new String[] { "java/io/IOException" });
mv.visitCode();
Label l0 = new Label();
Label l1 = new Label();
Label l2 = new Label();
mv.visitTryCatchBlock(l0, l1, l2, "java/io/EOFException");
Label l3 = new Label();
mv.visitTryCatchBlock(l0, l1, l3, null);
Label l4 = new Label();
mv.visitTryCatchBlock(l2, l4, l3, null);
Label l5 = new Label();
mv.visitTryCatchBlock(l3, l5, l3, null);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "sandbox/FileStream", "file", "Ljava/io/RandomAccessFile;");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/RandomAccessFile", "getFilePointer", "()J");
mv.visitVarInsn(LSTORE, 1);
mv.visitLabel(l0);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "sandbox/FileStream", "file", "Ljava/io/RandomAccessFile;");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/RandomAccessFile", "readChar", "()C");
mv.visitVarInsn(ISTORE, 3);
mv.visitLabel(l1);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "sandbox/FileStream", "file", "Ljava/io/RandomAccessFile;");
mv.visitVarInsn(LLOAD, 1);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/RandomAccessFile", "seek", "(J)V");
Label l6 = new Label();
mv.visitJumpInsn(GOTO, l6);
mv.visitLabel(l2);
mv.visitVarInsn(ASTORE, 4);
mv.visitInsn(ACONST_NULL);
mv.visitVarInsn(ASTORE, 5);
mv.visitLabel(l4);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "sandbox/FileStream", "file", "Ljava/io/RandomAccessFile;");
mv.visitVarInsn(LLOAD, 1);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/RandomAccessFile", "seek", "(J)V");
mv.visitVarInsn(ALOAD, 5);
mv.visitInsn(ARETURN);
mv.visitLabel(l3);
mv.visitVarInsn(ASTORE, 6);
mv.visitLabel(l5);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "sandbox/FileStream", "file", "Ljava/io/RandomAccessFile;");
mv.visitVarInsn(LLOAD, 1);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/RandomAccessFile", "seek", "(J)V");
mv.visitVarInsn(ALOAD, 6);
mv.visitInsn(ATHROW);
mv.visitLabel(l6);
mv.visitVarInsn(ILOAD, 3);
mv.visitMethodInsn(INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;");
mv.visitInsn(ARETURN);
mv.visitMaxs(3, 7);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "position", "()J", null, new String[] { "java/io/IOException" });
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "sandbox/FileStream", "file", "Ljava/io/RandomAccessFile;");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/RandomAccessFile", "getFilePointer", "()J");
mv.visitInsn(LRETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "position", "(J)V", null, new String[] { "java/io/IOException" });
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "sandbox/FileStream", "file", "Ljava/io/RandomAccessFile;");
mv.visitVarInsn(LLOAD, 1);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/RandomAccessFile", "seek", "(J)V");
mv.visitInsn(RETURN);
mv.visitMaxs(3, 3);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "size", "()J", null, new String[] { "java/io/IOException" });
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "sandbox/FileStream", "file", "Ljava/io/RandomAccessFile;");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/RandomAccessFile", "length", "()J");
mv.visitInsn(LRETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "truncate", "()V", null, new String[] { "java/io/IOException" });
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "sandbox/FileStream", "file", "Ljava/io/RandomAccessFile;");
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "sandbox/FileStream", "file", "Ljava/io/RandomAccessFile;");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/RandomAccessFile", "getFilePointer", "()J");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/RandomAccessFile", "setLength", "(J)V");
mv.visitInsn(RETURN);
mv.visitMaxs(3, 1);
mv.visitEnd();
}
cw.visitEnd();
return cw.toByteArray();
}
}
Process finished with exit code 0
package sandbox;
import java.io.*;
/**
* @author brweber2
*/
public class FileStream {
private final RandomAccessFile file;
// todo do any types need to be changed to special redline-st types?
/**
* Mode can be: read, write, readWrite or append from/to
*/
public FileStream(String filename, String mode) throws FileNotFoundException {
file = new RandomAccessFile(filename, mode);
}
@Override
protected void finalize() throws Throwable {
super.finalize();
file.close();
}
/**
* Answer the offset from the start of the file corresponding to the beginning of the read buffer.
*/
private long bufferStart() throws IOException {
return file.getFilePointer();
}
/**
* Answer the contents of the file between the two given positions
*/
public byte[] copyFrom( int from, int to ) throws IOException {
int len = to-from;
byte[] bytes = new byte[len];
file.readFully(bytes, from, len);
return bytes;
}
/**
* Return the next character in the file, or nil at eof
*/
public Character next() throws IOException {
try {
return file.readChar();
} catch (EOFException e) {
return null; // todo should this be smalltalk nil?
}
}
/**
* Store aCharacter on the file
*/
public void nextPut( char c ) throws IOException {
file.writeChar( c );
}
/**
* Return the next character in the file, or nil at eof. Don't advance the file pointer.
*/
public Character peek() throws IOException {
long currentPosition = file.getFilePointer();
char c;
try {
c = file.readChar();
} catch (EOFException e) {
return null; // todo should this be smalltalk nil?
}
finally {
file.seek( currentPosition );
}
return c;
}
/**
* Answer the zero-based position from the start of the file
*/
public long position() throws IOException {
return file.getFilePointer();
}
/**
* Set the file pointer to the zero-based position n
*/
public void position( long n ) throws IOException {
file.seek( n );
}
/**
* Return the current size of the file, in bytes
*/
public long size() throws IOException {
return file.length();
}
/**
* Truncate the file at the current position
*/
public void truncate() throws IOException {
file.setLength( file.getFilePointer() );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment