01: /*
02: * Copyright (C) 2007 XStream Committers.
03: * All rights reserved.
04: *
05: * The software in this package is published under the terms of the BSD
06: * style license a copy of which has been included with this distribution in
07: * the LICENSE.txt file.
08: *
09: * Created on 04. May 2007 by Joerg Schaible
10: */
11: package com.thoughtworks.xstream.benchmark.strings.products;
12:
13: import com.thoughtworks.xstream.XStream;
14: import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;
15: import com.thoughtworks.xstream.io.xml.XppDriver;
16: import com.thoughtworks.xstream.tools.benchmark.Product;
17:
18: import java.io.InputStream;
19: import java.io.OutputStream;
20:
21: /**
22: * Uses String.intern() for StringConverter (and allocates PermSpace).
23: *
24: * @author Jörg Schaible
25: * @see com.thoughtworks.xstream.tools.benchmark.Harness
26: * @see Product
27: */
28: public class StringInternConverter implements Product {
29:
30: private final XStream xstream;
31:
32: public StringInternConverter() {
33: xstream = new XStream(new XppDriver());
34: xstream.registerConverter(new StringConverter());
35: }
36:
37: public void serialize(Object object, OutputStream output)
38: throws Exception {
39: xstream.toXML(object, output);
40: }
41:
42: public Object deserialize(InputStream input) throws Exception {
43: return xstream.fromXML(input);
44: }
45:
46: public String toString() {
47: return "StringConverter using intern() allocating perm space";
48: }
49:
50: /**
51: * Converts a String to a String ;). Well ok, it doesn't
52: * <i>actually</i> do any conversion.
53: * <p>The converter always calls intern() on the returned
54: * String to encourage the JVM to reuse instances.</p>
55: *
56: * @author Joe Walnes
57: * @see String#intern()
58: */
59: public static class StringConverter extends
60: AbstractSingleValueConverter {
61:
62: public boolean canConvert(Class type) {
63: return type.equals(String.class);
64: }
65:
66: public Object fromString(String str) {
67: return str.intern();
68: }
69:
70: }
71:
72: }
|