001/*
002 * nimbus-jose-jwt
003 *
004 * Copyright 2012-2016, Connect2id Ltd and contributors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.jose.util;
019
020
021import java.io.*;
022import java.nio.charset.Charset;
023
024
025/**
026 * Input / output utilities.
027 *
028 * @author Vladimir Dzhuvinov
029 * @version 2020-02-23
030 */
031public class IOUtils {
032        
033        
034        /**
035         * Reads the specified input stream into a string using UTF-8 character
036         * set encoding.
037         *
038         * @param stream The input stream. Must not be {@code null}.
039         *
040         * @return The string.
041         *
042         * @throws IOException If an input exception is encountered.
043         */
044        public static String readInputStreamToString(final InputStream stream)
045                throws IOException {
046                
047                return readInputStreamToString(stream, StandardCharset.UTF_8);
048        }
049        
050        
051        /**
052         * Reads the specified input stream into a string.
053         *
054         * @param stream  The input stream. Must not be {@code null}.
055         * @param charset The expected character set. Must not be {@code null}.
056         *
057         * @return The string.
058         *
059         * @throws IOException If an input exception is encountered.
060         */
061        public static String readInputStreamToString(final InputStream stream, final Charset charset)
062                throws IOException {
063                
064                final int bufferSize = 1024;
065                final char[] buffer = new char[bufferSize];
066                final StringBuilder out = new StringBuilder();
067                
068                Reader in = new InputStreamReader(stream, charset);
069                try {
070                        while (true) {
071                                int rsz = in.read(buffer, 0, buffer.length);
072                                if (rsz < 0)
073                                        break;
074                                out.append(buffer, 0, rsz);
075                        }
076                        return out.toString();
077                } finally {
078                        in.close();
079                }
080        }
081        
082        
083        /**
084         * Reads the content of the specified file into a string using UTF-8
085         * character set encoding.
086         *
087         * @param file The file. Must not be {@code null}.
088         *
089         * @return The string.
090         *
091         * @throws IOException If an input exception is encountered.
092         */
093        public static String readFileToString(final File file)
094                throws IOException {
095                
096                return readInputStreamToString(new FileInputStream(file));
097        }
098        
099        
100        /**
101         * Reads the content of the specified file into a string.
102         *
103         * @param file    The file. Must not be {@code null}.
104         * @param charset The expected character set. Must not be {@code null}.
105         *
106         * @return The string.
107         *
108         * @throws IOException If an input exception is encountered.
109         */
110        public static String readFileToString(final File file, final Charset charset)
111                throws IOException {
112                
113                return readInputStreamToString(new FileInputStream(file), charset);
114        }
115        
116        
117        /**
118         * Prevents public instantiation.
119         */
120        private IOUtils() {}
121}