I noticed that one of the implementations of Serializer<T> is the curiously
named PlainTextSerializer, which apparently returns a Document! I'm trying
to imagine what that would be (I couldn't find any (ahem) documentation of
Document other than the rather unhelpful javadoc), but meanwhile, what if I
really did want to read a web result as plain text? Am I supposed to
use ByteArraySerializer and then convert it to a String? Not particularly
hard, I suppose -- in fact, it's trivial to write my own StringSerializer
that uses ByteArraySerializer internally. I was just wondering why I should
be the one to do that :).
public class StringSerializer implements Serializer<String> {
private String encoding = "UTF8";
public StringSerializer() {
}
public StringSerializer(String encoding) {
this.encoding = encoding;
}
@Override
public String getMIMEType(String object) {
return "text/plain";
}
@Override
public String readObject(InputStream inputStream)
throws IOException, SerializationException {
byte[] bytes = new ByteArraySerializer().readObject(inputStream);
return new String(bytes, encoding);
}
@Override
public void writeObject(String object, OutputStream outputStream)
throws IOException, SerializationException {
byte[] bytes = object.getBytes(encoding);
new ByteArraySerializer().writeObject(bytes, outputStream);
}
}