Mercurial Hosting > luan
comparison 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 |
comparison
equal
deleted
inserted
replaced
1491:491b355acef7 | 1492:aaac1d29edea |
---|---|
1 package luan.impl; | 1 package luan.impl; |
2 | 2 |
3 import java.io.OutputStream; | 3 import java.io.OutputStream; |
4 import java.io.BufferedOutputStream; | |
4 import java.io.ByteArrayOutputStream; | 5 import java.io.ByteArrayOutputStream; |
5 import java.io.StringWriter; | 6 import java.io.StringWriter; |
6 import java.io.File; | 7 import java.io.File; |
7 import java.io.FileInputStream; | 8 import java.io.FileInputStream; |
8 import java.io.FileOutputStream; | 9 import java.io.FileOutputStream; |
9 import java.io.DataInputStream; | |
10 import java.io.DataOutputStream; | |
11 import java.io.IOException; | 10 import java.io.IOException; |
12 import java.net.URI; | 11 import java.net.URI; |
13 import java.util.Collections; | 12 import java.util.Collections; |
14 import java.util.Map; | 13 import java.util.Map; |
15 import java.util.HashMap; | 14 import java.util.HashMap; |
21 import javax.tools.JavaCompiler; | 20 import javax.tools.JavaCompiler; |
22 import javax.tools.ToolProvider; | 21 import javax.tools.ToolProvider; |
23 import javax.tools.JavaFileManager; | 22 import javax.tools.JavaFileManager; |
24 import javax.tools.StandardJavaFileManager; | 23 import javax.tools.StandardJavaFileManager; |
25 import javax.tools.ForwardingJavaFileManager; | 24 import javax.tools.ForwardingJavaFileManager; |
25 import goodjava.io.BufferedInputStream; | |
26 import goodjava.io.DataInputStream; | |
27 import goodjava.io.DataOutputStream; | |
26 import goodjava.logging.Logger; | 28 import goodjava.logging.Logger; |
27 import goodjava.logging.LoggerFactory; | 29 import goodjava.logging.LoggerFactory; |
28 | 30 |
29 | 31 |
30 final class Compiled { | 32 final class Compiled { |
129 throw new RuntimeException(e); | 131 throw new RuntimeException(e); |
130 } | 132 } |
131 } | 133 } |
132 | 134 |
133 | 135 |
134 private static final int VERSION = 2; | 136 private static final int VERSION = 3; |
135 private static final File tmpDir; | 137 private static final File tmpDir; |
136 static { | 138 static { |
137 File f = new File(System.getProperty("java.io.tmpdir")); | 139 File f = new File(System.getProperty("java.io.tmpdir")); |
138 tmpDir = new File(f,"luan"); | 140 tmpDir = new File(f,"luan"); |
139 tmpDir.mkdir(); | 141 tmpDir.mkdir(); |
140 if( !tmpDir.exists() ) | 142 if( !tmpDir.exists() ) |
141 throw new RuntimeException(); | 143 throw new RuntimeException(); |
142 } | 144 } |
143 | 145 |
144 static void writeLongString(DataOutputStream out,String s) throws IOException { | |
145 int n = s.length()/0xFFFF; | |
146 out.writeInt(n+1); | |
147 for( int i=0; i<n; i++ ) { | |
148 out.writeUTF( s.substring(i*0xFFFF,(i+1)*0xFFFF) ); | |
149 } | |
150 out.writeUTF( s.substring(n*0xFFFF) ); | |
151 } | |
152 | |
153 static String readLongString(DataInputStream in) throws IOException { | |
154 StringBuilder sb = new StringBuilder(); | |
155 int n = in.readInt(); | |
156 for( int i=0; i<n; i++ ) { | |
157 sb.append( in.readUTF() ); | |
158 } | |
159 return sb.toString(); | |
160 } | |
161 | |
162 static Compiled load(String fileName,String key) { | 146 static Compiled load(String fileName,String key) { |
163 try { | 147 try { |
164 File f = new File(tmpDir,fileName); | 148 File f = new File(tmpDir,fileName); |
165 if( !f.exists() ) | 149 if( !f.exists() ) |
166 return null; | 150 return null; |
167 DataInputStream in = new DataInputStream(new FileInputStream(f)); | 151 DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(f))); |
168 if( in.readInt() != VERSION ) | 152 if( in.readInt() != VERSION ) |
169 return null; | 153 return null; |
170 if( !readLongString(in).equals(key) ) | 154 if( !in.readString().equals(key) ) |
171 return null; | 155 return null; |
172 String className = in.readUTF(); | 156 String className = in.readUTF(); |
173 int n = in.readInt(); | 157 int n = in.readInt(); |
174 Map<String,byte[]> map = new HashMap<String,byte[]>(); | 158 Map<String,byte[]> map = new HashMap<String,byte[]>(); |
175 for( int i=0; i<n; i++ ) { | 159 for( int i=0; i<n; i++ ) { |
188 } | 172 } |
189 | 173 |
190 void save(String fileName,String key) { | 174 void save(String fileName,String key) { |
191 try { | 175 try { |
192 File f = new File(tmpDir,fileName); | 176 File f = new File(tmpDir,fileName); |
193 DataOutputStream out = new DataOutputStream(new FileOutputStream(f)); | 177 DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(f))); |
194 out.writeInt(VERSION); | 178 out.writeInt(VERSION); |
195 writeLongString(out,key); | 179 out.writeString(key); |
196 out.writeUTF(className); | 180 out.writeUTF(className); |
197 out.writeInt(map.size()); | 181 out.writeInt(map.size()); |
198 for( Map.Entry<String,byte[]> entry : map.entrySet() ) { | 182 for( Map.Entry<String,byte[]> entry : map.entrySet() ) { |
199 out.writeUTF( entry.getKey() ); | 183 out.writeUTF( entry.getKey() ); |
200 byte[] a = entry.getValue(); | 184 byte[] a = entry.getValue(); |
201 out.writeInt(a.length); | 185 out.writeInt(a.length); |
202 out.write(a,0,a.length); | 186 out.write(a); |
203 } | 187 } |
204 out.close(); | 188 out.close(); |
205 } catch(IOException e) { | 189 } catch(IOException e) { |
206 logger.error("save failed",e); | 190 logger.error("save failed",e); |
207 } | 191 } |