Convert String to InputStream in Java with Examples - Java 147

Convert String to InputStream in Java with Examples – Java 147

Convert String to InputStream in Java with Examples

指定字串,使用外部程式庫將 String 轉換為 InputStream 物件,介紹常見的 Apache Commons IO 、 Guava 等方法, Convert a String to an InputStream in Java 任務是將字串轉換為 InputStream ,本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- conversion
       |                   +- string
       |                       +- StringToInputStreamWithExamplesTest.java   

單元測試

Convert a String to InputStream in Java 提供字串轉換成 InputStream 等操作。

IOUtils_toInputStream

Convert a String to InputStream in Java 使用 Apache Commons IO – IOUtils 將字串轉換成 InputStream 物件。

	@Test
	public void IOUtils_toInputStream() {
		String value = "java147,springboot168,junit151,bash460,it484";
		InputStream is = null;
		try {
			is = IOUtils.toInputStream(value, StandardCharsets.UTF_8);
			System.out.println(is);

			String result = null;
			try {
				result = IOUtils.toString(is, StandardCharsets.UTF_8);
			} catch (IOException ex) {
				ex.printStackTrace();
			}
			System.out.println(result);
			assertEquals(value, result);
		} finally {
			IOUtils.closeQuietly(is);
		}
	}
java.io.ByteArrayInputStream@7960847b
java147,springboot168,junit151,bash460,it484

IOUtils_toInputStream_ThrowException

Convert a String to InputStream in Java 使用 Apache Commons IO – IOUtils 將字串轉換成 InputStream 物件,傳入 String 參數為 null 值,會拋出例外。

	@Test(expected = NullPointerException.class)
	public void IOUtils_toInputStream_ThrowException() {
		String value = null;
		InputStream is = null;
		try {
			is = IOUtils.toInputStream(value, StandardCharsets.UTF_8);
			System.out.println(is);

			String result = null;
			try {
				result = IOUtils.toString(is, StandardCharsets.UTF_8);
			} catch (IOException ex) {
				ex.printStackTrace();
			}
			System.out.println(result);
			assertEquals(value, result);
		} finally {
			IOUtils.closeQuietly(is);
		}
	}
java.lang.NullPointerException
	at org.apache.commons.io.IOUtils.toInputStream(IOUtils.java:2750)
	at org.ruoxue.java_147.string.StringToInputStreamWithExamplesTest.toInputStreamThrowException_IOUtils(StringToInputStreamWithExamplesTest.java:44)

CharSource_openStream

Convert a String to InputStream in Java 使用 Guava – CharSource 將字串轉換成 InputStream 物件。

	@Test
	public void CharSource_openStream() {
		String value = "java147,springboot168,junit151,bash460,it484";
		InputStream is = null;
		try {
			is = CharSource.wrap(value).asByteSource(StandardCharsets.UTF_8).openStream();
			System.out.println(is);

			String result = CharStreams.toString(new InputStreamReader(is, StandardCharsets.UTF_8));
			System.out.println(result);
			assertEquals(value, result);
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException ex) {
				}
			}
		}
	}
com.google.common.io.ReaderInputStream@3b22cdd0
java147,springboot168,junit151,bash460,it484

CharSource_openStream_ThrowException

Convert a String to InputStream in Java 使用 Guava – CharSource 將字串轉換成 InputStream 物件,傳入 String 參數為 null 值,會拋出例外。

	@Test(expected = NullPointerException.class)
	public void CharSource_openStream_ThrowException() {
		String value = null;
		InputStream is = null;
		try {
			is = CharSource.wrap(value).asByteSource(StandardCharsets.UTF_8).openStream();
			System.out.println(is);

			String result = CharStreams.toString(new InputStreamReader(is, StandardCharsets.UTF_8));
			System.out.println(result);
			assertEquals(value, result);
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException ex) {
				}
			}
		}
	}
java.lang.NullPointerException
	at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:889)
	at com.google.common.io.CharSource$CharSequenceCharSource.<init>(CharSource.java:545)
	at com.google.common.io.CharSource.wrap(CharSource.java:498)
	at org.ruoxue.java_147.string.StringToInputStreamWithExamplesTest.openStream_CharSource_ThrowException(StringToInputStreamWithExamplesTest.java:88)

CharSource_openStream_TryWithResources

Convert a String to InputStream in Java 使用 Guava – CharSource 將字串轉換成 InputStream 物件,以及用 Try-with-resources 關閉串流。

	@Test
	public void CharSource_openStream_TryWithResources() {
		String value = "java147,springboot168,junit151,bash460,it484";
		try (InputStream is = CharSource.wrap(value).asByteSource(StandardCharsets.UTF_8).openStream()) {
			System.out.println(is);

			String result = CharStreams.toString(new InputStreamReader(is, StandardCharsets.UTF_8));
			System.out.println(result);
			assertEquals(value, result);
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
com.google.common.io.ReaderInputStream@3b22cdd0
java147,springboot168,junit151,bash460,it484

StringToInputStreamWithExamplesTest.java

Java String to InputStream Examples 新增單元測試,驗證是否符合預期。

package org.ruoxue.java_147.conversion.string;

import static org.junit.Assert.*;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;
import org.junit.Test;

import com.google.common.io.CharSource;
import com.google.common.io.CharStreams;

public class StringToInputStreamWithExamplesTest {

	@Test
	public void IOUtils_toInputStream() {
		String value = "java147,springboot168,junit151,bash460,it484";
		InputStream is = null;
		try {
			is = IOUtils.toInputStream(value, StandardCharsets.UTF_8);
			System.out.println(is);

			String result = null;
			try {
				result = IOUtils.toString(is, StandardCharsets.UTF_8);
			} catch (IOException ex) {
				ex.printStackTrace();
			}
			System.out.println(result);
			assertEquals(value, result);
		} finally {
			IOUtils.closeQuietly(is);
		}
	}

	@Test(expected = NullPointerException.class)
	public void IOUtils_toInputStream_ThrowException() {
		String value = null;
		InputStream is = null;
		try {
			is = IOUtils.toInputStream(value, StandardCharsets.UTF_8);
			System.out.println(is);

			String result = null;
			try {
				result = IOUtils.toString(is, StandardCharsets.UTF_8);
			} catch (IOException ex) {
				ex.printStackTrace();
			}
			System.out.println(result);
			assertEquals(value, result);
		} finally {
			IOUtils.closeQuietly(is);
		}
	}

	@Test
	public void CharSource_openStream() {
		String value = "java147,springboot168,junit151,bash460,it484";
		InputStream is = null;
		try {
			is = CharSource.wrap(value).asByteSource(StandardCharsets.UTF_8).openStream();
			System.out.println(is);

			String result = CharStreams.toString(new InputStreamReader(is, StandardCharsets.UTF_8));
			System.out.println(result);
			assertEquals(value, result);
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException ex) {
				}
			}
		}
	}

	@Test(expected = NullPointerException.class)
	public void CharSource_openStream_ThrowException() {
		String value = null;
		InputStream is = null;
		try {
			is = CharSource.wrap(value).asByteSource(StandardCharsets.UTF_8).openStream();
			System.out.println(is);

			String result = CharStreams.toString(new InputStreamReader(is, StandardCharsets.UTF_8));
			System.out.println(result);
			assertEquals(value, result);
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException ex) {
				}
			}
		}
	}

	@Test
	public void CharSource_openStream_TryWithResources() {
		String value = "java147,springboot168,junit151,bash460,it484";
		try (InputStream is = CharSource.wrap(value).asByteSource(StandardCharsets.UTF_8).openStream()) {
			System.out.println(is);

			String result = CharStreams.toString(new InputStreamReader(is, StandardCharsets.UTF_8));
			System.out.println(result);
			assertEquals(value, result);
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}

心得分享

Convert a String to an InputStream in Java with Examples 除了使用 Java 所提供的 ByteArrayInputStream 之外,也有其他選擇使用外部程式庫來轉換成 InputStream ,像是 Apache Commons IO 、 Guava 等, Java String to InputStream Examples 提供了幾種常見方法的操作範例。

發佈留言