1 /*
2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package util;
24 
25 import java.io.BufferedInputStream;
26 import java.io.ByteArrayInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.InputStreamReader;
30 import java.io.UnsupportedEncodingException;
31 import java.util.HashMap;
32 import java.util.Map;
33 
34 public class BOMInputStream {
createStream(String charset, InputStream input)35         public static InputStream createStream(String charset, InputStream input) {
36 
37                         try {
38                                 byte[] content = read(input).getBytes(charset);
39                                 byte[] head = bom.get(charset);
40                                 if (head == null)
41                                         return null;
42                                 byte[] result = new byte[head.length + content.length];
43                                 System.arraycopy(head, 0, result, 0, head.length);
44                                 System.arraycopy(content, 0, result, head.length, content.length);
45                                 return new ByteArrayInputStream(result);
46                         } catch (UnsupportedEncodingException e) {
47                                 return null;
48                         }
49         }
50 
read(InputStream input)51         private static String read(InputStream input)
52         {
53                 try {
54                         StringBuffer sb = new StringBuffer();
55                         InputStreamReader r = new InputStreamReader(new BufferedInputStream(input));
56                         int c = 0;
57                         while ((c = r.read()) != -1)
58                                 sb.append((char)c);
59                         return sb.toString();
60                 } catch (IOException e) {
61                         return "";
62                 } finally {
63                         try {
64                                 input.close();
65                         } catch (IOException e)
66                         {}
67                 }
68         }
69 
70 
71         private final static Map<String, byte[]> bom = new HashMap();
72         private final static byte[][] bomBytes = {{(byte)0xEF, (byte)0xBB, (byte)0xBF},
73                                         {(byte)0xFE, (byte)0xFF}};
74 
75         static {
76                 bom.put("UTF-8", bomBytes[0]);
77                 bom.put("UTF-16BE", bomBytes[1]);
78         }
79 
80 }
81