1 package org.apache.maven.doxia.wrapper;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.OutputStream;
23 import java.util.Objects;
24
25 import static org.codehaus.plexus.util.StringUtils.isEmpty;
26
27 /**
28 * Wrapper for an output stream.
29 *
30 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
31 */
32 public class OutputStreamWrapper
33 extends AbstractWrapper
34 {
35 /** serialVersionUID */
36 static final long serialVersionUID = 3329037527245430610L;
37
38 private final OutputStream out;
39
40 private final String encoding;
41
42 /**
43 * Private constructor.
44 *
45 * @param format not null
46 * @param supportedFormat not null
47 * @throws IllegalArgumentException if any.
48 */
49 private OutputStreamWrapper( OutputStream out, String format, String encoding, String[] supportedFormat )
50 {
51 super( format, supportedFormat );
52
53 if ( getFormat().equalsIgnoreCase( AUTO_FORMAT ) )
54 {
55 throw new IllegalArgumentException( "output format is required" );
56 }
57
58 this.out = out;
59 this.encoding = encoding;
60 }
61
62 /**
63 * @return the output stream
64 */
65 public OutputStream getOutputStream()
66 {
67 return this.out;
68 }
69
70 /**
71 * @return the encoding
72 */
73 public String getEncoding()
74 {
75 return encoding;
76 }
77
78 /**
79 * @param out not null
80 * @param format not null
81 * @param encoding not null
82 * @param supportedFormat not null
83 * @return a type safe output stream wrapper
84 */
85 public static OutputStreamWrapper valueOf( OutputStream out, String format, String encoding,
86 String[] supportedFormat )
87 {
88 Objects.requireNonNull( out, "output writer is required" );
89 if ( isEmpty( format ) )
90 {
91 throw new IllegalArgumentException( "output format is required" );
92 }
93
94 return new OutputStreamWrapper( out, format, encoding, supportedFormat );
95 }
96 }