Skip to content

Commit 562da33

Browse files
author
Kannan Goundan
committed
Use Charset.newDecoder() so that we error on invalid UTF-8.
1 parent 348000d commit 562da33

4 files changed

Lines changed: 39 additions & 9 deletions

File tree

examples/web-file-browser/src/com/dropbox/core/examples/web_file_browser/DropboxBrowse.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.dropbox.core.examples.web_file_browser;
22

33
import static com.dropbox.core.util.StringUtil.jq;
4-
import static com.dropbox.core.util.StringUtil.UTF8;
54

65
import com.dropbox.core.DbxException;
76
import com.dropbox.core.DbxRequestUtil;
87
import com.dropbox.core.util.IOUtil;
8+
import com.dropbox.core.util.StringUtil;
99
import com.dropbox.core.v2.DbxClientV2;
1010
import com.dropbox.core.v2.DbxPathV2;
1111
import com.dropbox.core.v2.Files;
@@ -108,7 +108,7 @@ private void renderFolder(HttpServletResponse response, User user, DbxClientV2 d
108108

109109
response.setContentType("text/html");
110110
response.setCharacterEncoding("utf-8");
111-
PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), UTF8));
111+
PrintWriter out = new PrintWriter(IOUtil.utf8Writer(response.getOutputStream()));
112112

113113
out.println("<html>");
114114
out.println("<head><title>" + escapeHtml4(path) + "- Web File Browser</title></head>");
@@ -145,7 +145,7 @@ private void renderFile(HttpServletResponse response, String path, Files.FileMet
145145

146146
response.setContentType("text/html");
147147
response.setCharacterEncoding("utf-8");
148-
PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), UTF8));
148+
PrintWriter out = new PrintWriter(IOUtil.utf8Writer(response.getOutputStream()));
149149

150150
out.println("<html>");
151151
out.println("<head><title>" + escapeHtml4(path) + "- Web File Browser</title></head>");
@@ -312,7 +312,7 @@ private static String slurpUtf8Part(HttpServletRequest request, HttpServletRespo
312312
byte[] bytes = new byte[maxLength];
313313
InputStream in = part.getInputStream();
314314
int bytesRead = in.read(bytes);
315-
String s = new String(bytes, 0, bytesRead, UTF8);
315+
String s = StringUtil.utf8ToString(bytes, 0, bytesRead);
316316
if (in.read() != -1) {
317317
response.sendError(400, "Field " + jq(name) + " is too long (the limit is " + maxLength + " bytes): " + jq(s));
318318
return null;

src/com/dropbox/core/DbxSdkVersion.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.io.BufferedReader;
77
import java.io.IOException;
88
import java.io.InputStream;
9-
import java.io.InputStreamReader;
109
import java.util.regex.Pattern;
1110

1211
/*>>> import checkers.nullness.quals.Nullable; */
@@ -35,7 +34,7 @@ private static String loadLineFromResource()
3534
InputStream in = DbxSdkVersion.class.getResourceAsStream(ResourceName);
3635
if (in == null) throw new LoadException("Not found.");
3736
try {
38-
BufferedReader bin = new BufferedReader(new InputStreamReader(in, StringUtil.UTF8));
37+
BufferedReader bin = new BufferedReader(IOUtil.utf8Reader(in));
3938
String version = bin.readLine();
4039
if (version == null) throw new LoadException("No lines.");
4140
String secondLine = bin.readLine();

src/com/dropbox/core/util/IOUtil.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
11
package com.dropbox.core.util;
22

3-
import java.io.*;
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.File;
5+
import java.io.FileInputStream;
6+
import java.io.FileOutputStream;
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.io.InputStreamReader;
10+
import java.io.OutputStream;
11+
import java.io.OutputStreamWriter;
12+
import java.io.Reader;
13+
import java.io.Writer;
414

515
public class IOUtil
616
{
717
public static final int DefaultCopyBufferSize = 16 * 1024;
818

19+
public static Reader utf8Reader(InputStream in)
20+
{
21+
// NOTE: Just passing StringUtil.UTF8 instead of StringUtil.UTF8.newDecoder() would be wrong.
22+
// The former will cause the InputStreamReader to ignore UTF-8 errors in the input.
23+
return new InputStreamReader(in, StringUtil.UTF8.newDecoder());
24+
}
25+
26+
public static Writer utf8Writer(OutputStream out)
27+
{
28+
return new OutputStreamWriter(out, StringUtil.UTF8.newEncoder());
29+
}
30+
931
public static void copyStreamToStream(InputStream in, OutputStream out)
1032
throws ReadException, WriteException
1133
{

src/com/dropbox/core/util/StringUtil.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.nio.charset.CharacterCodingException;
99
import java.nio.charset.Charset;
1010
import java.nio.charset.CharsetDecoder;
11+
import java.nio.charset.CharsetEncoder;
1112

1213
public class StringUtil
1314
{
@@ -16,11 +17,19 @@ public class StringUtil
1617
private static final char[] HexDigits = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f',};
1718
public static char hexDigit(int i) { return HexDigits[i]; }
1819

19-
public static String utf8ToString(byte[] utf8data)
20+
public static String utf8ToString(byte[] utf8Data)
2021
throws CharacterCodingException
2122
{
23+
return utf8ToString(utf8Data, 0, utf8Data.length);
24+
}
25+
26+
public static String utf8ToString(byte[] utf8Data, int offset, int length)
27+
throws CharacterCodingException
28+
{
29+
// NOTE: Using the String(..., UTF8) constructor would be wrong. That method will
30+
// ignore UTF-8 errors in the input.
2231
CharsetDecoder decoder = UTF8.newDecoder();
23-
CharBuffer result = decoder.decode(ByteBuffer.wrap(utf8data));
32+
CharBuffer result = decoder.decode(ByteBuffer.wrap(utf8Data, offset, length));
2433
return result.toString();
2534
}
2635

0 commit comments

Comments
 (0)