|
| 1 | +/* |
| 2 | + * Protocol-Attacker - A Framework to create Protocol Analysis Tools |
| 3 | + * |
| 4 | + * Copyright 2023-2025 Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH |
| 5 | + * |
| 6 | + * Licensed under Apache License, Version 2.0 |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0.txt |
| 8 | + */ |
| 9 | +package de.rub.nds.protocol.util; |
| 10 | + |
| 11 | +import java.io.ByteArrayOutputStream; |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.OutputStream; |
| 14 | +import java.io.UnsupportedEncodingException; |
| 15 | +import java.nio.charset.Charset; |
| 16 | +import org.apache.logging.log4j.LogManager; |
| 17 | +import org.apache.logging.log4j.Logger; |
| 18 | + |
| 19 | +/** |
| 20 | + * A wrapper around {@link ByteArrayOutputStream} that suppresses IOExceptions in most operations |
| 21 | + * and logs them silently. |
| 22 | + */ |
| 23 | +public class SilentByteArrayOutputStream extends OutputStream { |
| 24 | + |
| 25 | + private static final Logger LOGGER = LogManager.getLogger(); |
| 26 | + private final ByteArrayOutputStream outputStream; |
| 27 | + |
| 28 | + /** |
| 29 | + * Creates a new {@code SilentByteArrayOutputStream}. The buffer capacity is initially 32 bytes, |
| 30 | + * though its size increases if necessary. |
| 31 | + */ |
| 32 | + public SilentByteArrayOutputStream() { |
| 33 | + this.outputStream = new ByteArrayOutputStream(32); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Creates a new {@code ByteArrayOutputStream}, with a buffer capacity of the specified size, in |
| 38 | + * bytes. |
| 39 | + * |
| 40 | + * @param size the initial size. |
| 41 | + * @throws IllegalArgumentException if size is negative. |
| 42 | + */ |
| 43 | + public SilentByteArrayOutputStream(int size) { |
| 44 | + this.outputStream = new ByteArrayOutputStream(size); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Writes the specified byte to this {@code SilentByteArrayOutputStream}. |
| 49 | + * |
| 50 | + * @param b the byte to be written. |
| 51 | + */ |
| 52 | + @Override |
| 53 | + public void write(int b) { |
| 54 | + this.outputStream.write(b); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Writes {@code b.length} bytes from the specified byte array to this output stream. The |
| 59 | + * general contract for {@code write(b)} is that it should have exactly the same effect as the |
| 60 | + * call {@code write(b, 0, b.length)}. |
| 61 | + * |
| 62 | + * <p>This method does not throw IOException, it logs instead. |
| 63 | + * |
| 64 | + * @param b the data. |
| 65 | + * @see java.io.OutputStream#write(byte[], int, int) |
| 66 | + */ |
| 67 | + @Override |
| 68 | + public void write(byte[] b) { |
| 69 | + try { |
| 70 | + this.outputStream.write(b); |
| 71 | + } catch (IOException ex) { |
| 72 | + LOGGER.warn("Encountered exception while writing to ByteArrayOutputStream."); |
| 73 | + LOGGER.debug(ex); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Writes {@code len} bytes from the specified byte array starting at offset {@code off} to this |
| 79 | + * {@code SilentByteArrayOutputStream}. |
| 80 | + * |
| 81 | + * @param b {@inheritDoc} |
| 82 | + * @param off {@inheritDoc} |
| 83 | + * @param len {@inheritDoc} |
| 84 | + * @throws NullPointerException if {@code b} is {@code null}. |
| 85 | + * @throws IndexOutOfBoundsException if {@code off} is negative, {@code len} is negative, or |
| 86 | + * {@code len} is greater than {@code b.length - off} |
| 87 | + */ |
| 88 | + @Override |
| 89 | + public void write(byte[] b, int off, int len) { |
| 90 | + this.outputStream.write(b, off, len); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Writes the complete contents of the specified byte array to this {@code |
| 95 | + * SilentByteArrayOutputStream}. |
| 96 | + * |
| 97 | + * <p>This method is equivalent to {@link #write(byte[],int,int) write(b, 0, b.length)}. |
| 98 | + * |
| 99 | + * @param b the data. |
| 100 | + * @throws NullPointerException if {@code b} is {@code null}. |
| 101 | + * @since 11 |
| 102 | + */ |
| 103 | + public void writeBytes(byte[] b) { |
| 104 | + write(b, 0, b.length); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Writes the complete contents of this {@code SilentByteArrayOutputStream} to the specified |
| 109 | + * output stream argument, as if by calling the output stream's write method using {@code |
| 110 | + * out.write(buf, 0, count)}. |
| 111 | + * |
| 112 | + * <p>This method does not throw IOException, it logs instead. |
| 113 | + * |
| 114 | + * @param out the output stream to which to write the data. |
| 115 | + * @throws NullPointerException if {@code out} is {@code null}. |
| 116 | + */ |
| 117 | + public void writeTo(OutputStream out) { |
| 118 | + try { |
| 119 | + this.outputStream.writeTo(out); |
| 120 | + } catch (IOException ex) { |
| 121 | + LOGGER.error("Encountered exception while writing to OutputStream.", ex); |
| 122 | + throw new RuntimeException(ex); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Resets the {@code count} field of this {@code SilentByteArrayOutputStream} to zero, so that |
| 128 | + * all currently accumulated output in the output stream is discarded. The output stream can be |
| 129 | + * used again, reusing the already allocated buffer space. |
| 130 | + */ |
| 131 | + public void reset() { |
| 132 | + this.outputStream.reset(); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Creates a newly allocated byte array. Its size is the current size of this output stream and |
| 137 | + * the valid contents of the buffer have been copied into it. |
| 138 | + * |
| 139 | + * @return the current contents of this output stream, as a byte array. |
| 140 | + * @see java.io.ByteArrayOutputStream#size() |
| 141 | + */ |
| 142 | + public byte[] toByteArray() { |
| 143 | + return this.outputStream.toByteArray(); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Returns the current size of the buffer. |
| 148 | + * |
| 149 | + * @return the value of the {@code count} field, which is the number of valid bytes in this |
| 150 | + * output stream. |
| 151 | + */ |
| 152 | + public int size() { |
| 153 | + return this.outputStream.size(); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Converts the buffer's contents into a string decoding bytes using the default charset. The |
| 158 | + * length of the new {@code String} is a function of the charset, and hence may not be equal to |
| 159 | + * the size of the buffer. |
| 160 | + * |
| 161 | + * <p>This method always replaces malformed-input and unmappable-character sequences with the |
| 162 | + * default replacement string for the default charset. The {@linkplain |
| 163 | + * java.nio.charset.CharsetDecoder} class should be used when more control over the decoding |
| 164 | + * process is required. |
| 165 | + * |
| 166 | + * @see Charset#defaultCharset() |
| 167 | + * @return String decoded from the buffer's contents. |
| 168 | + * @since 1.1 |
| 169 | + */ |
| 170 | + @Override |
| 171 | + public String toString() { |
| 172 | + return this.outputStream.toString(); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Converts the buffer's contents into a string by decoding the bytes using the named {@link |
| 177 | + * Charset charset}. |
| 178 | + * |
| 179 | + * <p>This method is equivalent to {@code #toString(charset)} that takes a {@link Charset |
| 180 | + * charset}. |
| 181 | + * |
| 182 | + * <p>An invocation of this method of the form |
| 183 | + * |
| 184 | + * {@snippet lang=java : |
| 185 | + * ByteArrayOutputStream b; |
| 186 | + * b.toString("UTF-8") |
| 187 | + * } |
| 188 | + * |
| 189 | + * behaves in exactly the same way as the expression |
| 190 | + * |
| 191 | + * {@snippet lang=java : |
| 192 | + * ByteArrayOutputStream b; |
| 193 | + * b.toString(StandardCharsets.UTF_8) |
| 194 | + * } |
| 195 | + * |
| 196 | + * @param charsetName the name of a supported {@link Charset charset} |
| 197 | + * @return String decoded from the buffer's contents. |
| 198 | + * @throws IllegalArgumentException If the named charset is not supported |
| 199 | + * @since 1.1 |
| 200 | + */ |
| 201 | + public String toString(String charsetName) { |
| 202 | + try { |
| 203 | + return this.outputStream.toString(charsetName); |
| 204 | + } catch (UnsupportedEncodingException ex) { |
| 205 | + LOGGER.warn("Unsupported encoding: {}", charsetName); |
| 206 | + LOGGER.debug(ex); |
| 207 | + throw new IllegalArgumentException("Unsupported encoding: " + charsetName, ex); |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Converts the buffer's contents into a string by decoding the bytes using the specified {@link |
| 213 | + * Charset charset}. The length of the new {@code String} is a function of the charset, and |
| 214 | + * hence may not be equal to the length of the byte array. |
| 215 | + * |
| 216 | + * <p>This method always replaces malformed-input and unmappable-character sequences with the |
| 217 | + * charset's default replacement string. The {@link java.nio.charset.CharsetDecoder} class |
| 218 | + * should be used when more control over the decoding process is required. |
| 219 | + * |
| 220 | + * @param charset the {@linkplain Charset charset} to be used to decode the {@code bytes} |
| 221 | + * @return String decoded from the buffer's contents. |
| 222 | + * @since 10 |
| 223 | + */ |
| 224 | + public String toString(Charset charset) { |
| 225 | + return this.outputStream.toString(charset); |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * Creates a newly allocated string. Its size is the current size of the output stream and the |
| 230 | + * valid contents of the buffer have been copied into it. Each character <i>c</i> in the |
| 231 | + * resulting string is constructed from the corresponding element <i>b</i> in the byte array |
| 232 | + * such that: |
| 233 | + * |
| 234 | + * {@snippet lang=java : |
| 235 | + * c == (char)(((hibyte & 0xff) << 8) | (b & 0xff)) |
| 236 | + * } |
| 237 | + * |
| 238 | + * @deprecated This method does not properly convert bytes into characters. As of JDK 1.1, |
| 239 | + * the preferred way to do this is via the {@link #toString(String charsetName)} or {@link |
| 240 | + * #toString(Charset charset)} method, which takes an encoding-name or charset argument, or |
| 241 | + * the {@code toString()} method, which uses the default charset. |
| 242 | + * @param hibyte the high byte of each resulting Unicode character. |
| 243 | + * @return the current contents of the output stream, as a string. |
| 244 | + * @see java.io.ByteArrayOutputStream#size() |
| 245 | + * @see java.io.ByteArrayOutputStream#toString(String) |
| 246 | + * @see java.io.ByteArrayOutputStream#toString() |
| 247 | + * @see Charset#defaultCharset() |
| 248 | + */ |
| 249 | + @Deprecated |
| 250 | + public String toString(int hibyte) { |
| 251 | + return this.outputStream.toString(hibyte); |
| 252 | + } |
| 253 | + |
| 254 | + /** |
| 255 | + * Closing a {@code SilentByteArrayOutputStream} has no effect. The methods in this class can be |
| 256 | + * called after the stream has been closed without generating an {@code IOException}. |
| 257 | + */ |
| 258 | + @Override |
| 259 | + public void close() {} |
| 260 | +} |
0 commit comments