2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.util
;
18 import java
.io
.IOException
;
19 import java
.io
.OutputStream
;
20 import java
.io
.OutputStreamWriter
;
21 import java
.io
.UnsupportedEncodingException
;
22 import java
.io
.Writer
;
23 import java
.util
.Iterator
;
24 import java
.util
.List
;
26 import org
.argeo
.ArgeoException
;
28 /** Write in CSV format. */
29 public class CsvWriter
{
30 private final Writer out
;
32 private char separator
= ',';
33 private char quote
= '\"';
36 * Creates a CSV writer.
39 * the stream to write to. Caller is responsible for closing it.
41 public CsvWriter(OutputStream out
) {
42 this.out
= new OutputStreamWriter(out
);
46 * Creates a CSV writer.
49 * the stream to write to. Caller is responsible for closing it.
51 public CsvWriter(OutputStream out
, String encoding
) {
53 this.out
= new OutputStreamWriter(out
, encoding
);
54 } catch (UnsupportedEncodingException e
) {
55 throw new ArgeoException("Cannot initialize CSV writer", e
);
60 * Write a CSV line. Also used to write a header if needed (this is
61 * transparent for the CSV writer): simply call it first, before writing the
64 public void writeLine(List
<?
> tokens
) {
66 Iterator
<?
> it
= tokens
.iterator();
67 while (it
.hasNext()) {
68 writeToken(it
.next().toString());
74 } catch (IOException e
) {
75 throw new ArgeoException("Could not write " + tokens
, e
);
80 * Write a CSV line. Also used to write a header if needed (this is
81 * transparent for the CSV writer): simply call it first, before writing the
84 public void writeLine(Object
[] tokens
) {
86 for (int i
= 0; i
< tokens
.length
; i
++) {
87 if (tokens
[i
] == null) {
88 // TODO configure how to deal with null
91 writeToken(tokens
[i
].toString());
93 if (i
!= (tokens
.length
- 1))
98 } catch (IOException e
) {
99 throw new ArgeoException("Could not write " + tokens
, e
);
103 protected void writeToken(String token
) throws IOException
{
104 // +2 for possible quotes, another +2 assuming there would be an already
105 // quoted string where quotes needs to be duplicated
106 // another +2 for safety
107 // we don't want to increase buffer size while writing
108 StringBuffer buf
= new StringBuffer(token
.length() + 6);
109 char[] arr
= token
.toCharArray();
110 boolean shouldQuote
= false;
129 if (shouldQuote
== true)
131 out
.write(buf
.toString());
132 if (shouldQuote
== true)
136 public void setSeparator(char separator
) {
137 this.separator
= separator
;
140 public void setQuote(char quote
) {