Skip to content

Commit 61b41ee

Browse files
author
Niall Pemberton
committed
IO-140 JDK 1.5 changes: Add new CharSequence flavours of methods
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@619188 13f79535-47bb-0310-9956-ffa450edef68
1 parent ae8a261 commit 61b41ee

5 files changed

Lines changed: 272 additions & 0 deletions

File tree

src/java/org/apache/commons/io/FileUtils.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,34 @@ public static void writeStringToFile(File file, String data) throws IOException
12661266
writeStringToFile(file, data, null);
12671267
}
12681268

1269+
/**
1270+
* Writes a CharSequence to a file creating the file if it does not exist using the default encoding for the VM.
1271+
*
1272+
* @param file the file to write
1273+
* @param data the content to write to the file
1274+
* @throws IOException in case of an I/O error
1275+
* @since IO 2.0
1276+
*/
1277+
public static void write(File file, CharSequence data) throws IOException {
1278+
String str = data == null ? null : data.toString();
1279+
writeStringToFile(file, str);
1280+
}
1281+
1282+
/**
1283+
* Writes a CharSequence to a file creating the file if it does not exist.
1284+
*
1285+
* @param file the file to write
1286+
* @param data the content to write to the file
1287+
* @param encoding the encoding to use, <code>null</code> means platform default
1288+
* @throws IOException in case of an I/O error
1289+
* @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
1290+
* @since IO 2.0
1291+
*/
1292+
public static void write(File file, CharSequence data, String encoding) throws IOException {
1293+
String str = data == null ? null : data.toString();
1294+
writeStringToFile(file, str, encoding);
1295+
}
1296+
12691297
/**
12701298
* Writes a byte array to a file creating the file if it does not exist.
12711299
* <p>

src/java/org/apache/commons/io/IOUtils.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,36 @@ public static LineIterator lineIterator(InputStream input, String encoding)
600600
return new LineIterator(reader);
601601
}
602602

603+
//-----------------------------------------------------------------------
604+
/**
605+
* Convert the specified CharSequence to an input stream, encoded as bytes
606+
* using the default character encoding of the platform.
607+
*
608+
* @param input the CharSequence to convert
609+
* @return an input stream
610+
* @since IO 2.0
611+
*/
612+
public static InputStream toInputStream(CharSequence input) {
613+
return toInputStream(input.toString());
614+
}
615+
616+
/**
617+
* Convert the specified CharSequence to an input stream, encoded as bytes
618+
* using the specified character encoding.
619+
* <p>
620+
* Character encoding names can be found at
621+
* <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
622+
*
623+
* @param input the CharSequence to convert
624+
* @param encoding the encoding to use, null means platform default
625+
* @throws IOException if the encoding is invalid
626+
* @return an input stream
627+
* @since IO 2.0
628+
*/
629+
public static InputStream toInputStream(CharSequence input, String encoding) throws IOException {
630+
return toInputStream(input.toString(), encoding);
631+
}
632+
603633
//-----------------------------------------------------------------------
604634
/**
605635
* Convert the specified string to an input stream, encoded as bytes
@@ -767,6 +797,66 @@ public static void write(char[] data, OutputStream output, String encoding)
767797
}
768798
}
769799

800+
// write CharSequence
801+
//-----------------------------------------------------------------------
802+
/**
803+
* Writes chars from a <code>CharSequence</code> to a <code>Writer</code>.
804+
*
805+
* @param data the <code>CharSequence</code> to write, null ignored
806+
* @param output the <code>Writer</code> to write to
807+
* @throws NullPointerException if output is null
808+
* @throws IOException if an I/O error occurs
809+
* @since Commons IO 2.0
810+
*/
811+
public static void write(CharSequence data, Writer output) throws IOException {
812+
if (data != null) {
813+
write(data.toString(), output);
814+
}
815+
}
816+
817+
/**
818+
* Writes chars from a <code>CharSequence</code> to bytes on an
819+
* <code>OutputStream</code> using the default character encoding of the
820+
* platform.
821+
* <p>
822+
* This method uses {@link String#getBytes()}.
823+
*
824+
* @param data the <code>CharSequence</code> to write, null ignored
825+
* @param output the <code>OutputStream</code> to write to
826+
* @throws NullPointerException if output is null
827+
* @throws IOException if an I/O error occurs
828+
* @since Commons IO 2.0
829+
*/
830+
public static void write(CharSequence data, OutputStream output)
831+
throws IOException {
832+
if (data != null) {
833+
write(data.toString(), output);
834+
}
835+
}
836+
837+
/**
838+
* Writes chars from a <code>CharSequence</code> to bytes on an
839+
* <code>OutputStream</code> using the specified character encoding.
840+
* <p>
841+
* Character encoding names can be found at
842+
* <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
843+
* <p>
844+
* This method uses {@link String#getBytes(String)}.
845+
*
846+
* @param data the <code>CharSequence</code> to write, null ignored
847+
* @param output the <code>OutputStream</code> to write to
848+
* @param encoding the encoding to use, null means platform default
849+
* @throws NullPointerException if output is null
850+
* @throws IOException if an I/O error occurs
851+
* @since Commons IO 2.0
852+
*/
853+
public static void write(CharSequence data, OutputStream output, String encoding)
854+
throws IOException {
855+
if (data != null) {
856+
write(data.toString(), output, encoding);
857+
}
858+
}
859+
770860
// write String
771861
//-----------------------------------------------------------------------
772862
/**
@@ -841,6 +931,7 @@ public static void write(String data, OutputStream output, String encoding)
841931
* @throws NullPointerException if output is null
842932
* @throws IOException if an I/O error occurs
843933
* @since Commons IO 1.1
934+
* @deprecated replaced by write(CharSequence, Writer)
844935
*/
845936
public static void write(StringBuffer data, Writer output)
846937
throws IOException {
@@ -861,6 +952,7 @@ public static void write(StringBuffer data, Writer output)
861952
* @throws NullPointerException if output is null
862953
* @throws IOException if an I/O error occurs
863954
* @since Commons IO 1.1
955+
* @deprecated replaced by write(CharSequence, OutputStream)
864956
*/
865957
public static void write(StringBuffer data, OutputStream output)
866958
throws IOException {
@@ -884,6 +976,7 @@ public static void write(StringBuffer data, OutputStream output)
884976
* @throws NullPointerException if output is null
885977
* @throws IOException if an I/O error occurs
886978
* @since Commons IO 1.1
979+
* @deprecated replaced by write(CharSequence, OutputStream, String)
887980
*/
888981
public static void write(StringBuffer data, OutputStream output,
889982
String encoding) throws IOException {

src/test/org/apache/commons/io/FileUtilsTestCase.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,20 @@ public void testWriteStringToFile2() throws Exception {
11081108
assertEqualContent(text, file);
11091109
}
11101110

1111+
public void testWriteCharSequence1() throws Exception {
1112+
File file = new File(getTestDirectory(), "write.txt");
1113+
FileUtils.write(file, "Hello /u1234", "UTF8");
1114+
byte[] text = "Hello /u1234".getBytes("UTF8");
1115+
assertEqualContent(text, file);
1116+
}
1117+
1118+
public void testWriteCharSequence2() throws Exception {
1119+
File file = new File(getTestDirectory(), "write.txt");
1120+
FileUtils.write(file, "Hello /u1234", null);
1121+
byte[] text = "Hello /u1234".getBytes();
1122+
assertEqualContent(text, file);
1123+
}
1124+
11111125
public void testWriteByteArrayToFile() throws Exception {
11121126
File file = new File(getTestDirectory(), "write.obj");
11131127
byte[] data = new byte[] {11, 21, 31};

src/test/org/apache/commons/io/IOUtilsTestCase.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,26 @@ public void testByteArrayToString()
276276
}
277277
}
278278

279+
/**
280+
* Test for {@link IOUtils#toInputStream(CharSequence)} and {@link IOUtils#toInputStream(CharSequence, String)}.
281+
* Note, this test utilizes on {@link IOUtils#toByteArray(java.io.InputStream)} and so relies on
282+
* {@link #testInputStreamToByteArray()} to ensure this method functions correctly.
283+
*
284+
* @throws Exception on error
285+
*/
286+
public void testCharSequenceToInputStream() throws Exception {
287+
CharSequence csq = new StringBuilder("Abc123Xyz!");
288+
InputStream inStream = IOUtils.toInputStream(csq);
289+
byte[] bytes = IOUtils.toByteArray(inStream);
290+
assertEqualContent(csq.toString().getBytes(), bytes);
291+
inStream = IOUtils.toInputStream(csq, null);
292+
bytes = IOUtils.toByteArray(inStream);
293+
assertEqualContent(csq.toString().getBytes(), bytes);
294+
inStream = IOUtils.toInputStream(csq, "UTF-8");
295+
bytes = IOUtils.toByteArray(inStream);
296+
assertEqualContent(csq.toString().getBytes("UTF-8"), bytes);
297+
}
298+
279299
/**
280300
* Test for {@link IOUtils#toInputStream(String)} and {@link IOUtils#toInputStream(String, String)}.
281301
* Note, this test utilizes on {@link IOUtils#toByteArray(java.io.InputStream)} and so relies on

src/test/org/apache/commons/io/IOUtilsWriteTestCase.java

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,124 @@ public void testWrite_byteArrayToWriter_Encoding_nullEncoding() throws Exception
183183
assertEquals("Sizes differ", inData.length, baout.size());
184184
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
185185
}
186+
//-----------------------------------------------------------------------
187+
public void testWrite_charSequenceToOutputStream() throws Exception {
188+
CharSequence csq = new StringBuilder(new String(inData, "US-ASCII"));
189+
190+
ByteArrayOutputStream baout = new ByteArrayOutputStream();
191+
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
192+
193+
IOUtils.write(csq, out);
194+
out.off();
195+
out.flush();
196+
197+
assertEquals("Sizes differ", inData.length, baout.size());
198+
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
199+
}
200+
201+
public void testWrite_charSequenceToOutputStream_nullData() throws Exception {
202+
ByteArrayOutputStream baout = new ByteArrayOutputStream();
203+
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
204+
205+
IOUtils.write((CharSequence) null, out);
206+
out.off();
207+
out.flush();
208+
209+
assertEquals("Sizes differ", 0, baout.size());
210+
}
211+
212+
public void testWrite_charSequenceToOutputStream_nullStream() throws Exception {
213+
CharSequence csq = new StringBuilder(new String(inData, "US-ASCII"));
214+
try {
215+
IOUtils.write(csq, (OutputStream) null);
216+
fail();
217+
} catch (NullPointerException ex) {}
218+
}
219+
220+
//-----------------------------------------------------------------------
221+
public void testWrite_charSequenceToOutputStream_Encoding() throws Exception {
222+
CharSequence csq = new StringBuilder(new String(inData, "US-ASCII"));
223+
224+
ByteArrayOutputStream baout = new ByteArrayOutputStream();
225+
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
226+
227+
IOUtils.write(csq, out, "UTF16");
228+
out.off();
229+
out.flush();
230+
231+
byte[] bytes = baout.toByteArray();
232+
bytes = new String(bytes, "UTF16").getBytes("US-ASCII");
233+
assertTrue("Content differs", Arrays.equals(inData, bytes));
234+
}
235+
236+
public void testWrite_charSequenceToOutputStream_Encoding_nullData() throws Exception {
237+
ByteArrayOutputStream baout = new ByteArrayOutputStream();
238+
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
239+
240+
IOUtils.write((CharSequence) null, out);
241+
out.off();
242+
out.flush();
243+
244+
assertEquals("Sizes differ", 0, baout.size());
245+
}
246+
247+
public void testWrite_charSequenceToOutputStream_Encoding_nullStream() throws Exception {
248+
CharSequence csq = new StringBuilder(new String(inData, "US-ASCII"));
249+
try {
250+
IOUtils.write(csq, (OutputStream) null);
251+
fail();
252+
} catch (NullPointerException ex) {}
253+
}
254+
255+
public void testWrite_charSequenceToOutputStream_nullEncoding() throws Exception {
256+
CharSequence csq = new StringBuilder(new String(inData, "US-ASCII"));
257+
258+
ByteArrayOutputStream baout = new ByteArrayOutputStream();
259+
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
186260

261+
IOUtils.write(csq, out, null);
262+
out.off();
263+
out.flush();
264+
265+
assertEquals("Sizes differ", inData.length, baout.size());
266+
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
267+
}
268+
269+
//-----------------------------------------------------------------------
270+
public void testWrite_charSequenceToWriter() throws Exception {
271+
CharSequence csq = new StringBuilder(new String(inData, "US-ASCII"));
272+
273+
ByteArrayOutputStream baout = new ByteArrayOutputStream();
274+
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
275+
Writer writer = new OutputStreamWriter(baout, "US-ASCII");
276+
277+
IOUtils.write(csq, writer);
278+
out.off();
279+
writer.flush();
280+
281+
assertEquals("Sizes differ", inData.length, baout.size());
282+
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
283+
}
284+
285+
public void testWrite_charSequenceToWriter_Encoding_nullData() throws Exception {
286+
ByteArrayOutputStream baout = new ByteArrayOutputStream();
287+
YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
288+
Writer writer = new OutputStreamWriter(baout, "US-ASCII");
289+
290+
IOUtils.write((CharSequence) null, writer);
291+
out.off();
292+
writer.flush();
293+
294+
assertEquals("Sizes differ", 0, baout.size());
295+
}
296+
297+
public void testWrite_charSequenceToWriter_Encoding_nullStream() throws Exception {
298+
CharSequence csq = new StringBuilder(new String(inData, "US-ASCII"));
299+
try {
300+
IOUtils.write(csq, (Writer) null);
301+
fail();
302+
} catch (NullPointerException ex) {}
303+
}
187304
//-----------------------------------------------------------------------
188305
public void testWrite_stringToOutputStream() throws Exception {
189306
String str = new String(inData, "US-ASCII");

0 commit comments

Comments
 (0)