54 |
static final int BIG_ENDIAN = 0; |
static final int BIG_ENDIAN = 0; |
55 |
static final int LITTLE_ENDIAN = 1; |
static final int LITTLE_ENDIAN = 1; |
56 |
static final int UNKNOWN_ENDIAN = 2; |
static final int UNKNOWN_ENDIAN = 2; |
57 |
|
static final int MAYBE_BIG_ENDIAN = 3; |
58 |
|
static final int MAYBE_LITTLE_ENDIAN = 4; |
59 |
|
|
60 |
private static final char BYTE_ORDER_MARK = 0xFEFF; |
private static final char BYTE_ORDER_MARK = 0xFEFF; |
61 |
private static final char REVERSED_BYTE_ORDER_MARK = 0xFFFE; |
private static final char REVERSED_BYTE_ORDER_MARK = 0xFFFE; |
83 |
byte b2 = in.get (); |
byte b2 = in.get (); |
84 |
|
|
85 |
// handle byte order mark |
// handle byte order mark |
86 |
if (byteOrder == UNKNOWN_ENDIAN) |
if (byteOrder == UNKNOWN_ENDIAN || |
87 |
|
byteOrder == MAYBE_BIG_ENDIAN || |
88 |
|
byteOrder == MAYBE_LITTLE_ENDIAN) |
89 |
{ |
{ |
90 |
char c = (char) (((b1 & 0xFF) << 8) | (b2 & 0xFF)); |
char c = (char) (((b1 & 0xFF) << 8) | (b2 & 0xFF)); |
91 |
if (c == BYTE_ORDER_MARK) |
if (c == BYTE_ORDER_MARK) |
92 |
{ |
{ |
93 |
|
if (byteOrder == MAYBE_LITTLE_ENDIAN) |
94 |
|
{ |
95 |
|
return CoderResult.malformedForLength (2); |
96 |
|
} |
97 |
byteOrder = BIG_ENDIAN; |
byteOrder = BIG_ENDIAN; |
98 |
inPos += 2; |
inPos += 2; |
99 |
continue; |
continue; |
100 |
} |
} |
101 |
else if (c == REVERSED_BYTE_ORDER_MARK) |
else if (c == REVERSED_BYTE_ORDER_MARK) |
102 |
{ |
{ |
103 |
|
if (byteOrder == MAYBE_BIG_ENDIAN) |
104 |
|
{ |
105 |
|
return CoderResult.malformedForLength (2); |
106 |
|
} |
107 |
byteOrder = LITTLE_ENDIAN; |
byteOrder = LITTLE_ENDIAN; |
108 |
inPos += 2; |
inPos += 2; |
109 |
continue; |
continue; |
110 |
} |
} |
111 |
else |
else |
112 |
{ |
{ |
113 |
// assume big endian, do not consume bytes, |
// assume big or little endian, do not consume bytes, |
114 |
// continue with normal processing |
// continue with normal processing |
115 |
byteOrder = BIG_ENDIAN; |
byteOrder = (byteOrder == MAYBE_LITTLE_ENDIAN ? |
116 |
|
LITTLE_ENDIAN : BIG_ENDIAN); |
117 |
} |
} |
118 |
} |
} |
119 |
|
|