Skip to content

Commit bc2242e

Browse files
committed
GRAILS-11513 improve previous fix: use optimized way for writing CharSequences to Writer
1 parent 1756bcc commit bc2242e

File tree

2 files changed

+108
-2
lines changed

2 files changed

+108
-2
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.codehaus.groovy.grails.support.encoding;
17+
18+
import spock.lang.Specification
19+
20+
class CharSequencesSpec extends Specification {
21+
def "should support writing String instance to writer"() {
22+
given:
23+
StringWriter writer=new StringWriter()
24+
when:
25+
CharSequences.writeCharSequence(writer, input)
26+
then:
27+
writer.toString()==input
28+
where:
29+
input << ['Hello world','','1','12','123']
30+
}
31+
32+
def "should support writing StringBuilder instance to writer"() {
33+
given:
34+
StringWriter writer=new StringWriter()
35+
when:
36+
StringBuilder sb=new StringBuilder()
37+
sb.append(input)
38+
CharSequences.writeCharSequence(writer, sb)
39+
then:
40+
writer.toString()==input
41+
where:
42+
input << ['Hello world','','1','12','123']
43+
}
44+
45+
def "should support writing StringBuffer instance to writer"() {
46+
given:
47+
StringWriter writer=new StringWriter()
48+
when:
49+
StringBuffer sb=new StringBuffer()
50+
sb.append(input)
51+
CharSequences.writeCharSequence(writer, sb)
52+
then:
53+
writer.toString()==input
54+
where:
55+
input << ['Hello world','','1','12','123']
56+
}
57+
58+
def "should support writing CharArrayAccessible instance to writer"() {
59+
given:
60+
StringWriter writer=new StringWriter()
61+
when:
62+
CharArrayAccessible charArrayAccessible = new CharArrayCharSequence(input.toCharArray(), 0, input.length())
63+
CharSequences.writeCharSequence(writer, charArrayAccessible)
64+
then:
65+
writer.toString()==input
66+
where:
67+
input << ['Hello world','','1','12','123']
68+
}
69+
70+
def "should support writing CharSequence instance to writer"() {
71+
given:
72+
StringWriter writer=new StringWriter()
73+
when:
74+
CharSequence charSequence = new CustomCharSequence(input)
75+
CharSequences.writeCharSequence(writer, charSequence)
76+
then:
77+
writer.toString()==input
78+
where:
79+
input << ['Hello world','','1','12','123']
80+
}
81+
82+
class CustomCharSequence implements CharSequence {
83+
String source
84+
85+
CustomCharSequence(String source) {
86+
this.source = source
87+
}
88+
89+
90+
@Override
91+
public int length() {
92+
return source.length();
93+
}
94+
95+
@Override
96+
public char charAt(int index) {
97+
return source.charAt(index);
98+
}
99+
100+
@Override
101+
public CharSequence subSequence(int start, int end) {
102+
return source.subSequence(start, end);
103+
}
104+
}
105+
}

grails-plugin-codecs/src/main/groovy/org/codehaus/groovy/grails/plugins/codecs/AbstractCharReplacementEncoder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.IOException;
1919
import java.io.Writer;
2020

21+
import org.codehaus.groovy.grails.support.encoding.CharSequences;
2122
import org.codehaus.groovy.grails.support.encoding.CodecIdentifier;
2223
import org.codehaus.groovy.grails.support.encoding.EncodedAppender;
2324
import org.codehaus.groovy.grails.support.encoding.Encoder;
@@ -142,7 +143,7 @@ public void encodeToWriter(CharSequence str, int off, int len, Writer writer, En
142143
String escaped = escapeCharacter(ch, prevChar);
143144
if (escaped != null) {
144145
if (i - startPos > 0) {
145-
writer.append(str, startPos, i);
146+
CharSequences.writeCharSequence(writer, str, startPos, i);
146147
}
147148
if (escaped.length() > 0) {
148149
writer.write(escaped);
@@ -152,7 +153,7 @@ public void encodeToWriter(CharSequence str, int off, int len, Writer writer, En
152153
prevChar = ch;
153154
}
154155
if (startPos > -1 && i - startPos > 0) {
155-
writer.append(str, startPos, i);
156+
CharSequences.writeCharSequence(writer, str, startPos, i);
156157
}
157158
}
158159

0 commit comments

Comments
 (0)