001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2009 Sun Microsystems Inc.
015 * Portions Copyright 2010–2011 ApexIdentity Inc.
016 * Portions Copyright 2011-2014 ForgeRock AS.
017 */
018
019package org.forgerock.openig.util;
020
021import java.io.BufferedReader;
022import java.io.IOException;
023import java.io.InputStream;
024import java.io.InputStreamReader;
025import java.nio.charset.Charset;
026import java.util.Arrays;
027import java.util.Iterator;
028
029/**
030 * Miscellaneous string utility methods.
031 */
032public final class StringUtil {
033
034    /** Platform specific end of line character. */
035    private static final String EOL = System.getProperty("line.separator");
036
037    /**
038     * Static methods only.
039     */
040    private StringUtil() { }
041
042    /**
043     * Joins a collection of elements into a single string value, with a specified separator.
044     *
045     * @param separator the separator to place between joined elements.
046     * @param elements the collection of strings to be joined.
047     * @return the string containing the joined elements.
048     */
049    public static String join(String separator, Iterable<?> elements) {
050        StringBuilder sb = new StringBuilder();
051        for (Iterator<?> i = elements.iterator(); i.hasNext();) {
052            sb.append(i.next().toString());
053            if (i.hasNext() && separator != null) {
054                sb.append(separator);
055            }
056        }
057        return sb.toString();
058    }
059
060    /**
061     * Joins an array of strings into a single string value, with a specified separator.
062     *
063     * @param separator the separator to place between joined elements.
064     * @param elements the array of strings to be joined.
065     * @return the string containing the joined string array.
066     */
067    public static String join(String separator, Object... elements) {
068        return join(separator, Arrays.asList(elements));
069    }
070
071    /**
072     * Reads the provided input stream as a string and then closes the stream.
073     *
074     * @param is
075     *            the input stream to be read.
076     * @param charset
077     *            the character set encoding of the input stream.
078     * @return the content of the stream.
079     * @throws IOException
080     *             If an I/O error occurs.
081     */
082    public static String asString(final InputStream is, Charset charset) throws IOException {
083        final BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset));
084        try {
085            final String firstLine = reader.readLine();
086            if (firstLine == null) {
087                return "";
088            }
089            final StringBuilder builder = new StringBuilder(firstLine);
090            for (String line = reader.readLine(); line != null; line = reader.readLine()) {
091                builder.append(EOL);
092                builder.append(line);
093            }
094            return builder.toString();
095        } finally {
096            reader.close();
097        }
098    }
099
100}