How do I convert a String to an InputStream in Java?
Like this:
InputStream stream = new ByteArrayInputStream(exampleString.getBytes(StandardCharsets.UTF_8));
Note that this assumes that you want an InputStream that is a stream of bytes that represent your original string encoded as UTF-8.
For versions of Java less than 7, replace StandardCharsets.UTF_8
with "UTF-8"
.
How do I turn a String into a InputStreamReader in java?
ByteArrayInputStream also does the trick:
InputStream is = new ByteArrayInputStream( myString.getBytes( charset ) );
Then convert to reader:
InputStreamReader reader = new InputStreamReader(is);
How do I read / convert an InputStream into a String in Java?
A nice way to do this is using Apache commons IOUtils
to copy the InputStream
into a StringWriter
... something like
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, encoding);
String theString = writer.toString();
or even
// NB: does not close inputStream, you'll have to use try-with-resources for that
String theString = IOUtils.toString(inputStream, encoding);
Alternatively, you could use ByteArrayOutputStream
if you don't want to mix your Streams and Writers
How to convert a String to an InputStream in Kotlin?
Kotlin has an extension for String
to convert directly.
val inputStream: InputStream = myString.byteInputStream()
The argument on byteInputStream
is defaulted to charset: Charset = Charsets.UTF_8
.
You can look at the extension by writing it and then cmd+click on it or in the package kotlin.io
file IOStream.kt
Relying on the Java version is not wrong, but rather using a more kotlin idiomatic way when possible
How does one create an InputStream from a String?
Here you go:
InputStream is = new ByteArrayInputStream( myString.getBytes() );
Update For multi-byte support use (thanks to Aaron Waibel's comment):
InputStream is = new ByteArrayInputStream(Charset.forName("UTF-16").encode(myString).array());
Please see ByteArrayInputStream manual.
It is safe to use a charset argument in String#getBytes(charset) method above.
After JDK 7+ you can use
java.nio.charset.StandardCharsets.UTF_16
instead of hardcoded encoding string:
InputStream is = new ByteArrayInputStream(StandardCharsets.UTF_16.encode(myString).array());
How can I read a String into an inputStream in Java?
public class StringToInputStreamExample {
public static void main(String[] args) throws IOException {
String str = "This is a String ~ GoGoGo";
// convert String into InputStream
InputStream is = new ByteArrayInputStream(str.getBytes());
// read it with BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
}
Source: How To Convert String To InputStream In Java
How to convert a String[] array to InputStream in Java
You can construct a merged String
with some separator and then to byte[]
and then to ByteArrayInputStream
.
Here's a way to convert a String to InputStream in Java.
converting string path to inputstream
after lots of efforts and code changing i done what i required. Instead of using inputstream, I used ByteArrayInputStream. Firstly converted my string picturepath to bitmap (don't know why i did that .. but did) ten converted that bitmap to ByteArrayInputStream using following code
ByteArrayOutputStream bos = new ByteArrayOutputStream();
imagebitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
and sent that result to my function....success..!!!
Receiving a utf-8 string using InputStreamReader from a Socket?
An InputStreamReader
will only block until it can read one character into the buffer, or detect EOF. There is no guarantee that the buffer will be filled.
If your protocol indicates the length of the string being sent, the receiver needs to loop, tracking the number of characters remaining, until all have been read.
Related Topics
Jvm Takes a Long Time to Resolve Ip-Address for Localhost
How to Create Custom Methods for Use in Spring Security Expression Language Annotations
Can a Java File Have More Than One Class
Post Request Send JSON Data Java Httpurlconnection
Whitespace Matching Regex - Java
Are Static Variables Shared Between Threads
Clone() VS Copy Constructor VS Factory Method
Java Null Check Why Use == Instead of .Equals()
Spring MVC - How to Get All Request Params in a Map in Spring Controller
Using Generics in Spring Data JPA Repositories
Getting Java Version at Runtime
How Is an Instance Initializer Different from a Constructor
How to Create and Run Apache Jmeter Test Scripts from a Java Program