@@ -99,9 +99,9 @@ public void append(Encoder encoder, EncodingState encodingState, char[] b, int o
9999 return ;
100100 }
101101 if (shouldEncode (encoder , encodingState )) {
102- EncodingState newEncoders = appendEncoders (encoder , encodingState );
102+ EncodingState newEncoders = createNewEncodingState (encoder , encodingState );
103103 if (encoder instanceof StreamingEncoder ) {
104- ((StreamingEncoder )encoder ).encodeToStream (encoder , new CharArrayCharSequence (b , off , len ), 0 , len , this ,
104+ ((StreamingEncoder )encoder ).encodeToStream (encoder , CharSequences . createCharSequence (b , off , len ), 0 , len , this ,
105105 newEncoders );
106106 }
107107 else {
@@ -113,7 +113,7 @@ public void append(Encoder encoder, EncodingState encodingState, char[] b, int o
113113 }
114114 }
115115
116- private EncodingState appendEncoders (Encoder encoder , EncodingState encodingState ) {
116+ protected EncodingState createNewEncodingState (Encoder encoder , EncodingState encodingState ) {
117117 if (encodingState == null ) {
118118 return new EncodingStateImpl (encoder );
119119 }
@@ -134,13 +134,13 @@ public void append(Encoder encoder, EncodingState encodingState, CharSequence st
134134 return ;
135135 }
136136 if (shouldEncode (encoder , encodingState )) {
137- EncodingState newEncoders = appendEncoders (encoder , encodingState );
137+ EncodingState newEncoders = createNewEncodingState (encoder , encodingState );
138138 if (encoder instanceof StreamingEncoder ) {
139139 ((StreamingEncoder )encoder ).encodeToStream (encoder , str , off , len , this , newEncoders );
140140 }
141141 else {
142142 CharSequence source ;
143- if (off == 0 && len == str . length ( )) {
143+ if (CharSequences . canUseOriginalForSubSequence ( str , off , len )) {
144144 source = str ;
145145 }
146146 else {
@@ -156,12 +156,12 @@ public void append(Encoder encoder, EncodingState encodingState, CharSequence st
156156
157157 public void appendEncoded (Encoder encoder , EncodingState encodingState , char [] b , int off , int len )
158158 throws IOException {
159- write (appendEncoders (encoder , encodingState ), b , off , len );
159+ write (createNewEncodingState (encoder , encodingState ), b , off , len );
160160 }
161161
162162 public void appendEncoded (Encoder encoder , EncodingState encodingState , CharSequence str , int off , int len )
163163 throws IOException {
164- appendCharSequence (appendEncoders (encoder , encodingState ), str , off , off + len );
164+ appendCharSequence (createNewEncodingState (encoder , encodingState ), str , off , off + len );
165165 }
166166
167167 /**
@@ -175,8 +175,12 @@ public void appendEncoded(Encoder encoder, EncodingState encodingState, CharSequ
175175 */
176176 protected boolean shouldEncode (Encoder encoderToApply , EncodingState encodingState ) {
177177 return ignoreEncodingState || (encoderToApply != null
178- && (encodingState == null || DefaultEncodingStateRegistry .shouldEncodeWith (encoderToApply ,
179- encodingState )));
178+ && (encodingState == null || shouldEncodeWith (encoderToApply , encodingState )));
179+ }
180+
181+ protected boolean shouldEncodeWith (Encoder encoderToApply , EncodingState encodingState ) {
182+ return DefaultEncodingStateRegistry .shouldEncodeWith (encoderToApply ,
183+ encodingState );
180184 }
181185
182186 /**
@@ -219,62 +223,6 @@ public void flush() throws IOException {
219223
220224 }
221225
222- static final class CharArrayCharSequence implements CharSequence , CharArrayAccessible {
223- private final char [] chars ;
224- private final int count ;
225- private final int start ;
226-
227- public CharArrayCharSequence (char [] chars ) {
228- this (chars , 0 , chars .length );
229- }
230-
231- public CharArrayCharSequence (char [] chars , int start , int count ) {
232- if (start + count > chars .length )
233- throw new StringIndexOutOfBoundsException (start );
234- this .chars = chars ;
235- this .start = start ;
236- this .count = count ;
237- }
238-
239- public char charAt (int index ) {
240- if ((index < 0 ) || (index + start >= chars .length ))
241- throw new StringIndexOutOfBoundsException (index );
242- return chars [index + start ];
243- }
244-
245- public int length () {
246- return count ;
247- }
248-
249- public CharSequence subSequence (int start , int end ) {
250- if (start < 0 )
251- throw new StringIndexOutOfBoundsException (start );
252- if (end > count )
253- throw new StringIndexOutOfBoundsException (end );
254- if (start > end )
255- throw new StringIndexOutOfBoundsException (end - start );
256- if (start == 0 && end == count ) {
257- return this ;
258- }
259- return new CharArrayCharSequence (chars , this .start + start , end - start );
260- }
261-
262- @ Override
263- public String toString () {
264- return new String (chars , start , count );
265- }
266-
267- public void getChars (int srcBegin , int srcEnd , char [] dst , int dstBegin ) {
268- if (srcBegin < 0 )
269- throw new StringIndexOutOfBoundsException (srcBegin );
270- if ((srcEnd < 0 ) || (srcEnd > start +count ))
271- throw new StringIndexOutOfBoundsException (srcEnd );
272- if (srcBegin > srcEnd )
273- throw new StringIndexOutOfBoundsException ("srcBegin > srcEnd" );
274- System .arraycopy (chars , start + srcBegin , dst , dstBegin , srcEnd - srcBegin );
275- }
276- }
277-
278226 public boolean isIgnoreEncodingState () {
279227 return ignoreEncodingState ;
280228 }
0 commit comments