2323
2424package com .codename1 .tools .translator ;
2525
26+ import java .io .BufferedInputStream ;
27+ import java .io .BufferedOutputStream ;
2628import java .io .DataInputStream ;
2729import java .io .File ;
2830import java .io .FileFilter ;
3335import java .io .IOException ;
3436import java .io .InputStream ;
3537import java .io .OutputStream ;
38+ import java .nio .file .Files ;
39+ import java .nio .file .StandardCopyOption ;
40+ import java .nio .file .StandardOpenOption ;
3641import java .util .ArrayList ;
3742import java .util .Arrays ;
3843import java .util .HashSet ;
@@ -236,14 +241,14 @@ private static void handleCleanOutput(ByteCodeTranslator b, File[] sources, File
236241 b .execute (sources , srcRoot );
237242
238243 File cn1Globals = new File (srcRoot , "cn1_globals.h" );
239- copy (ByteCodeTranslator .class .getResourceAsStream ("/cn1_globals.h" ), new FileOutputStream ( cn1Globals ) );
244+ Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/cn1_globals.h" ), cn1Globals . toPath (), StandardCopyOption . REPLACE_EXISTING );
240245 if (System .getProperty ("INCLUDE_NPE_CHECKS" , "false" ).equals ("true" )) {
241246 replaceInFile (cn1Globals , "//#define CN1_INCLUDE_NPE_CHECKS" , "#define CN1_INCLUDE_NPE_CHECKS" );
242247 }
243248 File xmlvm = new File (srcRoot , "xmlvm.h" );
244- copy (ByteCodeTranslator .class .getResourceAsStream ("/xmlvm.h" ), new FileOutputStream ( xmlvm ) );
249+ Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/xmlvm.h" ), xmlvm . toPath (), StandardCopyOption . REPLACE_EXISTING );
245250 File nativeMethods = new File (srcRoot , "nativeMethods.m" );
246- copy (ByteCodeTranslator .class .getResourceAsStream ("/nativeMethods.m" ), new FileOutputStream ( nativeMethods ) );
251+ Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/nativeMethods.m" ), nativeMethods . toPath (), StandardCopyOption . REPLACE_EXISTING );
247252
248253 Parser .writeOutput (srcRoot );
249254
@@ -268,13 +273,13 @@ private static void handleIosOutput(ByteCodeTranslator b, File[] sources, File d
268273 launchImageLaunchimage .mkdirs ();
269274 //cleanDir(launchImageLaunchimage);
270275
271- copy (ByteCodeTranslator .class .getResourceAsStream ("/LaunchImages.json" ), new FileOutputStream ( new File (launchImageLaunchimage , "Contents.json" )) );
276+ Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/LaunchImages.json" ), new File (launchImageLaunchimage , "Contents.json" ). toPath (), StandardCopyOption . REPLACE_EXISTING );
272277
273278 File appIconAppiconset = new File (imagesXcassets , "AppIcon.appiconset" );
274279 appIconAppiconset .mkdirs ();
275280 //cleanDir(appIconAppiconset);
276281
277- copy (ByteCodeTranslator .class .getResourceAsStream ("/Icons.json" ), new FileOutputStream ( new File (appIconAppiconset , "Contents.json" )) );
282+ Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/Icons.json" ), new File (appIconAppiconset , "Contents.json" ). toPath (), StandardCopyOption . REPLACE_EXISTING );
278283
279284
280285 File xcproj = new File (root , appName + ".xcodeproj" );
@@ -291,37 +296,37 @@ private static void handleIosOutput(ByteCodeTranslator b, File[] sources, File d
291296 b .execute (sources , srcRoot );
292297
293298 File cn1Globals = new File (srcRoot , "cn1_globals.h" );
294- copy (ByteCodeTranslator .class .getResourceAsStream ("/cn1_globals.h" ), new FileOutputStream ( cn1Globals ) );
299+ Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/cn1_globals.h" ), cn1Globals . toPath (), StandardCopyOption . REPLACE_EXISTING );
295300 if (System .getProperty ("INCLUDE_NPE_CHECKS" , "false" ).equals ("true" )) {
296301 replaceInFile (cn1Globals , "//#define CN1_INCLUDE_NPE_CHECKS" , "#define CN1_INCLUDE_NPE_CHECKS" );
297302 }
298303 File cn1GlobalsM = new File (srcRoot , "cn1_globals.m" );
299- copy (ByteCodeTranslator .class .getResourceAsStream ("/cn1_globals.m" ), new FileOutputStream ( cn1GlobalsM ) );
304+ Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/cn1_globals.m" ), cn1GlobalsM . toPath (), StandardCopyOption . REPLACE_EXISTING );
300305 File nativeMethods = new File (srcRoot , "nativeMethods.m" );
301- copy (ByteCodeTranslator .class .getResourceAsStream ("/nativeMethods.m" ), new FileOutputStream ( nativeMethods ) );
306+ Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/nativeMethods.m" ), nativeMethods . toPath (), StandardCopyOption . REPLACE_EXISTING );
302307
303308 if (System .getProperty ("USE_RPMALLOC" , "false" ).equals ("true" )) {
304309 File malloc = new File (srcRoot , "malloc.c" );
305- copy (ByteCodeTranslator .class .getResourceAsStream ("/malloc.c" ), new FileOutputStream ( malloc ) );
310+ Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/malloc.c" ), malloc . toPath (), StandardCopyOption . REPLACE_EXISTING );
306311 File rpmalloc = new File (srcRoot , "rpmalloc.c" );
307- copy (ByteCodeTranslator .class .getResourceAsStream ("/rpmalloc.c" ), new FileOutputStream ( rpmalloc ) );
312+ Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/rpmalloc.c" ), rpmalloc . toPath (), StandardCopyOption . REPLACE_EXISTING );
308313 File rpmalloch = new File (srcRoot , "rpmalloc.h" );
309- copy (ByteCodeTranslator .class .getResourceAsStream ("/rpmalloc.h" ), new FileOutputStream ( rpmalloch ) );
314+ Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/rpmalloc.h" ), rpmalloch . toPath (), StandardCopyOption . REPLACE_EXISTING );
310315 }
311316
312317 Parser .writeOutput (srcRoot );
313318
314319 File templateInfoPlist = new File (srcRoot , appName + "-Info.plist" );
315- copy (ByteCodeTranslator .class .getResourceAsStream ("/template/template/template-Info.plist" ), new FileOutputStream ( templateInfoPlist ) );
320+ Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/template/template/template-Info.plist" ), templateInfoPlist . toPath (), StandardCopyOption . REPLACE_EXISTING );
316321
317322 File templatePch = new File (srcRoot , appName + "-Prefix.pch" );
318- copy (ByteCodeTranslator .class .getResourceAsStream ("/template/template/template-Prefix.pch" ), new FileOutputStream ( templatePch ) );
323+ Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/template/template/template-Prefix.pch" ), templatePch . toPath (), StandardCopyOption . REPLACE_EXISTING );
319324
320325 File xmlvm = new File (srcRoot , "xmlvm.h" );
321- copy (ByteCodeTranslator .class .getResourceAsStream ("/xmlvm.h" ), new FileOutputStream ( xmlvm ) );
326+ Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/xmlvm.h" ), xmlvm . toPath (), StandardCopyOption . REPLACE_EXISTING );
322327
323328 File projectWorkspaceData = new File (projectXCworkspace , "contents.xcworkspacedata" );
324- copy (ByteCodeTranslator .class .getResourceAsStream ("/template/template.xcodeproj/project.xcworkspace/contents.xcworkspacedata" ), new FileOutputStream ( projectWorkspaceData ) );
329+ Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/template/template.xcodeproj/project.xcworkspace/contents.xcworkspacedata" ), projectWorkspaceData . toPath (), StandardCopyOption . REPLACE_EXISTING );
325330 replaceInFile (projectWorkspaceData , "KitchenSink" , appName );
326331
327332
@@ -620,11 +625,8 @@ private static String getFileType(String s) {
620625 //
621626 private static StringBuilder readFileAsStringBuilder (File sourceFile ) throws IOException
622627 {
623- DataInputStream dis = new DataInputStream (new FileInputStream (sourceFile ));
624- byte [] data = new byte [(int )sourceFile .length ()];
625- dis .readFully (data );
626- dis .close ();
627- StringBuilder b = new StringBuilder (new String (data ));
628+ byte [] data = Files .readAllBytes (sourceFile .toPath ());
629+ StringBuilder b = new StringBuilder (new String (data , "UTF-8" ));
628630 return b ;
629631 }
630632 //
@@ -654,9 +656,7 @@ private static void replaceInFile(File sourceFile, String... values) throws IOEx
654656 // don't start the output file until all the processing is done
655657 //
656658 System .out .println ("Rewrite " + sourceFile + " with " + totchanges + " changes" );
657- FileWriter fios = new FileWriter (sourceFile );
658- fios .write (str .toString ());
659- fios .close ();
659+ Files .write (sourceFile .toPath (), str .toString ().getBytes ("UTF-8" ), StandardOpenOption .TRUNCATE_EXISTING , StandardOpenOption .WRITE );
660660 }
661661
662662
@@ -683,12 +683,25 @@ public static void copy(InputStream i, OutputStream o) throws IOException {
683683 */
684684 public static void copy (InputStream i , OutputStream o , int bufferSize ) throws IOException {
685685 try {
686+ if (i instanceof FileInputStream && o instanceof FileOutputStream ) {
687+ // This case should be handled by caller using Files.copy usually, but if streams are passed we can't cast to Path
688+ // We can't use Files.copy with streams easily without copying to temp.
689+ // So we stick to stream copy but ensure buffering.
690+ }
691+
692+ if (!(i instanceof BufferedInputStream )) {
693+ i = new BufferedInputStream (i );
694+ }
695+ if (!(o instanceof BufferedOutputStream )) {
696+ o = new BufferedOutputStream (o );
697+ }
686698 byte [] buffer = new byte [bufferSize ];
687699 int size = i .read (buffer );
688700 while (size > -1 ) {
689701 o .write (buffer , 0 , size );
690702 size = i .read (buffer );
691703 }
704+ o .flush ();
692705 } finally {
693706 cleanup (o );
694707 cleanup (i );
0 commit comments