@@ -109,8 +109,10 @@ long getBytesRead() {
109109
110110 private long getEncodedCharLength (final char [] buf , final int offset , final int length ) throws CharacterCodingException {
111111 int len = 0 ;
112- for (int i = offset ; i < length ; i ++) {
113- len += getEncodedCharLength (buf [i ]);
112+ int previous = lastChar ;
113+ for (int i = offset ; i < offset + length ; i ++) {
114+ len += getEncodedCharLength (buf [i ], previous );
115+ previous = buf [i ];
114116 }
115117 return len ;
116118 }
@@ -140,9 +142,9 @@ private long getEncodedCharLength(final char[] buf, final int offset, final int
140142 * @return the byte length of the character.
141143 * @throws CharacterCodingException if the character cannot be encoded.
142144 */
143- private int getEncodedCharLength (final int current ) throws CharacterCodingException {
145+ private int getEncodedCharLength (final int current , final int previous ) throws CharacterCodingException {
144146 final char cChar = (char ) current ;
145- final char lChar = (char ) lastChar ;
147+ final char lChar = (char ) previous ;
146148 if (!Character .isSurrogate (cChar )) {
147149 return encoder .encode (CharBuffer .wrap (new char [] { cChar })).limit ();
148150 }
@@ -205,7 +207,7 @@ public int read() throws IOException {
205207 lineNumber ++;
206208 }
207209 if (encoder != null ) {
208- this .bytesRead += getEncodedCharLength (current );
210+ this .bytesRead += getEncodedCharLength (current , lastChar );
209211 }
210212 lastChar = current ;
211213 position ++;
@@ -229,13 +231,13 @@ public int read(final char[] buf, final int offset, final int length) throws IOE
229231 lineNumber ++;
230232 }
231233 }
234+ if (encoder != null ) {
235+ this .bytesRead += getEncodedCharLength (buf , offset , len );
236+ }
232237 lastChar = buf [offset + len - 1 ];
233238 } else if (len == EOF ) {
234239 lastChar = EOF ;
235240 }
236- if (encoder != null ) {
237- this .bytesRead += getEncodedCharLength (buf , offset , len );
238- }
239241 position += len ;
240242 return len ;
241243 }
0 commit comments