add simplified ways to create XContentBuilder that will output to OutputStream using XContentFactory

This commit is contained in:
kimchy 2011-03-10 16:00:19 +02:00
parent 0df3dfd72b
commit e1c86b86e5
1 changed files with 27 additions and 0 deletions

View File

@ -27,6 +27,7 @@ import org.elasticsearch.common.xcontent.smile.SmileXContent;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
/**
@ -56,6 +57,13 @@ public class XContentFactory {
return contentBuilder(XContentType.JSON);
}
/**
* Constructs a new json builder that will output the result into the provided output stream.
*/
public static XContentBuilder jsonBuilder(OutputStream os) throws IOException {
return new XContentBuilder(JsonXContent.jsonXContent, os);
}
/**
* Returns a content builder using JSON format ({@link org.elasticsearch.common.xcontent.XContentType#JSON}
* that can be used outside of the scope of passing it directly to an API call.
@ -74,6 +82,13 @@ public class XContentFactory {
return contentBuilder(XContentType.SMILE);
}
/**
* Constructs a new json builder that will output the result into the provided output stream.
*/
public static XContentBuilder smileBuilder(OutputStream os) throws IOException {
return new XContentBuilder(SmileXContent.smileXContent, os);
}
/**
* Returns a content builder using SMILE format ({@link org.elasticsearch.common.xcontent.XContentType#SMILE}
* that can be used outside of the scope of passing it directly to an API call.
@ -82,6 +97,18 @@ public class XContentFactory {
return unCachedContentBuilder(XContentType.SMILE);
}
/**
* Constructs a xcontent builder that will output the result into the provided output stream.
*/
public static XContentBuilder contentBuilder(XContentType type, OutputStream outputStream) throws IOException {
if (type == XContentType.JSON) {
return jsonBuilder(outputStream);
} else if (type == XContentType.SMILE) {
return smileBuilder(outputStream);
}
throw new ElasticSearchIllegalArgumentException("No matching content type for " + type);
}
/**
* Returns a binary content builder for the provided content type.
*/