Skip to content

Commit 3a8b7dc

Browse files
committed
Remove BOM from strings in StringFromFile
1 parent ec583dc commit 3a8b7dc

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/CommunityCommons/javasource/communitycommons/StringUtils.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import javax.swing.text.html.parser.ParserDelegator;
3737

3838
import org.apache.commons.io.IOUtils;
39+
import org.apache.commons.io.input.BOMInputStream;
3940
import org.apache.commons.text.StringEscapeUtils;
4041
import org.owasp.html.PolicyFactory;
4142
import org.owasp.html.Sanitizers;
@@ -222,10 +223,14 @@ public static String stringFromFile(IContext context, FileDocument source, Chars
222223
return null;
223224
}
224225
try (InputStream f = Core.getFileDocumentContent(context, source.getMendixObject())) {
225-
return IOUtils.toString(f, charset);
226+
return stringFromInputStream(f, charset);
226227
}
227228
}
228229

230+
public static String stringFromInputStream(InputStream inputStream, Charset charset) throws IOException {
231+
return IOUtils.toString(BOMInputStream.builder().setInputStream(inputStream).get(), charset);
232+
}
233+
229234
public static void stringToFile(IContext context, String value, FileDocument destination) throws IOException {
230235
stringToFile(context, value, destination, StandardCharsets.UTF_8);
231236
}

src/test/communitycommons/StringUtilsTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
import org.owasp.html.PolicyFactory;
1515
import org.owasp.html.Sanitizers;
1616

17+
import java.io.ByteArrayInputStream;
18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
import java.nio.charset.Charset;
1721
import java.security.DigestException;
1822
import java.security.NoSuchAlgorithmException;
1923

@@ -170,4 +174,32 @@ public void testHash() throws DigestException, NoSuchAlgorithmException {
170174
assertEquals(StringUtils.hash(originalString), StringUtils.hash(originalString, length));
171175
assertEquals(StringUtils.hash(originalString), hashedString);
172176
}
177+
178+
@Test
179+
public void testStringFromInputStream() throws IOException {
180+
Charset utf8 = Charset.forName("UTF-8");
181+
Charset utf16 = Charset.forName("UTF-16");
182+
Charset utf16be = Charset.forName("UTF-16BE");
183+
Charset utf16le = Charset.forName("UTF-16LE");
184+
185+
String text = "hello";
186+
187+
assertEquals(text, testStringFromInputStream(text, utf8));
188+
assertEquals(text, testStringFromInputStream(text, utf16));
189+
assertEquals(text, testStringFromInputStream(text, utf16be));
190+
assertEquals(text, testStringFromInputStream(text, utf16le));
191+
192+
// BOM should be removed (UTF-8)
193+
byte[] UTF8BOM = { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF };
194+
String textUTF8BOM = new String(UTF8BOM) + text;
195+
assertEquals(text, testStringFromInputStream(textUTF8BOM, utf8));
196+
}
197+
198+
private String testStringFromInputStream(String text, Charset charset) throws IOException {
199+
return StringUtils.stringFromInputStream(stringToInputStream(text, charset), charset);
200+
}
201+
202+
private InputStream stringToInputStream(String str, Charset charset) {
203+
return new ByteArrayInputStream(str.getBytes(charset));
204+
}
173205
}

0 commit comments

Comments
 (0)