Polish contribution
See gh-24805
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -93,6 +93,27 @@ public abstract class StreamUtils {
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the contents of the given {@link ByteArrayOutputStream} into a {@link String}.
|
||||
* <p>This is a more effective equivalent of {@code new String(baos.toByteArray(), charset)}.
|
||||
* <p>As long as the {@code charset} is already available at the point of
|
||||
* invocation, no exception is expected to be thrown by this method.
|
||||
* @param baos the {@code ByteArrayOutputStream} to be copied into a String
|
||||
* @param charset the {@link Charset} to use to decode the bytes
|
||||
* @return the String that has been copied to (possibly empty)
|
||||
* @since 5.2.6
|
||||
*/
|
||||
public static String copyToString(ByteArrayOutputStream baos, Charset charset) {
|
||||
Assert.notNull(baos, "No ByteArrayOutputStream specified");
|
||||
Assert.notNull(charset, "No Charset specified");
|
||||
try {
|
||||
return baos.toString(charset.name());
|
||||
}
|
||||
catch (UnsupportedEncodingException ex) {
|
||||
throw new RuntimeException("Failed to copy contents of ByteArrayOutputStream into a String", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the contents of the given byte array to the given OutputStream.
|
||||
* Leaves the stream open when done.
|
||||
@@ -239,25 +260,6 @@ public abstract class StreamUtils {
|
||||
return new NonClosingOutputStream(out);
|
||||
}
|
||||
|
||||
/**
|
||||
* More effective equivalent of {@code new String(baos.toByteArray(), charset)}
|
||||
* As far as at invocation point {@code charset} is already available,
|
||||
* no exception is expected to be thrown.
|
||||
*
|
||||
* @param baos {@link ByteArrayOutputStream} to be flushed into String
|
||||
* @param charset applicable {@link Charset}
|
||||
* @return String represenation of bytes stored in {@code baos}
|
||||
*/
|
||||
public static String baosToString(ByteArrayOutputStream baos, Charset charset) {
|
||||
Assert.notNull(baos, "No ByteArrayOutputStream specified");
|
||||
Assert.notNull(charset, "No Charset specified");
|
||||
try {
|
||||
return baos.toString(charset.name());
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static class NonClosingInputStream extends FilterInputStream {
|
||||
|
||||
public NonClosingInputStream(InputStream in) {
|
||||
|
||||
@@ -749,7 +749,7 @@ public abstract class StringUtils {
|
||||
}
|
||||
Assert.notNull(charset, "Charset must not be null");
|
||||
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream(length);
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream(length);
|
||||
boolean changed = false;
|
||||
for (int i = 0; i < length; i++) {
|
||||
int ch = source.charAt(i);
|
||||
@@ -762,7 +762,7 @@ public abstract class StringUtils {
|
||||
if (u == -1 || l == -1) {
|
||||
throw new IllegalArgumentException("Invalid encoded sequence \"" + source.substring(i) + "\"");
|
||||
}
|
||||
bos.write((char) ((u << 4) + l));
|
||||
baos.write((char) ((u << 4) + l));
|
||||
i += 2;
|
||||
changed = true;
|
||||
}
|
||||
@@ -771,10 +771,10 @@ public abstract class StringUtils {
|
||||
}
|
||||
}
|
||||
else {
|
||||
bos.write(ch);
|
||||
baos.write(ch);
|
||||
}
|
||||
}
|
||||
return changed ? StreamUtils.baosToString(bos, charset) : source;
|
||||
return (changed ? StreamUtils.copyToString(baos, charset) : source);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user