/*
 * Copyright (C) 2011 Thomas Akehurst
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.tomakehurst.wiremock.common;

import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import com.google.common.primitives.Bytes;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

public class BinaryFile {

    private URI uri;

    public BinaryFile(URI uri) {
        this.uri = uri;
    }
	
	public byte[] readContents() {
		try {
            return ByteStreams.toByteArray(uri.toURL().openStream());
        } catch (final IOException ioe) {
			throw new RuntimeException(ioe);
		}
	}
	
	public String name() {
		return uri.toString();
	}
	
	@Override
	public String toString() {
		return name();
	}
}

@@ -686,107 +686,8 @@
 ms;%0A
-import com.google.common.io.Files;%0Aimport com.google.common.primitives.Bytes;%0A%0Aimport java.io.File;
 %0Aimp
@@ -873,19 +873,21 @@
 ;%0A    %7D%0A
-%09%0A%09
+%0A    
 public b
@@ -913,94 +913,521 @@
 ) %7B%0A
-%09%09try %7B%0A            return ByteStreams.toByteArray(uri.toURL().openStream());%0A
+        InputStream stream = null;%0A        try %7B%0A            stream = uri.toURL().openStream();%0A            return ByteStreams.toByteArray(stream);%0A        %7D catch (final IOException ioe) %7B%0A            throw new RuntimeException(ioe);%0A        %7D finally %7B%0A            closeStream(stream);%0A        %7D%0A    %7D%0A%0A    /**%0A     * @param stream Stream to close, may be null%0A     */%0A    private void closeStream(InputStream stream) %7B%0A        if (stream != null) %7B%0A            try %7B%0A                stream.close();%0A   
         
 %7D ca
@@ -1414,32 +1414,33 @@
 e();%0A           
+ 
 %7D catch (final I
@@ -1424,38 +1424,32 @@
        %7D catch (
-final 
 IOException ioe)
@@ -1455,11 +1455,24 @@
 ) %7B%0A
-%09%09%09
+                
 thro
@@ -1504,18 +1504,43 @@
 e);%0A
-%09%09%7D%0A%09%7D%0A%09%0A%09
+            %7D%0A        %7D%0A    %7D%0A%0A    
 publ
@@ -1554,26 +1554,32 @@
 ng name() %7B%0A
-%09%09
+        
 return uri.t
@@ -1593,14 +1593,19 @@
 ();%0A
-%09%7D%0A%09%0A%09
+    %7D%0A%0A    
 @Ove
@@ -1610,17 +1610,20 @@
 verride%0A
-%09
+    
 public S
@@ -1645,10 +1645,16 @@
 ) %7B%0A
-%09%09
+        
 retu
@@ -1668,9 +1668,12 @@
 ();%0A
-%09
+    
 %7D%0A%7D%0A

Make sure we close files after having read them (causes tests to fail otherwise and is generally good practice)