comparison src/luan/impl/Compiled.java @ 1436:65d4afc9ad07

cache fix
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 06 Jan 2020 18:17:46 -0700
parents 56fb5cd8228d
children aaac1d29edea
comparison
equal deleted inserted replaced
1435:3aae37efbf40 1436:65d4afc9ad07
129 throw new RuntimeException(e); 129 throw new RuntimeException(e);
130 } 130 }
131 } 131 }
132 132
133 133
134 private static final int VERSION = 1; 134 private static final int VERSION = 2;
135 private static final File tmpDir; 135 private static final File tmpDir;
136 static { 136 static {
137 File f = new File(System.getProperty("java.io.tmpdir")); 137 File f = new File(System.getProperty("java.io.tmpdir"));
138 tmpDir = new File(f,"luan"); 138 tmpDir = new File(f,"luan");
139 tmpDir.mkdir(); 139 tmpDir.mkdir();
140 if( !tmpDir.exists() ) 140 if( !tmpDir.exists() )
141 throw new RuntimeException(); 141 throw new RuntimeException();
142 }
143
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();
142 } 160 }
143 161
144 static Compiled load(String fileName,String key) { 162 static Compiled load(String fileName,String key) {
145 try { 163 try {
146 File f = new File(tmpDir,fileName); 164 File f = new File(tmpDir,fileName);
147 if( !f.exists() ) 165 if( !f.exists() )
148 return null; 166 return null;
149 DataInputStream in = new DataInputStream(new FileInputStream(f)); 167 DataInputStream in = new DataInputStream(new FileInputStream(f));
150 if( in.readInt() != VERSION ) 168 if( in.readInt() != VERSION )
151 return null; 169 return null;
152 if( !in.readUTF().equals(key) ) 170 if( !readLongString(in).equals(key) )
153 return null; 171 return null;
154 String className = in.readUTF(); 172 String className = in.readUTF();
155 int n = in.readInt(); 173 int n = in.readInt();
156 Map<String,byte[]> map = new HashMap<String,byte[]>(); 174 Map<String,byte[]> map = new HashMap<String,byte[]>();
157 for( int i=0; i<n; i++ ) { 175 for( int i=0; i<n; i++ ) {
172 void save(String fileName,String key) { 190 void save(String fileName,String key) {
173 try { 191 try {
174 File f = new File(tmpDir,fileName); 192 File f = new File(tmpDir,fileName);
175 DataOutputStream out = new DataOutputStream(new FileOutputStream(f)); 193 DataOutputStream out = new DataOutputStream(new FileOutputStream(f));
176 out.writeInt(VERSION); 194 out.writeInt(VERSION);
177 out.writeUTF(key); 195 writeLongString(out,key);
178 out.writeUTF(className); 196 out.writeUTF(className);
179 out.writeInt(map.size()); 197 out.writeInt(map.size());
180 for( Map.Entry<String,byte[]> entry : map.entrySet() ) { 198 for( Map.Entry<String,byte[]> entry : map.entrySet() ) {
181 out.writeUTF( entry.getKey() ); 199 out.writeUTF( entry.getKey() );
182 byte[] a = entry.getValue(); 200 byte[] a = entry.getValue();