Java byte array to outputstream java

Java byte array to outputstream java

This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it. The data can be retrieved using toByteArray() and toString() . Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

Field Summary

Constructor Summary

Method Summary

Resets the count field of this byte array output stream to zero, so that all currently accumulated output in the output stream is discarded.

Converts the buffer’s contents into a string decoding bytes using the platform’s default character set.

This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the toString(String enc) method, which takes an encoding-name argument, or the toString() method, which uses the platform’s default character encoding.

Writes len bytes from the specified byte array starting at offset off to this byte array output stream.

Writes the complete contents of this byte array output stream to the specified output stream argument, as if by calling the output stream’s write method using out.write(buf, 0, count) .

Читайте также:  Css display inline block width auto

Methods inherited from class java.io.OutputStream

Methods inherited from class java.lang.Object

Field Detail

buf

count

Constructor Detail

ByteArrayOutputStream

public ByteArrayOutputStream()

Creates a new byte array output stream. The buffer capacity is initially 32 bytes, though its size increases if necessary.

ByteArrayOutputStream

public ByteArrayOutputStream(int size)

Method Detail

write

write

public void write(byte[] b, int off, int len)

Writes len bytes from the specified byte array starting at offset off to this byte array output stream.

writeTo

Writes the complete contents of this byte array output stream to the specified output stream argument, as if by calling the output stream’s write method using out.write(buf, 0, count) .

reset

Resets the count field of this byte array output stream to zero, so that all currently accumulated output in the output stream is discarded. The output stream can be used again, reusing the already allocated buffer space.

toByteArray

Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.

size

toString

Converts the buffer’s contents into a string decoding bytes using the platform’s default character set. The length of the new String is a function of the character set, and hence may not be equal to the size of the buffer. This method always replaces malformed-input and unmappable-character sequences with the default replacement string for the platform’s default character set. The CharsetDecoder class should be used when more control over the decoding process is required.

toString

public String toString(String charsetName) throws UnsupportedEncodingException

Converts the buffer’s contents into a string by decoding the bytes using the named charset . The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array. This method always replaces malformed-input and unmappable-character sequences with this charset’s default replacement string. The CharsetDecoder class should be used when more control over the decoding process is required.

toString

@Deprecated public String toString(int hibyte)

Deprecated. This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the toString(String enc) method, which takes an encoding-name argument, or the toString() method, which uses the platform’s default character encoding.

Creates a newly allocated string. Its size is the current size of the output stream and the valid contents of the buffer have been copied into it. Each character c in the resulting string is constructed from the corresponding element b in the byte array such that:

close

Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.

Источник

Class ByteArrayOutputStream

This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it. The data can be retrieved using toByteArray() and toString() .

Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException .

Field Summary

Constructor Summary

Method Summary

Resets the count field of this ByteArrayOutputStream to zero, so that all currently accumulated output in the output stream is discarded.

Writes len bytes from the specified byte array starting at offset off to this ByteArrayOutputStream .

Writes the complete contents of this ByteArrayOutputStream to the specified output stream argument, as if by calling the output stream’s write method using out.write(buf, 0, count) .

Methods declared in class java.io.OutputStream

Methods declared in class java.lang.Object

Field Details

buf

count

Constructor Details

ByteArrayOutputStream

Creates a new ByteArrayOutputStream . The buffer capacity is initially 32 bytes, though its size increases if necessary.

ByteArrayOutputStream

Method Details

write

write

Writes len bytes from the specified byte array starting at offset off to this ByteArrayOutputStream .

writeBytes

writeTo

Writes the complete contents of this ByteArrayOutputStream to the specified output stream argument, as if by calling the output stream’s write method using out.write(buf, 0, count) .

reset

Resets the count field of this ByteArrayOutputStream to zero, so that all currently accumulated output in the output stream is discarded. The output stream can be used again, reusing the already allocated buffer space.

toByteArray

Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.

size

toString

Converts the buffer’s contents into a string decoding bytes using the default charset. The length of the new String is a function of the charset, and hence may not be equal to the size of the buffer. This method always replaces malformed-input and unmappable-character sequences with the default replacement string for the default charset. The CharsetDecoder class should be used when more control over the decoding process is required.

toString

Converts the buffer’s contents into a string by decoding the bytes using the named charset . This method is equivalent to #toString(charset) that takes a charset . An invocation of this method of the form

 ByteArrayOutputStream b = . b.toString("UTF-8") 
 ByteArrayOutputStream b = . b.toString(StandardCharsets.UTF_8) 

toString

Converts the buffer’s contents into a string by decoding the bytes using the specified charset . The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array. This method always replaces malformed-input and unmappable-character sequences with the charset’s default replacement string. The CharsetDecoder class should be used when more control over the decoding process is required.

toString

This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the toString(String charsetName) or toString(Charset charset) method, which takes an encoding-name or charset argument, or the toString() method, which uses the default charset.

Creates a newly allocated string. Its size is the current size of the output stream and the valid contents of the buffer have been copied into it. Each character c in the resulting string is constructed from the corresponding element b in the byte array such that:

close

Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException .

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Оцените статью