1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.pdfbox.examples; 18 19 import java.io.IOException; 20 import java.io.InputStream; 21 import java.io.OutputStream; 22 23 import org.apache.pdfbox.cos.COSDocument; 24 25 import org.apache.pdfbox.pdmodel.PDDocument; 26 27 import org.apache.pdfbox.pdfwriter.COSWriter; 28 29 /** 30 * A simple class which has some methods used by all examples. 31 * 32 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a> 33 * @version $Revision: 1.4 $ 34 */ 35 public abstract class AbstractExample 36 { 37 /** 38 * Close the stream. 39 * 40 * @param stream The stream to close. 41 * 42 * @throws IOException If there is an error closing the stream. 43 */ 44 public void close( InputStream stream ) throws IOException 45 { 46 if( stream != null ) 47 { 48 stream.close(); 49 } 50 } 51 52 /** 53 * Close the stream. 54 * 55 * @param stream The stream to close. 56 * 57 * @throws IOException If there is an error closing the stream. 58 */ 59 public void close( OutputStream stream ) throws IOException 60 { 61 if( stream != null ) 62 { 63 stream.close(); 64 } 65 } 66 67 /** 68 * Close the document. 69 * 70 * @param doc The doc to close. 71 * 72 * @throws IOException If there is an error closing the document. 73 */ 74 public void close( COSDocument doc ) throws IOException 75 { 76 if( doc != null ) 77 { 78 doc.close(); 79 } 80 } 81 82 /** 83 * Close the document. 84 * 85 * @param doc The doc to close. 86 * 87 * @throws IOException If there is an error closing the document. 88 */ 89 public void close( PDDocument doc ) throws IOException 90 { 91 if( doc != null ) 92 { 93 doc.close(); 94 } 95 } 96 97 /** 98 * Close the writer. 99 * 100 * @param writer The writer to close. 101 * 102 * @throws IOException If there is an error closing the writer. 103 */ 104 public static void close( COSWriter writer ) throws IOException 105 { 106 if( writer != null ) 107 { 108 writer.close(); 109 } 110 } 111 }