package io.protostuff.compiler.generator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author Kostiantyn Shchepanovskyi
 */
public class CompilerUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(CompilerUtils.class);

    private final OutputStreamFactory outputStreamFactory;

    @Inject
    public CompilerUtils(OutputStreamFactory outputStreamFactory) {
        this.outputStreamFactory = outputStreamFactory;
    }


    public void copyResource(String name, String destinationFilename) {
        OutputStream outputStream = null;
        try {
            outputStream = outputStreamFactory.createStream(destinationFilename);
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            if (classLoader == null) {
                throw new IllegalStateException("Can not obtain classloader instance from current thread");
            }
            URL resource = classLoader.getResource(name);
            if (resource != null) {
                try {
                    Path path = Paths.get(resource.toURI());
                    byte[] bytes = Files.readAllBytes(path);
                    outputStream.write(bytes);
                    outputStream.close();
                } catch (IOException | URISyntaxException e) {
                    throw new GeneratorException("Could not copy %s", e, name);
                }
            }
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    LOGGER.error("Could not close file: " + name, e);
                }
            }
        }
    }

}

@@ -36,16 +36,56 @@
 rator;%0A%0A
+import org.apache.commons.io.FileUtils;%0A
 import o
@@ -138,36 +138,8 @@
 y;%0A%0A
-import javax.inject.Inject;%0A
 impo
@@ -177,19 +177,18 @@
 java.io.
-Out
+In
 putStrea
@@ -206,79 +206,23 @@
 ava.
-net.URISyntaxException;%0Aimport java.net.URL;%0Aimport java.nio.file.Files
+io.OutputStream
 ;%0Aim
@@ -275,16 +275,45 @@
 Paths;%0A%0A
+import javax.inject.Inject;%0A%0A
 /**%0A * @
@@ -1026,40 +1026,23 @@
     
-throw new IllegalStateException(
+String error = 
 %22Can
@@ -1098,49 +1098,132 @@
 ead%22
-);%0A            %7D%0A            URL resource
+;%0A                throw new IllegalStateException(error);%0A            %7D%0A            try %7B%0A                InputStream stream
  = c
@@ -1244,16 +1244,24 @@
 Resource
+AsStream
 (name);%0A
@@ -1276,22 +1276,24 @@
     
-if (resource !
+    if (stream =
 = nu
@@ -1318,133 +1318,139 @@
     
-try %7B%0A                    Path path = Paths.get(resource.toURI());%0A                    byte%5B%5D bytes = Files.readAllBytes(path
+    String error = %22Could not copy file, source file not found: %22 + name;%0A                    throw new IllegalStateException(error
 );%0A 
@@ -1468,85 +1468,142 @@
     
+%7D%0A
     
-outputStream.write(bytes);%0A                    outputStream.clos
+            Path path = Paths.get(destinationFilename);%0A                FileUtils.copyInputStreamToFile(stream, path.toFil
 e()
+)
 ;%0A
-    
     
@@ -1635,38 +1635,13 @@
 ion 
-%7C URISyntaxException 
 e) %7B%0A
-    
     
@@ -1712,34 +1712,16 @@
  name);%0A
-                %7D%0A
         

Fix FileSystemNotFoundException when copying resource to file