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                try (Reader in = new InputStreamReader(stream, charset)) {
069                        while (true) {
070                                int rsz = in.read(buffer, 0, buffer.length);
071                                if (rsz < 0)
072                                        break;
073                                out.append(buffer, 0, rsz);
074                        }
075                        return out.toString();
076                }
077        }
078        
079        
080        /**
081         * Reads the content of the specified file into a string using UTF-8
082         * character set encoding.
083         *
084         * @param file The file. Must not be {@code null}.
085         *
086         * @return The string.
087         *
088         * @throws IOException If an input exception is encountered.
089         */
090        public static String readFileToString(final File file)
091                throws IOException {
092                
093                return readInputStreamToString(new FileInputStream(file));
094        }
095        
096        
097        /**
098         * Reads the content of the specified file into a string.
099         *
100         * @param file    The file. Must not be {@code null}.
101         * @param charset The expected character set. Must not be {@code null}.
102         *
103         * @return The string.
104         *
105         * @throws IOException If an input exception is encountered.
106         */
107        public static String readFileToString(final File file, final Charset charset)
108                throws IOException {
109                
110                return readInputStreamToString(new FileInputStream(file), charset);
111        }
112        
113        
114        /**
115         * Closes a {@linkplain Closeable} without throwing an
116         * {@linkplain IOException}.
117         * 
118         * @param closeable The closeable.
119         */
120        public static void closeSilently(final Closeable closeable) {
121                try {
122                        closeable.close();
123                } catch (IOException e) {
124                        // ignore
125                }
126        }
127        
128        
129        /**
130         * Prevents public instantiation.
131         */
132        private IOUtils() {}
133}