/*******************************************************************************
 * Copyright 2012-present Pixate, Inc.
 * 
 * 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.pixate.freestyle.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class IOUtil {

    private static final String UTF8 = "UTF-8";

    public static String read(String filePath) throws IOException {
        return read(filePath, UTF8);
    }

    public static String read(String filePath, String charsetName) throws IOException {
        return read(new FileInputStream(filePath), charsetName);
    }

    public static String read(InputStream inputStream) throws IOException {
        return read(inputStream, UTF8);
    }

    public static String read(InputStream inputStream, String charsetName) throws IOException {
        char[] readBuffer = new char[1024];
        InputStreamReader reader = null;
        try {
            reader = new InputStreamReader(inputStream, charsetName);
            StringBuilder builder = new StringBuilder();
            int read = -1;
            while ((read = reader.read(readBuffer)) != -1) {
                builder.append(readBuffer, 0, read);
            }
            return builder.toString();
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }
}

@@ -984,16 +984,72 @@
 UTF-8%22;%0A
+    public static final String UTF8_BOM = %22%5CuFEFF%22;%0A    
 %0A    pub
@@ -1925,22 +1925,26 @@
         
-return
+String s =
  builder
@@ -1952,24 +1952,177 @@
 toString();%0A
+            %0A            if (s.startsWith(UTF8_BOM)) %7B%0A                s = s.substring(1);%0A            %7D%0A            %0A            return s;%0A            %0A
         %7D fi

Check for BOM character when returning strings.