01: /*
02: * Copyright 2004-2006 Fouad HAMDI.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.
15: */
16: package org.csvbeans.builders;
17:
18: import java.io.BufferedWriter;
19: import java.io.IOException;
20: import java.io.OutputStream;
21: import java.io.OutputStreamWriter;
22:
23: import org.csvbeans.Constants;
24:
25: /**
26: * A writer of CSV files. It writes the CSV lines in a file.
27: *
28: * @author Fouad Hamdi
29: * @since 0.5
30: */
31: public class OutputStreamLinesWriter implements LinesWriter {
32: private BufferedWriter writer;
33:
34: /**
35: * Constructor.
36: *
37: * @param os
38: */
39: public OutputStreamLinesWriter(OutputStream os) {
40: writer = new BufferedWriter(new OutputStreamWriter(os));
41: }
42:
43: /*
44: * (non-Javadoc)
45: *
46: * @see org.csvbeans.interfaces.LinesWriter#writeLine(java.lang.String)
47: */
48: public void writeLine(String line) throws LinesWriterException {
49: try {
50: writer.write(line);
51: writer.write(Constants.RC);
52: } catch (IOException e) {
53: throw new LinesWriterException(e.getMessage(), e);
54: }
55:
56: }
57:
58: /*
59: * (non-Javadoc)
60: *
61: * @see org.csvbeans.interfaces.LinesWriter#close()
62: */
63: public void close() throws LinesWriterException {
64: try {
65: writer.flush();
66: writer.close();
67: } catch (IOException e) {
68: throw new LinesWriterException(e.getMessage(), e);
69: }
70: }
71: }
|