001/* 002 * (C) Copyright 2018 Nuxeo (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Funsho David 018 */ 019 020package org.nuxeo.ecm.core.io.marshallers.csv; 021 022import java.io.IOException; 023import java.io.OutputStream; 024 025import org.apache.commons.csv.CSVPrinter; 026import org.apache.commons.io.output.AppendableOutputStream; 027 028/** 029 * Wrapper for {@link CSVPrinter}. 030 * 031 * @since 10.3 032 */ 033public class OutputStreamWithCSVWriter extends OutputStream { 034 035 protected AppendableOutputStream<Appendable> out; 036 037 protected CSVPrinter csvPrinter; 038 039 public OutputStreamWithCSVWriter() { 040 super(); 041 } 042 043 public OutputStreamWithCSVWriter(CSVPrinter csvPrinter) { 044 super(); 045 setCSVPrinter(csvPrinter); 046 } 047 048 public void setCSVPrinter(CSVPrinter csvPrinter) { 049 this.csvPrinter = csvPrinter; 050 out = new AppendableOutputStream<>(csvPrinter.getOut()); 051 } 052 053 @Override 054 public void write(int i) throws IOException { 055 out.write(i); 056 } 057 058 @Override 059 public void write(byte[] b) throws IOException { 060 out.write(b); 061 } 062 063 @Override 064 public void write(byte[] b, int off, int len) throws IOException { 065 out.write(b, off, len); 066 } 067 068 @Override 069 public void flush() throws IOException { 070 out.flush(); 071 } 072 073 @Override 074 public void close() throws IOException { 075 out.close(); 076 } 077 078 /** 079 * Returns the wrapped CSV printer. 080 * 081 * @return the CSV printer 082 */ 083 public CSVPrinter getCsvPrinter() { 084 return csvPrinter; 085 } 086 087 @Override 088 public String toString() { 089 return out.getAppendable().toString(); 090 } 091}