comparison core/src/luan/modules/IoLuan.java @ 758:c29d11d675fd

added Json.toString() and rpc now sends tables as json
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 19 Jul 2016 00:57:37 -0600
parents ff865e954934
children ae612dfc57cb
comparison
equal deleted inserted replaced
757:e1dfeddfbc7b 758:c29d11d675fd
169 } 169 }
170 }; 170 };
171 } 171 }
172 172
173 173
174 private static File objToFile(Object obj) {
175 if( obj instanceof String ) {
176 return new File((String)obj);
177 }
178 if( obj instanceof LuanTable ) {
179 LuanTable t = (LuanTable)obj;
180 Object java = t.rawGet("java");
181 if( java instanceof LuanFile ) {
182 LuanFile luanFile = (LuanFile)java;
183 return luanFile.file;
184 }
185 }
186 return null;
187 }
188
174 189
175 public static abstract class LuanIn { 190 public static abstract class LuanIn {
176 public abstract InputStream inputStream() throws IOException, LuanException; 191 public abstract InputStream inputStream() throws IOException, LuanException;
177 public abstract String to_string(); 192 public abstract String to_string();
178 public abstract String to_uri_string(); 193 public abstract String to_uri_string();
211 } catch(FileNotFoundException e) { 226 } catch(FileNotFoundException e) {
212 return false; 227 return false;
213 } 228 }
214 } 229 }
215 230
216 public void unzip(String path) throws IOException, LuanException { 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)" );
217 ZipInputStream in = new ZipInputStream(new BufferedInputStream(inputStream())); 235 ZipInputStream in = new ZipInputStream(new BufferedInputStream(inputStream()));
218 ZipEntry entry; 236 ZipEntry entry;
219 while( (entry = in.getNextEntry()) != null ) { 237 while( (entry = in.getNextEntry()) != null ) {
220 if( entry.isDirectory() ) 238 if( entry.isDirectory() )
221 continue; 239 continue;
222 File file = new File(path,entry.getName()); 240 File file = new File(pathFile,entry.getName());
223 file.getParentFile().mkdirs(); 241 file.getParentFile().mkdirs();
224 OutputStream out = new FileOutputStream(file); 242 OutputStream out = new FileOutputStream(file);
225 Utils.copyAll(in,out); 243 Utils.copyAll(in,out);
226 out.close(); 244 out.close();
227 file.setLastModified(entry.getTime()); 245 file.setLastModified(entry.getTime());
253 ) ); 271 ) );
254 tbl.rawPut( "exists", new LuanJavaFunction( 272 tbl.rawPut( "exists", new LuanJavaFunction(
255 LuanIn.class.getMethod( "exists" ), this 273 LuanIn.class.getMethod( "exists" ), this
256 ) ); 274 ) );
257 tbl.rawPut( "unzip", new LuanJavaFunction( 275 tbl.rawPut( "unzip", new LuanJavaFunction(
258 LuanIn.class.getMethod( "unzip", String.class ), this 276 LuanIn.class.getMethod( "unzip", Object.class ), this
259 ) ); 277 ) );
260 } catch(NoSuchMethodException e) { 278 } catch(NoSuchMethodException e) {
261 throw new RuntimeException(e); 279 throw new RuntimeException(e);
262 } 280 }
263 return tbl; 281 return tbl;
331 349
332 public LuanTable binary_writer() throws IOException { 350 public LuanTable binary_writer() throws IOException {
333 return binaryWriter(new BufferedOutputStream(outputStream())); 351 return binaryWriter(new BufferedOutputStream(outputStream()));
334 } 352 }
335 353
336 public void zip(LuanState luan,String basePath,LuanTable filePathList) throws LuanException, IOException { 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)" );
337 String[] filePaths; 358 String[] filePaths;
338 if( filePathList==null ) { 359 if( filePathList==null ) {
339 File file = new File(basePath).getCanonicalFile(); 360 File file = basePathFile.getCanonicalFile();
340 filePaths = new String[]{file.toString()}; 361 filePaths = new String[]{file.toString()};
341 basePath = file.getParent(); 362 basePathFile = file.getParentFile();
342 } else { 363 } else {
343 List list = filePathList.asList(); 364 List list = filePathList.asList();
344 filePaths = new String[list.size()]; 365 filePaths = new String[list.size()];
345 for( int i=0; i<filePaths.length; i++ ) { 366 for( int i=0; i<filePaths.length; i++ ) {
346 Object obj = list.get(i); 367 Object obj = list.get(i);
347 if( !(obj instanceof String) ) 368 if( !(obj instanceof String) )
348 throw new LuanException("file paths must be strings"); 369 throw new LuanException("file paths must be strings");
349 filePaths[i] = (String)obj; 370 filePaths[i] = (String)obj;
350 } 371 }
351 } 372 }
352 if( !basePath.endsWith("/") ) 373 String basePath = basePathFile.toString() + '/';
353 basePath += '/';
354 ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream())); 374 ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream()));
355 zip(out,basePath,filePaths); 375 zip(out,basePath,filePaths);
356 out.close(); 376 out.close();
357 } 377 }
358 378
391 ) ); 411 ) );
392 tbl.rawPut( "binary_writer", new LuanJavaFunction( 412 tbl.rawPut( "binary_writer", new LuanJavaFunction(
393 LuanIO.class.getMethod( "binary_writer" ), this 413 LuanIO.class.getMethod( "binary_writer" ), this
394 ) ); 414 ) );
395 tbl.rawPut( "zip", new LuanJavaFunction( 415 tbl.rawPut( "zip", new LuanJavaFunction(
396 LuanIO.class.getMethod( "zip", LuanState.class, String.class, LuanTable.class ), this 416 LuanIO.class.getMethod( "zip", LuanState.class, Object.class, LuanTable.class ), this
397 ) ); 417 ) );
398 } catch(NoSuchMethodException e) { 418 } catch(NoSuchMethodException e) {
399 throw new RuntimeException(e); 419 throw new RuntimeException(e);
400 } 420 }
401 return tbl; 421 return tbl;
535 555
536 @Override public boolean exists() { 556 @Override public boolean exists() {
537 return file.exists(); 557 return file.exists();
538 } 558 }
539 559
540 public boolean rename_to(String dest) { 560 public void rename_to(Object destObj) throws LuanException {
541 return file.renameTo(new File(dest)); 561 File dest = objToFile(destObj);
562 if( dest==null )
563 throw new LuanException( "bad argument #1 to 'objToFile' (string or file table expected)" );
564 if( !file.renameTo(dest) )
565 throw new LuanException("couldn't rename file "+file+" to "+dest);
542 } 566 }
543 567
544 public LuanTable canonical(LuanState luan) throws LuanException, IOException { 568 public LuanTable canonical(LuanState luan) throws LuanException, IOException {
545 return new LuanFile(luan,file.getCanonicalFile()).table(); 569 return new LuanFile(luan,file.getCanonicalFile()).table();
546 } 570 }
603 ) ); 627 ) );
604 tbl.rawPut( "parent", new LuanJavaFunction( 628 tbl.rawPut( "parent", new LuanJavaFunction(
605 LuanFile.class.getMethod( "parent", LuanState.class ), this 629 LuanFile.class.getMethod( "parent", LuanState.class ), this
606 ) ); 630 ) );
607 tbl.rawPut( "rename_to", new LuanJavaFunction( 631 tbl.rawPut( "rename_to", new LuanJavaFunction(
608 LuanFile.class.getMethod( "rename_to", String.class ), this 632 LuanFile.class.getMethod( "rename_to", Object.class ), this
609 ) ); 633 ) );
610 tbl.rawPut( "canonical", new LuanJavaFunction( 634 tbl.rawPut( "canonical", new LuanJavaFunction(
611 LuanFile.class.getMethod( "canonical", LuanState.class ), this 635 LuanFile.class.getMethod( "canonical", LuanState.class ), this
612 ) ); 636 ) );
613 tbl.rawPut( "create_temp_file", new LuanJavaFunction( 637 tbl.rawPut( "create_temp_file", new LuanJavaFunction(