Mercurial Hosting > luan
diff src/luan/impl/Compiled.java @ 1492:aaac1d29edea
better io
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 02 May 2020 22:25:56 -0600 |
parents | 65d4afc9ad07 |
children | e1a13e707bf3 |
line wrap: on
line diff
--- a/src/luan/impl/Compiled.java Sat May 02 21:09:17 2020 -0600 +++ b/src/luan/impl/Compiled.java Sat May 02 22:25:56 2020 -0600 @@ -1,13 +1,12 @@ package luan.impl; import java.io.OutputStream; +import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.StringWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; import java.io.IOException; import java.net.URI; import java.util.Collections; @@ -23,6 +22,9 @@ import javax.tools.JavaFileManager; import javax.tools.StandardJavaFileManager; import javax.tools.ForwardingJavaFileManager; +import goodjava.io.BufferedInputStream; +import goodjava.io.DataInputStream; +import goodjava.io.DataOutputStream; import goodjava.logging.Logger; import goodjava.logging.LoggerFactory; @@ -125,13 +127,13 @@ } }; return cl.loadClass(className); - } catch(ClassNotFoundException e) { - throw new RuntimeException(e); - } + } catch(ClassNotFoundException e) { + throw new RuntimeException(e); + } } - private static final int VERSION = 2; + private static final int VERSION = 3; private static final File tmpDir; static { File f = new File(System.getProperty("java.io.tmpdir")); @@ -141,33 +143,15 @@ throw new RuntimeException(); } - static void writeLongString(DataOutputStream out,String s) throws IOException { - int n = s.length()/0xFFFF; - out.writeInt(n+1); - for( int i=0; i<n; i++ ) { - out.writeUTF( s.substring(i*0xFFFF,(i+1)*0xFFFF) ); - } - out.writeUTF( s.substring(n*0xFFFF) ); - } - - static String readLongString(DataInputStream in) throws IOException { - StringBuilder sb = new StringBuilder(); - int n = in.readInt(); - for( int i=0; i<n; i++ ) { - sb.append( in.readUTF() ); - } - return sb.toString(); - } - static Compiled load(String fileName,String key) { try { File f = new File(tmpDir,fileName); if( !f.exists() ) return null; - DataInputStream in = new DataInputStream(new FileInputStream(f)); + DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(f))); if( in.readInt() != VERSION ) return null; - if( !readLongString(in).equals(key) ) + if( !in.readString().equals(key) ) return null; String className = in.readUTF(); int n = in.readInt(); @@ -190,16 +174,16 @@ void save(String fileName,String key) { try { File f = new File(tmpDir,fileName); - DataOutputStream out = new DataOutputStream(new FileOutputStream(f)); + DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(f))); out.writeInt(VERSION); - writeLongString(out,key); + out.writeString(key); out.writeUTF(className); out.writeInt(map.size()); for( Map.Entry<String,byte[]> entry : map.entrySet() ) { out.writeUTF( entry.getKey() ); byte[] a = entry.getValue(); out.writeInt(a.length); - out.write(a,0,a.length); + out.write(a); } out.close(); } catch(IOException e) {