| 1 | package ezvcard.util; |
| 2 | |
| 3 | import java.io.BufferedInputStream; |
| 4 | import java.io.ByteArrayOutputStream; |
| 5 | import java.io.IOException; |
| 6 | import java.io.InputStream; |
| 7 | import java.io.InputStreamReader; |
| 8 | import java.io.Reader; |
| 9 | import java.nio.charset.Charset; |
| 10 | import java.nio.file.Files; |
| 11 | import java.nio.file.Path; |
| 12 | |
| 13 | /* |
| 14 | Copyright (c) 2012-2023, Michael Angstadt |
| 15 | All rights reserved. |
| 16 | |
| 17 | Redistribution and use in source and binary forms, with or without |
| 18 | modification, are permitted provided that the following conditions are met: |
| 19 | |
| 20 | 1. Redistributions of source code must retain the above copyright notice, this |
| 21 | list of conditions and the following disclaimer. |
| 22 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 23 | this list of conditions and the following disclaimer in the documentation |
| 24 | and/or other materials provided with the distribution. |
| 25 | |
| 26 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 27 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 28 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 29 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 30 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 31 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 32 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 33 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 34 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 35 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 | |
| 37 | The views and conclusions contained in the software and documentation are those |
| 38 | of the authors and should not be interpreted as representing official policies, |
| 39 | either expressed or implied, of the FreeBSD Project. |
| 40 | */ |
| 41 | |
| 42 | /** |
| 43 | * Gets the entire contents of an input stream or a file. |
| 44 | * @author Michael Angstadt |
| 45 | */ |
| 46 | public class Gobble { |
| 47 | private final Path file; |
| 48 | private final InputStream in; |
| 49 | private final Reader reader; |
| 50 | |
| 51 | /** |
| 52 | * Gets the contents of a file. |
| 53 | * @param file the file |
| 54 | */ |
| 55 | public Gobble(Path file) { |
| 56 | this(file, null, null); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Gets the contents of an input stream. |
| 61 | * @param in the input stream |
| 62 | */ |
| 63 | public Gobble(InputStream in) { |
| 64 | this(null, in, null); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Gets the contents of a reader. |
| 69 | * @param reader the reader |
| 70 | */ |
| 71 | public Gobble(Reader reader) { |
| 72 | this(null, null, reader); |
| 73 | } |
| 74 | |
| 75 | private Gobble(Path file, InputStream in, Reader reader) { |
| 76 | this.file = file; |
| 77 | this.in = in; |
| 78 | this.reader = reader; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Gets the stream contents as a string. If something other than a |
| 83 | * {@link Reader} was passed into this class's constructor, this method |
| 84 | * decodes the stream data using the system's default character encoding. |
| 85 | * @return the string |
| 86 | * @throws IOException if there was a problem reading from the stream |
| 87 | */ |
| 88 | public String asString() throws IOException { |
| 89 | return asString(Charset.defaultCharset()); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Gets the stream contents as a string. |
| 94 | * @param charset the character set to decode the stream data with (this |
| 95 | * parameter is ignored if a {@link Reader} was passed into this class's |
| 96 | * constructor) |
| 97 | * @return the string |
| 98 | * @throws IOException if there was a problem reading from the stream |
| 99 | */ |
| 100 | public String asString(Charset charset) throws IOException { |
| 101 | Reader reader = buildReader(charset); |
| 102 | return consumeReader(reader); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Gets the stream contents as a byte array. |
| 107 | * @return the byte array |
| 108 | * @throws IOException if there was a problem reading from the stream |
| 109 | * @throws IllegalStateException if a {@link Reader} object was passed into |
| 110 | * this class's constructor |
| 111 | */ |
| 112 | public byte[] asByteArray() throws IOException { |
| 113 | if (reader != null) { |
| 114 | throw new IllegalStateException("Cannot get raw bytes from a Reader object."); |
| 115 | } |
| 116 | |
| 117 | InputStream in = buildInputStream(); |
| 118 | return consumeInputStream(in); |
| 119 | } |
| 120 | |
| 121 | private Reader buildReader(Charset charset) throws IOException { |
| 122 | return (reader == null) ? new InputStreamReader(buildInputStream(), charset) : reader; |
| 123 | } |
| 124 | |
| 125 | private InputStream buildInputStream() throws IOException { |
| 126 | return (in == null) ? new BufferedInputStream(Files.newInputStream(file)) : in; |
| 127 | } |
| 128 | |
| 129 | private String consumeReader(Reader reader) throws IOException { |
| 130 | StringBuilder sb = new StringBuilder(); |
| 131 | char[] buffer = new char[4096]; |
| 132 | int read; |
| 133 | try { |
| 134 | while ((read = reader.read(buffer)) != -1) { |
| 135 | sb.append(buffer, 0, read); |
| 136 | } |
| 137 | } finally { |
| 138 | reader.close(); |
| 139 | } |
| 140 | return sb.toString(); |
| 141 | } |
| 142 | |
| 143 | private byte[] consumeInputStream(InputStream in) throws IOException { |
| 144 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 145 | byte[] buffer = new byte[4096]; |
| 146 | int read; |
| 147 | try { |
| 148 | while ((read = in.read(buffer)) != -1) { |
| 149 | out.write(buffer, 0, read); |
| 150 | } |
| 151 | } finally { |
| 152 | in.close(); |
| 153 | } |
| 154 | return out.toByteArray(); |
| 155 | } |
| 156 | } |