Here you can find the source of getStringFromStreamSource(StreamSource src)
Parameter | Description |
---|---|
inputStream | the Reader to copy from. |
Parameter | Description |
---|---|
IOException | in case something bad happens. |
public static String getStringFromStreamSource(StreamSource src) throws IOException
//package com.java2s; /* (c) 2015 Open Source Geospatial Foundation - all rights reserved * (c) 2001 - 2015 OpenPlans//w w w . j a v a 2 s .c om * This code is licensed under the GPL 2.0 license, available at the root * application directory. */ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import javax.xml.transform.stream.StreamSource; public class Main { /** * Convert the input from the provided {@link Reader} into a {@link String}. * * @param inputStream the {@link Reader} to copy from. * @return a {@link String} that contains the content of the provided {@link Reader}. * @throws IOException in case something bad happens. */ public static String getStringFromStreamSource(StreamSource src) throws IOException { inputNotNull(src); InputStream inputStream = src.getInputStream(); if (inputStream != null) { return getStringFromStream(inputStream); } else { final Reader r = src.getReader(); return getStringFromReader(r); } } /** * Checks if the input is not null. * @param oList list of elements to check for null. */ private static void inputNotNull(Object... oList) { for (Object o : oList) if (o == null) throw new NullPointerException( "Input objects cannot be null"); } /** * Convert the input from the provided {@link InputStream} into a {@link String}. * * @param inputStream the {@link InputStream} to copy from. * @return a {@link String} that contains the content of the provided {@link InputStream}. * @throws IOException in case something bad happens. */ public static String getStringFromStream(InputStream inputStream) throws IOException { inputNotNull(inputStream); final Reader inReq = new InputStreamReader(inputStream); return getStringFromReader(inReq); } /** * Convert the input from the provided {@link Reader} into a {@link String}. * * @param inputStream the {@link Reader} to copy from. * @return a {@link String} that contains the content of the provided {@link Reader}. * @throws IOException in case something bad happens. */ public static String getStringFromReader(final Reader inputReader) throws IOException { inputNotNull(inputReader); final StringBuilder sb = new StringBuilder(); final char[] buffer = new char[1024]; int len; while ((len = inputReader.read(buffer)) >= 0) { char[] read = new char[len]; System.arraycopy(buffer, 0, read, 0, len); sb.append(read); } return sb.toString(); } }