OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | Rev 156 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 144 Rev 149
Line 401... Line 401...
401
     * @param out the destination file.
401
     * @param out the destination file.
402
     * @param maxCount the number of bytes to copy at a time, 0 meaning size of <code>in</code>.
402
     * @param maxCount the number of bytes to copy at a time, 0 meaning size of <code>in</code>.
403
     * @throws IOException if an error occurs.
403
     * @throws IOException if an error occurs.
404
     */
404
     */
405
    public static void copyFile(File in, File out, long maxCount) throws IOException {
405
    public static void copyFile(File in, File out, long maxCount) throws IOException {
406
        final FileInputStream sourceIn = new FileInputStream(in);
406
        try (final FileInputStream sourceIn = new FileInputStream(in); final FileOutputStream sourceOut = new FileOutputStream(out);) {
407
        FileOutputStream sourceOut = null;
-
 
408
        try {
-
 
409
            sourceOut = new FileOutputStream(out);
-
 
410
            final FileChannel sourceChannel = sourceIn.getChannel();
407
            final FileChannel sourceChannel = sourceIn.getChannel();
411
            final long size = sourceChannel.size();
408
            final long size = sourceChannel.size();
412
            if (maxCount == 0)
409
            if (maxCount == 0)
413
                maxCount = size;
410
                maxCount = size;
414
            final FileChannel destinationChannel = sourceOut.getChannel();
411
            final FileChannel destinationChannel = sourceOut.getChannel();
415
            long position = 0;
412
            long position = 0;
416
            while (position < size) {
413
            while (position < size) {
417
                position += sourceChannel.transferTo(position, maxCount, destinationChannel);
414
                position += sourceChannel.transferTo(position, maxCount, destinationChannel);
418
            }
415
            }
419
        } finally {
-
 
420
            sourceIn.close();
-
 
421
            if (sourceOut != null)
-
 
422
                sourceOut.close();
-
 
423
        }
416
        }
424
    }
417
    }
425
 
418
 
426
    public static void copyFile(File in, File out, final boolean useTime) throws IOException {
419
    public static void copyFile(File in, File out, final boolean useTime) throws IOException {
427
        if (!useTime || in.lastModified() != out.lastModified()) {
420
        if (!useTime || in.lastModified() != out.lastModified()) {
Line 677... Line 670...
677
    public static void write(String s, File f) throws IOException {
670
    public static void write(String s, File f) throws IOException {
678
        write(s, f, null, false);
671
        write(s, f, null, false);
679
    }
672
    }
680
 
673
 
681
    public static void write(String s, File f, Charset charset, boolean append) throws IOException {
674
    public static void write(String s, File f, Charset charset, boolean append) throws IOException {
682
        final FileOutputStream fileStream = new FileOutputStream(f, append);
675
        try (final FileOutputStream fileStream = new FileOutputStream(f, append);
683
        final OutputStreamWriter out = charset == null ? new OutputStreamWriter(fileStream) : new OutputStreamWriter(fileStream, charset);
676
                final BufferedWriter w = new BufferedWriter(charset == null ? new OutputStreamWriter(fileStream) : new OutputStreamWriter(fileStream, charset))) {
684
        final BufferedWriter w = new BufferedWriter(out);
-
 
685
        try {
-
 
686
            w.write(s);
677
            w.write(s);
687
        } finally {
-
 
688
            w.close();
-
 
689
            // should have already been closed by the writer but the documentation is vague and
-
 
690
            // there's no way to check
-
 
691
            fileStream.close();
-
 
692
        }
678
        }
693
    }
679
    }
694
 
680
 
695
    /**
681
    /**
696
     * Create a writer for the passed file, and write the XML declaration.
682
     * Create a writer for the passed file, and write the XML declaration.
Line 786... Line 772...
786
            shortcutFile = File.createTempFile("windowsIsLame", ".vbs");
772
            shortcutFile = File.createTempFile("windowsIsLame", ".vbs");
787
            // ATTN if the VM is not terminated normally, the file won't be deleted
773
            // ATTN if the VM is not terminated normally, the file won't be deleted
788
            // perhaps a thread to delete the file after a certain amount of time
774
            // perhaps a thread to delete the file after a certain amount of time
789
            shortcutFile.deleteOnExit();
775
            shortcutFile.deleteOnExit();
790
            files.put(url, shortcutFile);
776
            files.put(url, shortcutFile);
791
            final InputStream stream = url.openStream();
-
 
792
            final FileOutputStream out = new FileOutputStream(shortcutFile);
777
            try (final InputStream stream = url.openStream(); final FileOutputStream out = new FileOutputStream(shortcutFile);) {
793
            try {
-
 
794
                StreamUtils.copy(stream, out);
778
                StreamUtils.copy(stream, out);
795
            } finally {
-
 
796
                out.close();
-
 
797
                stream.close();
-
 
798
            }
779
            }
799
        } else
780
        } else
800
            shortcutFile = currentFile;
781
            shortcutFile = currentFile;
801
        return shortcutFile;
782
        return shortcutFile;
802
    }
783
    }
Line 858... Line 839...
858
            // add -f to canonicalize
839
            // add -f to canonicalize
859
            ps = Runtime.getRuntime().exec(new String[] { "readlink", "-f", link.getAbsolutePath() });
840
            ps = Runtime.getRuntime().exec(new String[] { "readlink", "-f", link.getAbsolutePath() });
860
        }
841
        }
861
        try {
842
        try {
862
            ps.getErrorStream().close();
843
            ps.getErrorStream().close();
-
 
844
            final String res;
863
            final BufferedReader reader = new BufferedReader(new InputStreamReader(ps.getInputStream()));
845
            try (final BufferedReader reader = new BufferedReader(new InputStreamReader(ps.getInputStream()));) {
864
            final String res = reader.readLine();
846
                res = reader.readLine();
865
            reader.close();
847
            }
866
            if (ps.waitFor() != 0 || res == null || res.length() == 0)
848
            if (ps.waitFor() != 0 || res == null || res.length() == 0)
867
                return null;
849
                return null;
868
            else
850
            else
869
                return new File(res);
851
                return new File(res);
870
        } catch (InterruptedException e) {
852
        } catch (InterruptedException e) {