Read first bytes as ints to respect method contract

This commit is contained in:
Simon Willnauer 2015-03-20 17:08:20 -07:00
parent 865cbeb3d8
commit 5e5f00de08
1 changed files with 8 additions and 5 deletions

View File

@ -208,14 +208,17 @@ public class XContentFactory {
* Guesses the content type based on the provided input stream. * Guesses the content type based on the provided input stream.
*/ */
public static XContentType xContentType(InputStream si) throws IOException { public static XContentType xContentType(InputStream si) throws IOException {
byte first = (byte) si.read(); final int firstInt = si.read(); // this must be an int since we need to respect the method contract
if (first == -1) { if (firstInt == -1) {
return null; return null;
} }
byte second = (byte) si.read();
if (second == -1) { final int secondInt = si.read(); // this must be an int since we need to respect the method contract
return null; if (secondInt == -1) {
return null;
} }
final byte first = (byte) (0xff & firstInt);
final byte second = (byte) (0xff & secondInt);
if (first == SmileConstants.HEADER_BYTE_1 && second == SmileConstants.HEADER_BYTE_2) { if (first == SmileConstants.HEADER_BYTE_1 && second == SmileConstants.HEADER_BYTE_2) {
int third = si.read(); int third = si.read();
if (third == SmileConstants.HEADER_BYTE_3) { if (third == SmileConstants.HEADER_BYTE_3) {