package ch.johannes;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileUtil {

    public static String readFileInPackage(Object instanceOfPackage, String filename) {
        try {
            Package aPackage = instanceOfPackage.getClass().getPackage();
            String packageName = aPackage.getName();
            String resourceName = "/".concat(packageName.replace('.', '/')).concat("/").concat(filename);
            InputStream resourceAsStream = instanceOfPackage.getClass().getResourceAsStream(resourceName);
            if (resourceAsStream == null) {
                throw new IllegalArgumentException(String.format("Couldn't find resource '%s' in package '%s' (%s).", filename, packageName, resourceName));
            }

            return readInputStream(resourceAsStream);
        } catch (IOException e) {
            throw new IllegalArgumentException("Couldn't find or read resource " + filename, e);
        }
    }

    private static String readInputStream(InputStream inputStream) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(inputStream);
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        try {
            int result = bis.read();
            while (result != -1) {
                buf.write((byte) result);
                result = bis.read();
            }
            return buf.toString();
        } finally {
            bis.close();
            buf.close();
        }
    }

}

@@ -259,34 +259,16 @@
 name) %7B%0A
-        try %7B%0A    
         
@@ -333,28 +333,24 @@
 ();%0A        
-  
-  
 String packa
@@ -374,28 +374,24 @@
 .getName();%0A
-    
         Stri
@@ -476,32 +476,33 @@
 (filename);%0A
+%0A
         
     InputStr
@@ -489,20 +489,20 @@
         
-    
+try(
 InputStr
@@ -586,17 +586,19 @@
 rceName)
-;
+) %7B
 %0A       
@@ -1104,16 +1104,21 @@
         
+try (
 Buffered
@@ -1172,24 +1172,16 @@
 Stream);
-%0A       
  ByteArr
@@ -1232,21 +1232,9 @@
 am()
-;%0A        try
+)
  %7B%0A 
@@ -1393,24 +1393,24 @@
           %7D%0A
+
             
@@ -1436,78 +1436,8 @@
 ();%0A
-        %7D finally %7B%0A            bis.close();%0A            buf.close();%0A
     

Use new style try finally to close resources