Skip to content

Commit

Permalink
google#5376 Add conversion from reader to inputstream
Browse files Browse the repository at this point in the history
  • Loading branch information
louiscb committed Mar 1, 2022
1 parent ac11adc commit 0d81773
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 12 deletions.
66 changes: 59 additions & 7 deletions guava-tests/test/com/google/common/io/CharStreamsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@

package com.google.common.io;

import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import java.io.EOFException;
import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;

import java.io.*;
import java.nio.CharBuffer;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnmappableCharacterException;
import java.util.List;

/**
Expand Down Expand Up @@ -193,6 +192,47 @@ public void testCopy_toStringBuilder_fromReadable() throws IOException {
assertEquals(I18N.length(), copied);
}

public void testAsInputStream_fromReader() throws IOException {
Reader reader = new StringReader(TEXT);

InputStream inputStream = CharStreams.asInputStream(reader, Charsets.UTF_8);
String inputStreamString = getInputStreamString(inputStream);

assertEquals(TEXT, inputStreamString);
}

public void testAsInputStream_fromReader_withBufferSize() throws IOException {
Reader reader = new StringReader(TEXT);

InputStream inputStream = CharStreams.asInputStream(reader, Charsets.UTF_8, 100);
String inputStreamString = getInputStreamString(inputStream);

assertEquals(TEXT, inputStreamString);
}

public void testAsInputStream_fromReader_withBufferSizeAndCodingErrorAction() throws IOException {
Reader reader = new StringReader(TEXT);

CodingErrorAction codingErrorAction = CodingErrorAction.REPORT;
InputStream inputStream = CharStreams.asInputStream(reader, Charsets.US_ASCII, 100, codingErrorAction);
String inputStreamString = getInputStreamString(inputStream);

assertEquals(TEXT, inputStreamString);
}

public void testAsInputStream_fromReader_withBufferSizeAndCodingErrorActionWithMalformedText() throws IOException {
String malformedText = "ÏíÓãÈÑ";
Reader reader = new StringReader(malformedText);
CodingErrorAction codingErrorAction = CodingErrorAction.REPORT;
InputStream inputStream = CharStreams.asInputStream(reader, Charsets.US_ASCII, 100, codingErrorAction);

try {
getInputStreamString(inputStream);
fail();
} catch (UnmappableCharacterException e) {
}
}

public void testCopy_toWriter_fromReader() throws IOException {
StringWriter writer = new StringWriter();
long copied = CharStreams.copy(new StringReader(ASCII), writer);
Expand Down Expand Up @@ -339,4 +379,16 @@ public int read(CharBuffer cb) throws IOException {
}
};
}

private static String getInputStreamString(InputStream inputStream) throws IOException {
char[] buffer = new char[CharStreams.BUFFER_SIZE_INPUT_STREAM];
Reader input = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
StringBuilder output = new StringBuilder();

for (int numRead; (numRead = input.read(buffer, 0, buffer.length)) != -1; ) {
output.append(buffer, 0, numRead);
}

return output.toString();
}
}
57 changes: 52 additions & 5 deletions guava/src/com/google/common/io/CharStreams.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtIncompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.Closeable;
import java.io.EOFException;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

import java.io.*;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CodingErrorAction;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.CheckForNull;
Expand All @@ -49,6 +48,7 @@ public final class CharStreams {

// 2K chars (4K bytes)
private static final int DEFAULT_BUF_SIZE = 0x800;
public static final int BUFFER_SIZE_INPUT_STREAM = 8192;

/** Creates a new {@code CharBuffer} for buffering reads or writes. */
static CharBuffer createBuffer() {
Expand Down Expand Up @@ -182,6 +182,53 @@ private static StringBuilder toStringBuilder(Readable r) throws IOException {
return sb;
}

/**
* Converts a Reader object into a InputStream using the provided Charset.
* Does not close the {@code Reader}. Defaults to a standard buffer size for the input stream.
*
* @param reader the reader to convert
* @param charset the encoding charset
* @return a {@link InputStream} containing all the reader's contents
*/
public static InputStream asInputStream(Reader reader, Charset charset) {
return new ReaderInputStream(reader,
charset,
BUFFER_SIZE_INPUT_STREAM);
}

/**
* Converts a Reader object into a InputStream using the provided Charset and buffer size.
* Does not close the {@code Reader}.
*
* @param reader the reader to convert
* @param charset the encoding charset
* @param bufferSize the buffer size of the input stream
* @return a {@link InputStream} containing all the reader's contents
*/
public static InputStream asInputStream(Reader reader, Charset charset, int bufferSize) {
return new ReaderInputStream(reader,
charset,
bufferSize);
}

/**
* Converts a Reader object into a InputStream using the provided Charset and buffer size.
* Performs a specified action on malformed input.
* Does not close the {@code Reader}.
*
* @param reader the reader to convert
* @param charset the encoding charset
* @param bufferSize the buffer size of the input stream
* @param newAction the action to take on malformed input and unmappable characters
* @return a {@link InputStream} containing all the reader's contents
*/
public static InputStream asInputStream(Reader reader, Charset charset, int bufferSize,
CodingErrorAction newAction) {
return new ReaderInputStream(reader,
charset.newEncoder().onMalformedInput(newAction).onUnmappableCharacter(newAction),
bufferSize);
}

/**
* Reads all of the lines from a {@link Readable} object. The lines do not include
* line-termination characters, but do include other leading and trailing whitespace.
Expand Down

0 comments on commit 0d81773

Please sign in to comment.