|
27 | 27 | import java.io.OutputStreamWriter; |
28 | 28 | import java.io.Reader; |
29 | 29 | import java.io.StringReader; |
| 30 | +import java.io.Writer; |
30 | 31 | import java.nio.charset.Charset; |
31 | | -import java.nio.charset.StandardCharsets; |
32 | 32 | import java.util.ArrayList; |
33 | 33 | import java.util.List; |
34 | 34 |
|
@@ -673,38 +673,51 @@ static public DatatypeFactory getDatatypeFactory() { |
673 | 673 | return datatypeFactory; |
674 | 674 | } |
675 | 675 |
|
| 676 | + /** |
| 677 | + * Writes bytes from the input stream to the output stream. |
| 678 | + * @param in - the input stream passed in. |
| 679 | + * @param outStream - output stream where the bytes are written. |
| 680 | + */ |
676 | 681 | static public void write(InputStream in, OutputStream outStream) throws IOException { |
| 682 | + if(in == null || outStream == null) |
| 683 | + return; |
677 | 684 | try { |
678 | 685 | byte[] byteArray = new byte[BUFFER_SIZE * 2]; |
679 | 686 |
|
680 | | - while (in.read(byteArray) != -1) { |
681 | | - outStream.write(byteArray); |
| 687 | + int byteCount = 0; |
| 688 | + while ((byteCount = in.read(byteArray)) != -1) { |
| 689 | + outStream.write(byteArray, 0, byteCount); |
682 | 690 | } |
683 | 691 | } finally { |
684 | 692 | in.close(); |
685 | 693 | } |
686 | 694 | } |
687 | | - static public void write(Reader in, OutputStreamWriter out) throws IOException { |
| 695 | + |
| 696 | + /** |
| 697 | + * Writes bytes from the input Reader to the Writer stream. |
| 698 | + * @param in - the Reader passed in. |
| 699 | + * @param out - Writer stream where the bytes are written. |
| 700 | + */ |
| 701 | + static public void write(Reader in, Writer out) throws IOException { |
| 702 | + if(in == null || out == null) |
| 703 | + return; |
688 | 704 | try { |
689 | 705 | char[] charArray = new char[BUFFER_SIZE * 2]; |
690 | | - while (in.read(charArray) != -1) { |
691 | | - out.write(charArray); |
692 | | - } |
| 706 | + int charCount = 0; |
| 707 | + while ((charCount = in.read(charArray)) != -1) { |
| 708 | + out.write(charArray, 0, charCount); |
| 709 | + } |
693 | 710 | } finally { |
694 | 711 | in.close(); |
695 | 712 | } |
696 | 713 | } |
| 714 | + |
| 715 | + /** |
| 716 | + * Writes bytes from the input Reader to the output stream. |
| 717 | + * @param in - the Reader passed in. |
| 718 | + * @param out - OutputStream where the bytes are written. |
| 719 | + */ |
697 | 720 | static public void write(Reader in, OutputStream out) throws IOException { |
698 | | - try { |
699 | | - char[] charArray = new char[BUFFER_SIZE * 2]; |
700 | | - byte[] byteArray; |
701 | | - |
702 | | - while (in.read(charArray) != -1) { |
703 | | - byteArray = new String(charArray).getBytes(StandardCharsets.UTF_8); |
704 | | - out.write(byteArray); |
705 | | - } |
706 | | - } finally { |
707 | | - in.close(); |
708 | | - } |
| 721 | + write(in, new OutputStreamWriter(out)); |
709 | 722 | } |
710 | 723 | } |
0 commit comments