01: // Jericho HTML Parser - Java based library for analysing and manipulating HTML
02: // Version 2.5
03: // Copyright (C) 2007 Martin Jericho
04: // http://jerichohtml.sourceforge.net/
05: //
06: // This library is free software; you can redistribute it and/or
07: // modify it under the terms of either one of the following licences:
08: //
09: // 1. The Eclipse Public License (EPL) version 1.0,
10: // included in this distribution in the file licence-epl-1.0.html
11: // or available at http://www.eclipse.org/legal/epl-v10.html
12: //
13: // 2. The GNU Lesser General Public License (LGPL) version 2.1 or later,
14: // included in this distribution in the file licence-lgpl-2.1.txt
15: // or available at http://www.gnu.org/licenses/lgpl.txt
16: //
17: // This library is distributed on an "AS IS" basis,
18: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
19: // See the individual licence texts for more details.
20:
21: package au.id.jericho.lib.html;
22:
23: import java.io.*;
24: import java.nio.*;
25:
26: /**
27: * Contains static utility methods for manipulating the way data is retrieved from a {@link CharStreamSource} object.
28: * <p>
29: * See the documentation of the {@link CharStreamSource} class for details.
30: */
31: public final class CharStreamSourceUtil {
32: private static final int DEFAULT_ESTIMATED_MAXIMUM_OUTPUT_LENGTH = 2048;
33:
34: private CharStreamSourceUtil() {
35: }
36:
37: /**
38: * Returns a <code>Reader</code> that reads the output of the specified {@link CharStreamSource}.
39: * <p>
40: * The current implementation of this method simply returns <code>new StringReader(</code>{@link #toString(CharStreamSource) toString(charStreamSource)}<code>)</code>,
41: * but a future version may implement this method in a more memory efficient manner.
42: *
43: * @param charStreamSource the character stream source producing the output.
44: * @return a <code>Reader</code> that reads the output of the specified {@link CharStreamSource}.
45: */
46: public static Reader getReader(
47: final CharStreamSource charStreamSource) {
48: return new StringReader(toString(charStreamSource));
49: }
50:
51: /**
52: * Returns the output of the specified {@link CharStreamSource} as a string.
53: * <p>
54: * The current implementation of this method simply returns <code>new StringReader(</code>{@link #toString(CharStreamSource) toString(charStreamSource)}<code>)</code>,
55: * but a future version may implement this method in a more memory efficient manner, for example by utilising a temporary file.
56: *
57: * @param charStreamSource the character stream source producing the output.
58: * @return the output of the specified {@link CharStreamSource} as a string.
59: */
60: public static String toString(
61: final CharStreamSource charStreamSource) {
62: long estimatedMaximumOutputLength = charStreamSource
63: .getEstimatedMaximumOutputLength();
64: if (estimatedMaximumOutputLength == -1L)
65: estimatedMaximumOutputLength = DEFAULT_ESTIMATED_MAXIMUM_OUTPUT_LENGTH;
66: final StringWriter writer = new StringWriter(
67: (int) (estimatedMaximumOutputLength));
68: try {
69: charStreamSource.writeTo(writer);
70: } catch (IOException ex) {
71: throw new RuntimeException(ex);
72: } // assume the IOException is not thrown explicitly by the charStreamSource.output method
73: return writer.toString();
74: }
75: }
|