Skip to content

Commit

Permalink
refactor EncodedWriter from StreamCharBuffer to grails-core as Encode…
Browse files Browse the repository at this point in the history
…dAppender
  • Loading branch information
lhotari committed Apr 4, 2013
1 parent 7115009 commit 12218d8
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 56 deletions.
Expand Up @@ -47,8 +47,12 @@ public void registerEncodedWith(Encoder encoder, CharSequence escaped) {

public boolean shouldEncodeWith(Encoder encoderToApply, CharSequence string) {
Set<Encoder> tags = getEncodersFor(string);
if(tags != null) {
for(Encoder encoder : tags) {
return shouldEncodeWith(encoderToApply, tags);
}

public static boolean shouldEncodeWith(Encoder encoderToApply, Set<Encoder> currentEncoders) {
if(currentEncoders != null) {
for(Encoder encoder : currentEncoders) {
if(isEncoderEquivalentToPrevious(encoderToApply, encoder)) {
return false;
}
Expand Down
@@ -0,0 +1,10 @@
package org.codehaus.groovy.grails.support.encoding;

import java.io.IOException;
import java.util.Set;

public interface EncodedAppender {
public void append(Encoder encoder, Set<Encoder> currentEncoders, String str, int off, int len) throws IOException ;
public void append(Encoder encoder, Set<Encoder> currentEncoders, char[] b, int off, int len) throws IOException ;
public void append(Encoder encoder, StreamEncodeable streamEncodeable) throws IOException ;
}
@@ -0,0 +1,7 @@
package org.codehaus.groovy.grails.support.encoding;

import java.io.IOException;

public interface StreamEncodeable {
public void encodeTo(EncodedAppender appender, Encoder encoder) throws IOException;
}
@@ -1,8 +1,6 @@
package org.codehaus.groovy.grails.support.encoding;

import java.io.IOException;
import java.io.Writer;

public interface StreamingEncoder extends Encoder {
public void encodeToWriter(Object source, Writer writer) throws IOException;
public void encodeToStream(Object source, EncodedAppender appender);
}
Expand Up @@ -14,14 +14,13 @@
*/
package org.codehaus.groovy.grails.plugins.codecs;

import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.codehaus.groovy.grails.support.encoding.CodecFactory;
import org.codehaus.groovy.grails.support.encoding.Decoder;
import org.codehaus.groovy.grails.support.encoding.EncodedAppender;
import org.codehaus.groovy.grails.support.encoding.Encoder;
import org.codehaus.groovy.grails.support.encoding.StreamingEncoder;
import org.springframework.web.util.HtmlUtils;
Expand Down Expand Up @@ -49,7 +48,7 @@ public void markEncoded(CharSequence string) {
// no need to implement, wrapped automaticly
}

public void encodeToWriter(Object source, Writer writer) throws IOException {
public void encodeToStream(Object source, EncodedAppender appender) {

}

Expand Down
Expand Up @@ -40,8 +40,10 @@
import org.codehaus.groovy.grails.commons.DefaultGrailsCodecClass;
import org.codehaus.groovy.grails.support.encoding.DefaultEncodingState;
import org.codehaus.groovy.grails.support.encoding.Encodeable;
import org.codehaus.groovy.grails.support.encoding.EncodedAppender;
import org.codehaus.groovy.grails.support.encoding.Encoder;
import org.codehaus.groovy.grails.support.encoding.EncodingState;
import org.codehaus.groovy.grails.support.encoding.StreamEncodeable;

/**
* <p>
Expand Down Expand Up @@ -225,7 +227,7 @@
*
* @author Lari Hotari, Sagire Software Oy
*/
public class StreamCharBuffer implements Writable, CharSequence, Externalizable, Encodeable {
public class StreamCharBuffer implements Writable, CharSequence, Externalizable, Encodeable, StreamEncodeable {
static final long serialVersionUID = 5486972234419632945L;
private static final Log log=LogFactory.getLog(StreamCharBuffer.class);

Expand Down Expand Up @@ -835,7 +837,7 @@ protected static final void arrayCopy(char[] src, int srcPos, char[] dest, int d
*
* @author Lari Hotari, Sagire Software Oy
*/
public final class StreamCharBufferWriter extends Writer implements EncoderWriter {
public final class StreamCharBufferWriter extends Writer implements EncodedAppender {
boolean closed = false;
int writerUsedCounter = 0;
boolean increaseCounter = true;
Expand All @@ -845,19 +847,31 @@ public final void write(final char[] b, final int off, final int len) throws IOE
write(null, b, off, len);
}

public void write(Encoder encoder, EncodingTags currentTags, char[] b, int off, int len) throws IOException {
public void append(Encoder encoder, Set<Encoder> currentEncoders, char[] b, int off, int len) throws IOException {
if(b==null || len <= 0) {
return;
}
if(encoder != null && (currentTags==null || currentTags.shouldEncodeWith(encoder))) {
EncodingTags newTags = (currentTags != null) ? currentTags.appendToNew(encoder) : EncodingTags.createNew(encoder);
if(encoder != null && (currentEncoders==null || DefaultEncodingState.shouldEncodeWith(encoder, currentEncoders))) {
Set<Encoder> newEncoders=appendEncoders(encoder, currentEncoders);
String encoded = String.valueOf(encoder.encode(String.valueOf(b, off, len)));
write(newTags, encoded, 0, encoded.length());
write(new EncodingTags(newEncoders), encoded, 0, encoded.length());
} else {
write(currentTags, b, off, len);
write(currentEncoders != null ? new EncodingTags(currentEncoders) : null, b, off, len);
}
}

private Set<Encoder> appendEncoders(Encoder encoder, Set<Encoder> currentEncoders) {
Set<Encoder> newEncoders;
if(currentEncoders==null) {
newEncoders=Collections.singleton(encoder);
} else {
newEncoders=new LinkedHashSet<Encoder>();
newEncoders.addAll(currentEncoders);
newEncoders.add(encoder);
}
return newEncoders;
}

private final void write(EncodingTags tags, final char[] b, final int off, final int len) throws IOException {
if (b == null) {
throw new NullPointerException();
Expand Down Expand Up @@ -950,12 +964,12 @@ private EncodingTags resolveTags(String str) {
return null;
}

public void write(Encoder encoder, EncodingTags currentTags, String str, int off, int len) throws IOException {
public void append(Encoder encoder, Set<Encoder> currentEncoders, String str, int off, int len) throws IOException {
if(str==null || len <= 0) {
return;
}
if(encoder != null && (currentTags==null || currentTags.shouldEncodeWith(encoder))) {
EncodingTags newTags = (currentTags != null) ? currentTags.appendToNew(encoder) : EncodingTags.createNew(encoder);
if(encoder != null && (currentEncoders==null || DefaultEncodingState.shouldEncodeWith(encoder, currentEncoders))) {
Set<Encoder> newEncoders=appendEncoders(encoder, currentEncoders);
String source;
if(off==0 && len==str.length()) {
source = str;
Expand All @@ -965,10 +979,10 @@ public void write(Encoder encoder, EncodingTags currentTags, String str, int off
source = String.valueOf(buff);
}
String encoded = String.valueOf(encoder.encode(source));
write(newTags, encoded, 0, encoded.length());
write(new EncodingTags(newEncoders), encoded, 0, encoded.length());
} else {
write(currentTags, str, off, len);
}
write(currentEncoders != null ? new EncodingTags(currentEncoders) : null, str, off, len);
}
}

private final void write(EncodingTags tags, final String str, final int off, final int len) throws IOException {
Expand Down Expand Up @@ -1112,8 +1126,8 @@ public final StreamCharBuffer getBuffer() {
return StreamCharBuffer.this;
}

public void write(Encoder encoder, StreamCharBuffer subBuffer) throws IOException {
subBuffer.encodeTo(this, encoder);
public void append(Encoder encoder, StreamEncodeable streamEncodeable) throws IOException {
streamEncodeable.encodeTo(this, encoder);
}
}

Expand Down Expand Up @@ -1276,7 +1290,7 @@ public int getReadLenLimit(int askedAmount) {
}
}

abstract class AbstractChunk {
abstract class AbstractChunk implements StreamEncodeable {
AbstractChunk next;
AbstractChunk prev;
int writerUsedCounter;
Expand Down Expand Up @@ -1309,8 +1323,12 @@ public EncodingTags getTags() {
public void setTags(EncodingTags tags) {
this.tags = tags;
}

public Set<Encoder> getEncoders() {
return (tags != null) ? tags.getEncoders() : null;
}

public abstract void encodeTo(EncoderWriter target, Encoder encoder) throws IOException;
public abstract void encodeTo(EncodedAppender appender, Encoder encoder) throws IOException;

}

Expand Down Expand Up @@ -1426,14 +1444,18 @@ public boolean hasChunk() {
public EncodingTags getTags() {
return tags;
}

public Set<Encoder> getEncoders() {
return (tags != null) ? tags.getEncoders() : null;
}

public void setTags(EncodingTags tags) {
this.tags = tags;
}

public void encodeTo(EncoderWriter target, Encoder encoder) throws IOException {
public void encodeTo(EncodedAppender appender, Encoder encoder) throws IOException {
if (used-chunkStart > 0) {
target.write(encoder, getTags(), buffer, chunkStart, used-chunkStart);
appender.append(encoder, getEncoders(), buffer, chunkStart, used-chunkStart);
}
}

Expand Down Expand Up @@ -1487,8 +1509,8 @@ public boolean isSingleBuffer() {
}

@Override
public void encodeTo(EncoderWriter target, Encoder encoder) throws IOException {
target.write(encoder, getTags(), buffer, offset, length);
public void encodeTo(EncodedAppender appender, Encoder encoder) throws IOException {
appender.append(encoder, getEncoders(), buffer, offset, length);
}
}

Expand Down Expand Up @@ -1606,8 +1628,8 @@ public boolean isSingleBuffer() {
}

@Override
public void encodeTo(EncoderWriter target, Encoder encoder) throws IOException {
target.write(encoder, getTags(), str, offset, length);
public void encodeTo(EncodedAppender appender, Encoder encoder) throws IOException {
appender.append(encoder, getEncoders(), str, offset, length);
}
}

Expand Down Expand Up @@ -1690,10 +1712,8 @@ public void subtractFromTotalCount() {
dynamicChunkMap.remove(streamCharBuffer.bufferKey);
}

@Override
public void encodeTo(EncoderWriter target, Encoder encoder) throws IOException {
target.write(encoder, getSubBuffer());

public void encodeTo(EncodedAppender appender, Encoder encoder) throws IOException {
appender.append(encoder, getSubBuffer());
}
}

Expand Down Expand Up @@ -2026,7 +2046,7 @@ public void writeExternal(ObjectOutput out) throws IOException {

public StreamCharBuffer encodeToBuffer(Encoder encoder) {
StreamCharBuffer coded = new StreamCharBuffer(Math.min(Math.max(totalChunkSize, chunkSize) * 12 / 10, maxChunkSize));
EncoderWriter codedWriter = (EncoderWriter)coded.getWriter();
EncodedAppender codedWriter = (EncodedAppender)coded.getWriter();
try {
encodeTo(codedWriter, encoder);
} catch (IOException e) {
Expand All @@ -2036,13 +2056,13 @@ public StreamCharBuffer encodeToBuffer(Encoder encoder) {
return coded;
}

public void encodeTo(EncoderWriter target, Encoder encoder) throws IOException {
public void encodeTo(EncodedAppender appender, Encoder encoder) throws IOException {
AbstractChunk current = firstChunk;
while (current != null) {
current.encodeTo(target, encoder);
current.encodeTo(appender, encoder);
current = current.next;
}
allocBuffer.encodeTo(target, encoder);
allocBuffer.encodeTo(appender, encoder);
}

public static final class EncodingTags {
Expand All @@ -2067,23 +2087,16 @@ public EncodingTags appendToNew(Encoder encoder) {
}
}

public boolean shouldEncodeWith(Encoder encoderToApply) {
if(encoders != null) {
for(Encoder encoder : encoders) {
if(DefaultEncodingState.isEncoderEquivalentToPrevious(encoderToApply, encoder)) {
return false;
}
}
}
return true;
}

public Encoder getFirstEncoder() {
if(encoders != null && encoders.size() > 0) {
return encoders.iterator().next();
}
return null;
}

public Set<Encoder> getEncoders() {
return encoders;
}

@Override
public int hashCode() {
Expand Down Expand Up @@ -2113,12 +2126,6 @@ else if (!encoders.equals(other.encoders))

}

public static interface EncoderWriter {
public void write(Encoder encoder, EncodingTags currentTags, char[] b, int off, int len) throws IOException;
public void write(Encoder encoder, EncodingTags currentTags, String str, int off, int len) throws IOException;
public void write(Encoder encoder, StreamCharBuffer subBuffer) throws IOException;
}

public CharSequence encode(Encoder encoder) {
return encodeToBuffer(encoder);
}
Expand Down

0 comments on commit 12218d8

Please sign in to comment.