diff --git a/build.xml b/build.xml index 5da8aa9..c80deda 100644 --- a/build.xml +++ b/build.xml @@ -8,7 +8,9 @@ - + + + diff --git a/javadoc.xml b/javadoc.xml index d6492c2..ba1ded9 100644 --- a/javadoc.xml +++ b/javadoc.xml @@ -15,7 +15,7 @@ access="public"> JavaCSV]]> - http://sourceforge.net/projects/javacsv/]]> + ]]> diff --git a/src/AllTests.java b/src/AllTests.java index 9f159a1..d9a9dd6 100644 --- a/src/AllTests.java +++ b/src/AllTests.java @@ -1,3 +1,23 @@ +/* + * Java CSV is a stream based library for reading and writing + * CSV and other delimited data. + * + * Copyright (C) Bruce Dunwiddie bruce@csvreader.com + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -28,7 +48,7 @@ public class AllTests { public static void main(String[] args) throws Exception { - Class testClass = AllTests.class; + Class testClass = AllTests.class; ArrayList setups = new ArrayList(); ArrayList tearDowns = new ArrayList(); @@ -63,7 +83,7 @@ setup.invoke(instance, (Object[]) null); } - Class expectedException = testAnnotation.expected(); + Class expectedException = testAnnotation.expected(); // can't for the life of me get eclipse to be able to // resolve Test.None directly @@ -652,7 +672,7 @@ new OutputStreamWriter(new FileOutputStream("temp.csv"), Charset.forName("UTF-8"))), ','); // writer will trim all whitespace and put this in quotes to preserve - // it's existance + // it's existence writer.write(" \t \t"); writer.close(); @@ -1424,7 +1444,7 @@ @Test public void test76() throws Exception { CsvReader reader = CsvReader.parse("user_id,name\r\n1,Bruce"); - Assert.assertEquals(null, reader.getHeaders()); + Assert.assertNull(reader.getHeaders()); Assert.assertEquals(-1, reader.getIndex("user_id")); Assert.assertEquals("", reader.getHeader(0)); Assert.assertTrue(reader.readHeaders()); @@ -1435,7 +1455,7 @@ Assert.assertEquals("user_id", headers[0]); Assert.assertEquals("name", headers[1]); reader.setHeaders(null); - Assert.assertEquals(null, reader.getHeaders()); + Assert.assertNull(reader.getHeaders()); Assert.assertEquals(-1, reader.getIndex("user_id")); Assert.assertEquals("", reader.getHeader(0)); reader.close(); @@ -1576,7 +1596,7 @@ @Test public void test88() throws Exception { try { - CsvReader reader = new CsvReader((String) null, ',', Charset + new CsvReader((String) null, ',', Charset .forName("ISO-8859-1")); } catch (Exception ex) { assertException(new IllegalArgumentException( @@ -1587,7 +1607,7 @@ @Test public void test89() throws Exception { try { - CsvReader reader = new CsvReader("temp.csv", ',', null); + new CsvReader("temp.csv", ',', null); } catch (Exception ex) { assertException(new IllegalArgumentException( "Parameter charset can not be null."), ex); @@ -1597,7 +1617,7 @@ @Test public void test90() throws Exception { try { - CsvReader reader = new CsvReader((Reader) null, ','); + new CsvReader((Reader) null, ','); } catch (Exception ex) { assertException(new IllegalArgumentException( "Parameter inputStream can not be null."), ex); @@ -1649,7 +1669,7 @@ @Test public void test112() throws Exception { try { - CsvWriter writer = new CsvWriter((String) null, ',', Charset + new CsvWriter((String) null, ',', Charset .forName("ISO-8859-1")); } catch (Exception ex) { assertException(new IllegalArgumentException("Parameter fileName can not be null."), ex); @@ -1659,7 +1679,7 @@ @Test public void test113() throws Exception { try { - CsvWriter writer = new CsvWriter("test.csv", ',', (Charset) null); + new CsvWriter("test.csv", ',', (Charset) null); } catch (Exception ex) { assertException(new IllegalArgumentException("Parameter charset can not be null."), ex); } @@ -1668,7 +1688,7 @@ @Test public void test114() throws Exception { try { - CsvWriter writer = new CsvWriter((Writer) null, ','); + new CsvWriter((Writer) null, ','); } catch (Exception ex) { assertException(new IllegalArgumentException("Parameter outputStream can not be null."), ex); } @@ -2159,7 +2179,7 @@ public void test149() throws Exception { try { - CsvReader reader = new CsvReader("C:\\somefilethatdoesntexist.csv"); + new CsvReader("C:\\somefilethatdoesntexist.csv"); } catch (Exception ex) { @@ -2180,7 +2200,7 @@ reader.readRecord(); } catch (IOException ex) { // make sure stream that caused exception - // has been sent a dipose call + // has been sent a dispose call Assert.assertTrue(fail.DisposeCalled); exceptionThrown = true; Assert.assertEquals("Read failed.", ex.getMessage()); @@ -2217,4 +2237,27 @@ DisposeCalled = true; } } + + @Test + public void Test174() throws IOException { + // verifies that data is eventually automatically flushed + CsvWriter writer = new CsvWriter("temp.csv"); + + for (int i = 0; i < 10000; i++) + { + writer.write("stuff"); + writer.endRecord(); + } + + CsvReader reader = new CsvReader("temp.csv"); + + Assert.assertTrue(reader.readRecord()); + + Assert.assertEquals("stuff", reader.get(0)); + + writer.close(); + reader.close(); + + new File("temp.csv").delete(); + } } diff --git a/src/com/csvreader/CsvWriter.java b/src/com/csvreader/CsvWriter.java index bc224e9..1e17eca 100644 --- a/src/com/csvreader/CsvWriter.java +++ b/src/com/csvreader/CsvWriter.java @@ -24,7 +24,7 @@ import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; -import java.io.PrintWriter; +import java.io.BufferedWriter; import java.io.Writer; import java.nio.charset.Charset; @@ -32,8 +32,8 @@ * A stream based writer for writing delimited text data to a file or a stream. */ public class CsvWriter { - private PrintWriter outputStream = null; - + private Writer outputStream = null; + private String fileName = null; private boolean firstColumn = true; @@ -48,16 +48,18 @@ private boolean initialized = false; private boolean closed = false; - - /** - * Double up the text qualifier to represent an occurance of the text + + private String systemRecordDelimiter = System.getProperty("line.separator"); + + /** + * Double up the text qualifier to represent an occurrence of the text * qualifier. */ public static final int ESCAPE_MODE_DOUBLED = 1; /** * Use a backslash character before the text qualifier to represent an - * occurance of the text qualifier. + * occurrence of the text qualifier. */ public static final int ESCAPE_MODE_BACKSLASH = 2; @@ -113,7 +115,7 @@ throw new IllegalArgumentException("Parameter outputStream can not be null."); } - this.outputStream = new PrintWriter(outputStream); + this.outputStream = outputStream; userSettings.Delimiter = delimiter; initialized = true; } @@ -387,9 +389,9 @@ if (useCustomRecordDelimiter) { outputStream.write(userSettings.RecordDelimiter); } else { - outputStream.println(); - } - + outputStream.write(systemRecordDelimiter); + } + firstColumn = true; } @@ -447,7 +449,7 @@ if (useCustomRecordDelimiter) { outputStream.write(userSettings.RecordDelimiter); } else { - outputStream.println(); + outputStream.write(systemRecordDelimiter); } firstColumn = true; @@ -459,7 +461,7 @@ private void checkInit() throws IOException { if (!initialized) { if (fileName != null) { - outputStream = new PrintWriter(new OutputStreamWriter( + outputStream = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(fileName), charset)); } @@ -470,8 +472,11 @@ /** * Clears all buffers for the current writer and causes any buffered data to * be written to the underlying device. - */ - public void flush() { + * @exception IOException + * Thrown if an error occurs while writing data to the + * destination stream. + */ + public void flush() throws IOException { outputStream.flush(); }