comparison core/src/luan/modules/IoLuan.java @ 761:99356cfde2f0

remove horrible java zip
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 20 Jul 2016 01:52:20 -0600
parents ae612dfc57cb
children d2f61d5be9cc
comparison
equal deleted inserted replaced
760:2a91bde4e1e1 761:99356cfde2f0
30 import java.net.MalformedURLException; 30 import java.net.MalformedURLException;
31 import java.net.UnknownHostException; 31 import java.net.UnknownHostException;
32 import java.util.Enumeration; 32 import java.util.Enumeration;
33 import java.util.List; 33 import java.util.List;
34 import java.util.Map; 34 import java.util.Map;
35 import java.util.zip.ZipEntry;
36 import java.util.zip.ZipInputStream;
37 import java.util.zip.ZipOutputStream;
38 import luan.Luan; 35 import luan.Luan;
39 import luan.LuanState; 36 import luan.LuanState;
40 import luan.LuanTable; 37 import luan.LuanTable;
41 import luan.LuanFunction; 38 import luan.LuanFunction;
42 import luan.LuanJavaFunction; 39 import luan.LuanJavaFunction;
226 } catch(FileNotFoundException e) { 223 } catch(FileNotFoundException e) {
227 return false; 224 return false;
228 } 225 }
229 } 226 }
230 227
231 public void unzip(Object path) throws IOException, LuanException {
232 File pathFile = objToFile(path);
233 if( pathFile==null )
234 throw new LuanException( "bad argument #1 to 'unzip' (string or file table expected)" );
235 ZipInputStream in = new ZipInputStream(new BufferedInputStream(inputStream()));
236 ZipEntry entry;
237 while( (entry = in.getNextEntry()) != null ) {
238 if( entry.isDirectory() )
239 continue;
240 File file = new File(pathFile,entry.getName());
241 file.getParentFile().mkdirs();
242 OutputStream out = new FileOutputStream(file);
243 Utils.copyAll(in,out);
244 out.close();
245 file.setLastModified(entry.getTime());
246 }
247 in.close();
248 }
249
250 public LuanTable table() { 228 public LuanTable table() {
251 LuanTable tbl = new LuanTable(); 229 LuanTable tbl = new LuanTable();
252 try { 230 try {
253 tbl.rawPut( "java", this ); 231 tbl.rawPut( "java", this );
254 tbl.rawPut( "to_string", new LuanJavaFunction( 232 tbl.rawPut( "to_string", new LuanJavaFunction(
269 tbl.rawPut( "read_blocks", new LuanJavaFunction( 247 tbl.rawPut( "read_blocks", new LuanJavaFunction(
270 LuanIn.class.getMethod( "read_blocks", Integer.class ), this 248 LuanIn.class.getMethod( "read_blocks", Integer.class ), this
271 ) ); 249 ) );
272 tbl.rawPut( "exists", new LuanJavaFunction( 250 tbl.rawPut( "exists", new LuanJavaFunction(
273 LuanIn.class.getMethod( "exists" ), this 251 LuanIn.class.getMethod( "exists" ), this
274 ) );
275 tbl.rawPut( "unzip", new LuanJavaFunction(
276 LuanIn.class.getMethod( "unzip", Object.class ), this
277 ) ); 252 ) );
278 } catch(NoSuchMethodException e) { 253 } catch(NoSuchMethodException e) {
279 throw new RuntimeException(e); 254 throw new RuntimeException(e);
280 } 255 }
281 return tbl; 256 return tbl;
349 324
350 public LuanTable binary_writer() throws IOException { 325 public LuanTable binary_writer() throws IOException {
351 return binaryWriter(new BufferedOutputStream(outputStream())); 326 return binaryWriter(new BufferedOutputStream(outputStream()));
352 } 327 }
353 328
354 public void zip(LuanState luan,Object basePathObj,LuanTable filePathList) throws LuanException, IOException {
355 File basePathFile = objToFile(basePathObj);
356 if( basePathFile==null )
357 throw new LuanException( "bad argument #1 to 'zip' (string or file table expected)" );
358 String[] filePaths;
359 if( filePathList==null ) {
360 File file = basePathFile.getCanonicalFile();
361 filePaths = new String[]{file.toString()};
362 basePathFile = file.getParentFile();
363 } else {
364 List list = filePathList.asList();
365 filePaths = new String[list.size()];
366 for( int i=0; i<filePaths.length; i++ ) {
367 Object obj = list.get(i);
368 if( !(obj instanceof String) )
369 throw new LuanException("file paths must be strings");
370 filePaths[i] = (String)obj;
371 }
372 }
373 String basePath = basePathFile.toString() + '/';
374 ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream()));
375 zip(out,basePath,filePaths);
376 out.close();
377 }
378
379 private static void zip(ZipOutputStream out,String basePath,String[] filePaths) throws LuanException, IOException {
380 for( String filePath : filePaths ) {
381 File file = new File(filePath);
382 if( file.isDirectory() ) {
383 String[] children = file.list();
384 for( int i=0; i<children.length; i++ ) {
385 children[i] = filePath + "/" + children[i];
386 }
387 zip(out,basePath,children);
388 } else {
389 if( !filePath.startsWith(basePath) )
390 throw new LuanException(filePath+" not in "+basePath);
391 String relPath = filePath.substring(basePath.length());
392 ZipEntry entry = new ZipEntry(relPath);
393 entry.setTime(file.lastModified());
394 out.putNextEntry(entry);
395 InputStream in = new FileInputStream(file);
396 Utils.copyAll(in,out);
397 in.close();
398 out.closeEntry();
399 }
400 }
401 }
402
403 @Override public LuanTable table() { 329 @Override public LuanTable table() {
404 LuanTable tbl = super.table(); 330 LuanTable tbl = super.table();
405 try { 331 try {
406 tbl.rawPut( "write", new LuanJavaFunction( 332 tbl.rawPut( "write", new LuanJavaFunction(
407 LuanIO.class.getMethod( "write", Object.class ), this 333 LuanIO.class.getMethod( "write", Object.class ), this
409 tbl.rawPut( "text_writer", new LuanJavaFunction( 335 tbl.rawPut( "text_writer", new LuanJavaFunction(
410 LuanIO.class.getMethod( "text_writer" ), this 336 LuanIO.class.getMethod( "text_writer" ), this
411 ) ); 337 ) );
412 tbl.rawPut( "binary_writer", new LuanJavaFunction( 338 tbl.rawPut( "binary_writer", new LuanJavaFunction(
413 LuanIO.class.getMethod( "binary_writer" ), this 339 LuanIO.class.getMethod( "binary_writer" ), this
414 ) );
415 tbl.rawPut( "zip", new LuanJavaFunction(
416 LuanIO.class.getMethod( "zip", LuanState.class, Object.class, LuanTable.class ), this
417 ) ); 340 ) );
418 } catch(NoSuchMethodException e) { 341 } catch(NoSuchMethodException e) {
419 throw new RuntimeException(e); 342 throw new RuntimeException(e);
420 } 343 }
421 return tbl; 344 return tbl;
723 add( schemes, "socket", String.class ); 646 add( schemes, "socket", String.class );
724 add( schemes, "http", LuanState.class, String.class, LuanTable.class ); 647 add( schemes, "http", LuanState.class, String.class, LuanTable.class );
725 add( schemes, "https", LuanState.class, String.class, LuanTable.class ); 648 add( schemes, "https", LuanState.class, String.class, LuanTable.class );
726 add( schemes, "luan", LuanState.class, String.class ); 649 add( schemes, "luan", LuanState.class, String.class );
727 add( schemes, "stdin", LuanState.class ); 650 add( schemes, "stdin", LuanState.class );
728 add( schemes, "os", String.class ); 651 add( schemes, "os", LuanState.class, String.class, LuanTable.class );
729 } catch(NoSuchMethodException e) { 652 } catch(NoSuchMethodException e) {
730 throw new RuntimeException(e); 653 throw new RuntimeException(e);
731 } 654 }
732 return schemes; 655 return schemes;
733 } 656 }
816 }; 739 };
817 } 740 }
818 741
819 742
820 public static final class LuanOs extends LuanIO { 743 public static final class LuanOs extends LuanIO {
744 private final String cmd;
821 private final Process proc; 745 private final Process proc;
822 746
823 private LuanOs(String cmd) throws IOException { 747 private LuanOs(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
824 this.proc = Runtime.getRuntime().exec(cmd); 748 this.cmd = cmd;
749 File dir = null;
750 if( options != null ) {
751 Map map = options.asMap(luan);
752 Object obj = map.remove("dir");
753 dir = objToFile(obj);
754 if( dir==null )
755 throw new LuanException( "bad option 'dir' (string or file table expected)" );
756 if( !map.isEmpty() )
757 throw new LuanException( "unrecognized options: "+map );
758 }
759 this.proc = Runtime.getRuntime().exec(cmd,null,dir);
825 } 760 }
826 761
827 @Override public InputStream inputStream() throws IOException { 762 @Override public InputStream inputStream() throws IOException {
828 return proc.getInputStream(); 763 return proc.getInputStream();
829 } 764 }
853 throw new RuntimeException(e); 788 throw new RuntimeException(e);
854 } 789 }
855 int exitVal = proc.exitValue(); 790 int exitVal = proc.exitValue();
856 if( exitVal != 0 ) { 791 if( exitVal != 0 ) {
857 Reader err = new InputStreamReader(proc.getErrorStream()); 792 Reader err = new InputStreamReader(proc.getErrorStream());
858 String error = Utils.readAll(err); 793 String error = "error in: "+cmd+"\n"+Utils.readAll(err);
859 err.close(); 794 err.close();
860 throw new LuanException(error); 795 throw new LuanException(error);
861 } 796 }
862 } 797 }
863 798
878 } 813 }
879 return tbl; 814 return tbl;
880 } 815 }
881 } 816 }
882 817
883 public static LuanTable os(String cmd) throws IOException { 818 public static LuanTable os(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
884 return new LuanOs(cmd).table(); 819 return new LuanOs(luan,cmd,options).table();
885 } 820 }
886 821
887 822
888 public static String ip(String domain) { 823 public static String ip(String domain) {
889 try { 824 try {