2323
2424package com .codename1 .tools .translator ;
2525
26- import java .io .BufferedInputStream ;
27- import java .io .BufferedOutputStream ;
2826import java .io .DataInputStream ;
2927import java .io .File ;
3028import java .io .FileFilter ;
3533import java .io .IOException ;
3634import java .io .InputStream ;
3735import java .io .OutputStream ;
38- import java .nio .file .Files ;
39- import java .nio .file .StandardCopyOption ;
40- import java .nio .file .StandardOpenOption ;
4136import java .util .ArrayList ;
4237import java .util .Arrays ;
4338import java .util .HashSet ;
@@ -241,14 +236,14 @@ private static void handleCleanOutput(ByteCodeTranslator b, File[] sources, File
241236 b .execute (sources , srcRoot );
242237
243238 File cn1Globals = new File (srcRoot , "cn1_globals.h" );
244- Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/cn1_globals.h" ), cn1Globals . toPath (), StandardCopyOption . REPLACE_EXISTING );
239+ copy (ByteCodeTranslator .class .getResourceAsStream ("/cn1_globals.h" ), new FileOutputStream ( cn1Globals ) );
245240 if (System .getProperty ("INCLUDE_NPE_CHECKS" , "false" ).equals ("true" )) {
246241 replaceInFile (cn1Globals , "//#define CN1_INCLUDE_NPE_CHECKS" , "#define CN1_INCLUDE_NPE_CHECKS" );
247242 }
248243 File xmlvm = new File (srcRoot , "xmlvm.h" );
249- Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/xmlvm.h" ), xmlvm . toPath (), StandardCopyOption . REPLACE_EXISTING );
244+ copy (ByteCodeTranslator .class .getResourceAsStream ("/xmlvm.h" ), new FileOutputStream ( xmlvm ) );
250245 File nativeMethods = new File (srcRoot , "nativeMethods.m" );
251- Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/nativeMethods.m" ), nativeMethods . toPath (), StandardCopyOption . REPLACE_EXISTING );
246+ copy (ByteCodeTranslator .class .getResourceAsStream ("/nativeMethods.m" ), new FileOutputStream ( nativeMethods ) );
252247
253248 Parser .writeOutput (srcRoot );
254249
@@ -273,13 +268,13 @@ private static void handleIosOutput(ByteCodeTranslator b, File[] sources, File d
273268 launchImageLaunchimage .mkdirs ();
274269 //cleanDir(launchImageLaunchimage);
275270
276- Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/LaunchImages.json" ), new File (launchImageLaunchimage , "Contents.json" ). toPath (), StandardCopyOption . REPLACE_EXISTING );
271+ copy (ByteCodeTranslator .class .getResourceAsStream ("/LaunchImages.json" ), new FileOutputStream ( new File (launchImageLaunchimage , "Contents.json" )) );
277272
278273 File appIconAppiconset = new File (imagesXcassets , "AppIcon.appiconset" );
279274 appIconAppiconset .mkdirs ();
280275 //cleanDir(appIconAppiconset);
281276
282- Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/Icons.json" ), new File (appIconAppiconset , "Contents.json" ). toPath (), StandardCopyOption . REPLACE_EXISTING );
277+ copy (ByteCodeTranslator .class .getResourceAsStream ("/Icons.json" ), new FileOutputStream ( new File (appIconAppiconset , "Contents.json" )) );
283278
284279
285280 File xcproj = new File (root , appName + ".xcodeproj" );
@@ -296,37 +291,37 @@ private static void handleIosOutput(ByteCodeTranslator b, File[] sources, File d
296291 b .execute (sources , srcRoot );
297292
298293 File cn1Globals = new File (srcRoot , "cn1_globals.h" );
299- Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/cn1_globals.h" ), cn1Globals . toPath (), StandardCopyOption . REPLACE_EXISTING );
294+ copy (ByteCodeTranslator .class .getResourceAsStream ("/cn1_globals.h" ), new FileOutputStream ( cn1Globals ) );
300295 if (System .getProperty ("INCLUDE_NPE_CHECKS" , "false" ).equals ("true" )) {
301296 replaceInFile (cn1Globals , "//#define CN1_INCLUDE_NPE_CHECKS" , "#define CN1_INCLUDE_NPE_CHECKS" );
302297 }
303298 File cn1GlobalsM = new File (srcRoot , "cn1_globals.m" );
304- Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/cn1_globals.m" ), cn1GlobalsM . toPath (), StandardCopyOption . REPLACE_EXISTING );
299+ copy (ByteCodeTranslator .class .getResourceAsStream ("/cn1_globals.m" ), new FileOutputStream ( cn1GlobalsM ) );
305300 File nativeMethods = new File (srcRoot , "nativeMethods.m" );
306- Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/nativeMethods.m" ), nativeMethods . toPath (), StandardCopyOption . REPLACE_EXISTING );
301+ copy (ByteCodeTranslator .class .getResourceAsStream ("/nativeMethods.m" ), new FileOutputStream ( nativeMethods ) );
307302
308303 if (System .getProperty ("USE_RPMALLOC" , "false" ).equals ("true" )) {
309304 File malloc = new File (srcRoot , "malloc.c" );
310- Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/malloc.c" ), malloc . toPath (), StandardCopyOption . REPLACE_EXISTING );
305+ copy (ByteCodeTranslator .class .getResourceAsStream ("/malloc.c" ), new FileOutputStream ( malloc ) );
311306 File rpmalloc = new File (srcRoot , "rpmalloc.c" );
312- Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/rpmalloc.c" ), rpmalloc . toPath (), StandardCopyOption . REPLACE_EXISTING );
307+ copy (ByteCodeTranslator .class .getResourceAsStream ("/rpmalloc.c" ), new FileOutputStream ( rpmalloc ) );
313308 File rpmalloch = new File (srcRoot , "rpmalloc.h" );
314- Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/rpmalloc.h" ), rpmalloch . toPath (), StandardCopyOption . REPLACE_EXISTING );
309+ copy (ByteCodeTranslator .class .getResourceAsStream ("/rpmalloc.h" ), new FileOutputStream ( rpmalloch ) );
315310 }
316311
317312 Parser .writeOutput (srcRoot );
318313
319314 File templateInfoPlist = new File (srcRoot , appName + "-Info.plist" );
320- Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/template/template/template-Info.plist" ), templateInfoPlist . toPath (), StandardCopyOption . REPLACE_EXISTING );
315+ copy (ByteCodeTranslator .class .getResourceAsStream ("/template/template/template-Info.plist" ), new FileOutputStream ( templateInfoPlist ) );
321316
322317 File templatePch = new File (srcRoot , appName + "-Prefix.pch" );
323- Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/template/template/template-Prefix.pch" ), templatePch . toPath (), StandardCopyOption . REPLACE_EXISTING );
318+ copy (ByteCodeTranslator .class .getResourceAsStream ("/template/template/template-Prefix.pch" ), new FileOutputStream ( templatePch ) );
324319
325320 File xmlvm = new File (srcRoot , "xmlvm.h" );
326- Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/xmlvm.h" ), xmlvm . toPath (), StandardCopyOption . REPLACE_EXISTING );
321+ copy (ByteCodeTranslator .class .getResourceAsStream ("/xmlvm.h" ), new FileOutputStream ( xmlvm ) );
327322
328323 File projectWorkspaceData = new File (projectXCworkspace , "contents.xcworkspacedata" );
329- Files . copy (ByteCodeTranslator .class .getResourceAsStream ("/template/template.xcodeproj/project.xcworkspace/contents.xcworkspacedata" ), projectWorkspaceData . toPath (), StandardCopyOption . REPLACE_EXISTING );
324+ copy (ByteCodeTranslator .class .getResourceAsStream ("/template/template.xcodeproj/project.xcworkspace/contents.xcworkspacedata" ), new FileOutputStream ( projectWorkspaceData ) );
330325 replaceInFile (projectWorkspaceData , "KitchenSink" , appName );
331326
332327
@@ -625,8 +620,11 @@ private static String getFileType(String s) {
625620 //
626621 private static StringBuilder readFileAsStringBuilder (File sourceFile ) throws IOException
627622 {
628- byte [] data = Files .readAllBytes (sourceFile .toPath ());
629- StringBuilder b = new StringBuilder (new String (data , "UTF-8" ));
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 ));
630628 return b ;
631629 }
632630 //
@@ -656,7 +654,9 @@ private static void replaceInFile(File sourceFile, String... values) throws IOEx
656654 // don't start the output file until all the processing is done
657655 //
658656 System .out .println ("Rewrite " + sourceFile + " with " + totchanges + " changes" );
659- Files .write (sourceFile .toPath (), str .toString ().getBytes ("UTF-8" ), StandardOpenOption .TRUNCATE_EXISTING , StandardOpenOption .WRITE );
657+ FileWriter fios = new FileWriter (sourceFile );
658+ fios .write (str .toString ());
659+ fios .close ();
660660 }
661661
662662
@@ -683,25 +683,12 @@ 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- }
698686 byte [] buffer = new byte [bufferSize ];
699687 int size = i .read (buffer );
700688 while (size > -1 ) {
701689 o .write (buffer , 0 , size );
702690 size = i .read (buffer );
703691 }
704- o .flush ();
705692 } finally {
706693 cleanup (o );
707694 cleanup (i );
0 commit comments