add an option to provide an OutputStream to a builder, allowing to generate directly into a stream provided by the user

This commit is contained in:
kimchy 2010-12-22 13:26:29 +02:00
parent eef3a95fa6
commit 1ee24ce60f
4 changed files with 53 additions and 18 deletions

View File

@ -30,6 +30,7 @@ import org.elasticsearch.common.xcontent.support.XContentMapConverter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;
@ -64,21 +65,31 @@ public final class XContentBuilder {
private XContentGenerator generator;
private final FastByteArrayOutputStream bos;
private final OutputStream bos;
private FieldCaseConversion fieldCaseConversion = globalFieldCaseConversion;
private StringBuilder cachedStringBuilder;
/**
* Constructs a new cached builder over a cached (thread local) {@link FastByteArrayOutputStream}.
*/
public static XContentBuilder cachedBuilder(XContent xContent) throws IOException {
return new XContentBuilder(FastByteArrayOutputStream.Cached.cached(), xContent);
return new XContentBuilder(xContent, FastByteArrayOutputStream.Cached.cached());
}
/**
* Constructs a new builder using a fresh {@link FastByteArrayOutputStream}.
*/
public static XContentBuilder builder(XContent xContent) throws IOException {
return new XContentBuilder(new FastByteArrayOutputStream(), xContent);
return new XContentBuilder(xContent, new FastByteArrayOutputStream());
}
public XContentBuilder(FastByteArrayOutputStream bos, XContent xContent) throws IOException {
/**
* Constructs a new builder using the provided xcontent and an OutputStream. Make sure
* to call {@link #close()} when the builder is done with.
*/
public XContentBuilder(XContent xContent, OutputStream bos) throws IOException {
this.bos = bos;
this.generator = xContent.createGenerator(bos);
}
@ -801,28 +812,53 @@ public final class XContentBuilder {
}
}
/**
* Returns the unsafe bytes (thread local bound). Make sure to use it with
* {@link #unsafeBytesLength()}.
*
* <p>Only applicable when the builder is constructed with {@link FastByteArrayOutputStream}.
*/
public byte[] unsafeBytes() throws IOException {
close();
return bos.unsafeByteArray();
return ((FastByteArrayOutputStream) bos).unsafeByteArray();
}
/**
* Returns the unsafe bytes length (thread local bound). Make sure to use it with
* {@link #unsafeBytes()}.
*
* <p>Only applicable when the builder is constructed with {@link FastByteArrayOutputStream}.
*/
public int unsafeBytesLength() throws IOException {
close();
return bos.size();
return ((FastByteArrayOutputStream) bos).size();
}
/**
* Returns the actual stream used.
*/
public FastByteArrayOutputStream unsafeStream() throws IOException {
close();
return bos;
return (FastByteArrayOutputStream) bos;
}
/**
* Returns a copy of the bytes this builder generated.
*
* <p>Only applicable when the builder is constructed with {@link FastByteArrayOutputStream}.
*/
public byte[] copiedBytes() throws IOException {
close();
return bos.copiedByteArray();
return ((FastByteArrayOutputStream) bos).copiedByteArray();
}
/**
* Returns a string representation of the builder (only applicable for text based xcontent).
*
* <p>Only applicable when the builder is constructed with {@link FastByteArrayOutputStream}.
*/
public String string() throws IOException {
close();
return Unicode.fromBytes(bos.unsafeByteArray(), 0, bos.size());
return Unicode.fromBytes(unsafeBytes(), 0, unsafeBytesLength());
}
}

View File

@ -19,10 +19,9 @@
package org.elasticsearch.common.xcontent;
import org.elasticsearch.common.io.FastByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @author kimchy (shay.banon)
@ -106,9 +105,9 @@ public interface XContentGenerator {
void writeObjectFieldStart(XContentString fieldName) throws IOException;
void writeRawField(String fieldName, byte[] content, FastByteArrayOutputStream bos) throws IOException;
void writeRawField(String fieldName, byte[] content, OutputStream bos) throws IOException;
void writeRawField(String fieldName, InputStream content, FastByteArrayOutputStream bos) throws IOException;
void writeRawField(String fieldName, InputStream content, OutputStream bos) throws IOException;
void copyCurrentStructure(XContentParser parser) throws IOException;

View File

@ -20,13 +20,13 @@
package org.elasticsearch.common.xcontent.json;
import org.elasticsearch.common.Bytes;
import org.elasticsearch.common.io.FastByteArrayOutputStream;
import org.elasticsearch.common.io.Streams;
import org.elasticsearch.common.jackson.JsonGenerator;
import org.elasticsearch.common.xcontent.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @author kimchy (shay.banon)
@ -201,7 +201,7 @@ public class JsonXContentGenerator implements XContentGenerator {
generator.writeStartObject();
}
@Override public void writeRawField(String fieldName, byte[] content, FastByteArrayOutputStream bos) throws IOException {
@Override public void writeRawField(String fieldName, byte[] content, OutputStream bos) throws IOException {
generator.writeRaw(", \"");
generator.writeRaw(fieldName);
generator.writeRaw("\" : ");
@ -209,7 +209,7 @@ public class JsonXContentGenerator implements XContentGenerator {
bos.write(content);
}
@Override public void writeRawField(String fieldName, InputStream content, FastByteArrayOutputStream bos) throws IOException {
@Override public void writeRawField(String fieldName, InputStream content, OutputStream bos) throws IOException {
generator.writeRaw(", \"");
generator.writeRaw(fieldName);
generator.writeRaw("\" : ");

View File

@ -19,13 +19,13 @@
package org.elasticsearch.common.xcontent.smile;
import org.elasticsearch.common.io.FastByteArrayOutputStream;
import org.elasticsearch.common.jackson.JsonGenerator;
import org.elasticsearch.common.jackson.smile.SmileParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContentGenerator;
import java.io.IOException;
import java.io.OutputStream;
/**
* @author kimchy (shay.banon)
@ -40,7 +40,7 @@ public class SmileXContentGenerator extends JsonXContentGenerator {
return XContentType.SMILE;
}
@Override public void writeRawField(String fieldName, byte[] content, FastByteArrayOutputStream bos) throws IOException {
@Override public void writeRawField(String fieldName, byte[] content, OutputStream bos) throws IOException {
writeFieldName(fieldName);
SmileParser parser = SmileXContent.smileFactory.createJsonParser(content);
try {