- Convert byte array to String in Java example
- How to convert byte array to string?
- How to decode UTF-8 byte array to String?
- Class Base64.Decoder
- Method Summary
- Methods declared in class java.lang.Object
- Method Details
- decode
- decode
- decode
- decode
- wrap
- Decode byte array java
- Method Summary
- Methods inherited from class java.lang.Object
- Method Detail
- decode
- decode
- decode
- decode
- wrap
- Decode byte array java
- Method Summary
- Methods declared in class java.lang.Object
- Method Detail
- decode
- decode
- decode
- decode
- wrap
Convert byte array to String in Java example
Convert byte array to String in Java example shows how to convert byte array to String using the constructor of the String class and UTF-8 character set.
How to convert byte array to string?
You can use a String constructor that accepts the byte array argument to convert the byte array to the string.
This constructor decodes the specified byte array using the default character set of the platform and returns the String object.
Important Note:
If the bytes contained in the byte array cannot be decoded using the platform’s default character set, the behavior of this constructor is unspecified. Please also note that the string’s length may not be equal to the size of an array depending upon the character set used to encode the byte array.
How to decode UTF-8 byte array to String?
As given in the above example, the string constructor decodes the byte array using the default character set of the platform. It may produce undesirable results if the bytes are not valid for the default character set. Use String constructor which accepts a character set parameter while converting byte array to String to avoid that.
This constructor decodes the byte array to string using the character set specified. The length of the string may not be equal to the size of the array depending upon the character set specified. Additionally, this constructor replaces malformed characters and characters which cannot be mapped in the target character set with the default replacement string.
Class Base64.Decoder
This class implements a decoder for decoding byte data using the Base64 encoding scheme as specified in RFC 4648 and RFC 2045.
The Base64 padding character ‘=’ is accepted and interpreted as the end of the encoded byte data, but is not required. So if the final unit of the encoded byte data only has two or three Base64 characters (without the corresponding padding character(s) padded), they are decoded as if followed by padding character(s). If there is a padding character present in the final unit, the correct number of padding character(s) must be present, otherwise IllegalArgumentException ( IOException when reading from a Base64 stream) is thrown during decoding.
Instances of Base64.Decoder class are safe for use by multiple concurrent threads.
Unless otherwise noted, passing a null argument to a method of this class will cause a NullPointerException to be thrown.
If the decoded byte output of the needed size can not be allocated, the decode methods of this class will cause an OutOfMemoryError to be thrown.
Method Summary
Decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into a newly-allocated output byte array.
Decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into the given output byte array, starting at offset 0.
Decodes all bytes from the input byte buffer using the Base64 encoding scheme, writing the results into a newly-allocated ByteBuffer.
Methods declared in class java.lang.Object
Method Details
decode
Decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into a newly-allocated output byte array. The returned byte array is of the length of the resulting bytes.
decode
Decodes a Base64 encoded String into a newly-allocated byte array using the Base64 encoding scheme. An invocation of this method has exactly the same effect as invoking decode(src.getBytes(StandardCharsets.ISO_8859_1))
decode
Decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into the given output byte array, starting at offset 0. It is the responsibility of the invoker of this method to make sure the output byte array dst has enough space for decoding all bytes from the input byte array. No bytes will be written to the output byte array if the output byte array is not big enough. If the input byte array is not in valid Base64 encoding scheme then some bytes may have been written to the output byte array before IllegalargumentException is thrown.
decode
Decodes all bytes from the input byte buffer using the Base64 encoding scheme, writing the results into a newly-allocated ByteBuffer. Upon return, the source buffer’s position will be updated to its limit; its limit will not have been changed. The returned output buffer’s position will be zero and its limit will be the number of resulting decoded bytes IllegalArgumentException is thrown if the input buffer is not in valid Base64 encoding scheme. The position of the input buffer will not be advanced in this case.
wrap
Returns an input stream for decoding Base64 encoded byte stream. The read methods of the returned InputStream will throw IOException when reading bytes that cannot be decoded. Closing the returned input stream will close the underlying input stream.
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.
Decode byte array java
This class implements a decoder for decoding byte data using the Base64 encoding scheme as specified in RFC 4648 and RFC 2045. The Base64 padding character ‘=’ is accepted and interpreted as the end of the encoded byte data, but is not required. So if the final unit of the encoded byte data only has two or three Base64 characters (without the corresponding padding character(s) padded), they are decoded as if followed by padding character(s). If there is a padding character present in the final unit, the correct number of padding character(s) must be present, otherwise IllegalArgumentException ( IOException when reading from a Base64 stream) is thrown during decoding. Instances of Base64.Decoder class are safe for use by multiple concurrent threads. Unless otherwise noted, passing a null argument to a method of this class will cause a NullPointerException to be thrown.
Method Summary
Decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into a newly-allocated output byte array.
Decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into the given output byte array, starting at offset 0.
Decodes all bytes from the input byte buffer using the Base64 encoding scheme, writing the results into a newly-allocated ByteBuffer.
Methods inherited from class java.lang.Object
Method Detail
decode
public byte[] decode(byte[] src)
Decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into a newly-allocated output byte array. The returned byte array is of the length of the resulting bytes.
decode
Decodes a Base64 encoded String into a newly-allocated byte array using the Base64 encoding scheme. An invocation of this method has exactly the same effect as invoking decode(src.getBytes(StandardCharsets.ISO_8859_1))
decode
public int decode(byte[] src, byte[] dst)
Decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into the given output byte array, starting at offset 0. It is the responsibility of the invoker of this method to make sure the output byte array dst has enough space for decoding all bytes from the input byte array. No bytes will be be written to the output byte array if the output byte array is not big enough. If the input byte array is not in valid Base64 encoding scheme then some bytes may have been written to the output byte array before IllegalargumentException is thrown.
decode
Decodes all bytes from the input byte buffer using the Base64 encoding scheme, writing the results into a newly-allocated ByteBuffer. Upon return, the source buffer’s position will be updated to its limit; its limit will not have been changed. The returned output buffer’s position will be zero and its limit will be the number of resulting decoded bytes IllegalArgumentException is thrown if the input buffer is not in valid Base64 encoding scheme. The position of the input buffer will not be advanced in this case.
wrap
Returns an input stream for decoding Base64 encoded byte stream. The read methods of the returned InputStream will throw IOException when reading bytes that cannot be decoded. Closing the returned input stream will close the underlying input stream.
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.
Decode byte array java
This class implements a decoder for decoding byte data using the Base64 encoding scheme as specified in RFC 4648 and RFC 2045. The Base64 padding character ‘=’ is accepted and interpreted as the end of the encoded byte data, but is not required. So if the final unit of the encoded byte data only has two or three Base64 characters (without the corresponding padding character(s) padded), they are decoded as if followed by padding character(s). If there is a padding character present in the final unit, the correct number of padding character(s) must be present, otherwise IllegalArgumentException ( IOException when reading from a Base64 stream) is thrown during decoding. Instances of Base64.Decoder class are safe for use by multiple concurrent threads. Unless otherwise noted, passing a null argument to a method of this class will cause a NullPointerException to be thrown.
Method Summary
Decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into a newly-allocated output byte array.
Decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into the given output byte array, starting at offset 0.
Decodes all bytes from the input byte buffer using the Base64 encoding scheme, writing the results into a newly-allocated ByteBuffer.
Methods declared in class java.lang.Object
Method Detail
decode
public byte[] decode(byte[] src)
Decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into a newly-allocated output byte array. The returned byte array is of the length of the resulting bytes.
decode
Decodes a Base64 encoded String into a newly-allocated byte array using the Base64 encoding scheme. An invocation of this method has exactly the same effect as invoking decode(src.getBytes(StandardCharsets.ISO_8859_1))
decode
public int decode(byte[] src, byte[] dst)
Decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into the given output byte array, starting at offset 0. It is the responsibility of the invoker of this method to make sure the output byte array dst has enough space for decoding all bytes from the input byte array. No bytes will be written to the output byte array if the output byte array is not big enough. If the input byte array is not in valid Base64 encoding scheme then some bytes may have been written to the output byte array before IllegalargumentException is thrown.
decode
Decodes all bytes from the input byte buffer using the Base64 encoding scheme, writing the results into a newly-allocated ByteBuffer. Upon return, the source buffer’s position will be updated to its limit; its limit will not have been changed. The returned output buffer’s position will be zero and its limit will be the number of resulting decoded bytes IllegalArgumentException is thrown if the input buffer is not in valid Base64 encoding scheme. The position of the input buffer will not be advanced in this case.
wrap
Returns an input stream for decoding Base64 encoded byte stream. The read methods of the returned InputStream will throw IOException when reading bytes that cannot be decoded. Closing the returned input stream will close the underlying input stream.
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.
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.