Coverage Report - net.sf.jmatchparser.util.split.FileOutputStreamFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
FileOutputStreamFactory
0%
0/9
N/A
1
 
 1  
 /*
 2  
  * Copyright (c) 2006 - 2011 Michael Schierl
 3  
  * 
 4  
  * All rights reserved.
 5  
  * 
 6  
  * Redistribution and use in source and binary forms, with or without
 7  
  * modification, are permitted provided that the following conditions
 8  
  * are met:
 9  
  * 
 10  
  * - Redistributions of source code must retain the above copyright notice,
 11  
  *   this list of conditions and the following disclaimer.
 12  
  *   
 13  
  * - Redistributions in binary form must reproduce the above copyright
 14  
  *   notice, this list of conditions and the following disclaimer in the
 15  
  *   documentation and/or other materials provided with the distribution.
 16  
  *   
 17  
  * - Neither name of the copyright holders nor the names of its
 18  
  *   contributors may be used to endorse or promote products derived from
 19  
  *   this software without specific prior written permission.
 20  
  *   
 21  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND THE CONTRIBUTORS
 22  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 23  
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 24  
  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 25  
  * HOLDERS OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 26  
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 27  
  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 28  
  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 29  
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 30  
  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 31  
  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 32  
  */
 33  
 package net.sf.jmatchparser.util.split;
 34  
 
 35  
 import java.io.File;
 36  
 import java.io.FileNotFoundException;
 37  
 import java.io.FileOutputStream;
 38  
 import java.io.OutputStream;
 39  
 import java.text.Format;
 40  
 import java.text.MessageFormat;
 41  
 
 42  
 /**
 43  
  * Output stream factory that creates file output streams.
 44  
  */
 45  
 public class FileOutputStreamFactory implements SplitOutputStream.StreamFactory {
 46  
 
 47  
         private final File directory;
 48  
         private final Format filenameFormat;
 49  
         private final int firstNumber;
 50  
 
 51  
         /**
 52  
          * Create a new file output stream factory that creates files in a given
 53  
          * directory in a given format.
 54  
          * 
 55  
          * @param directory
 56  
          *            Directory to put the files into.
 57  
          * @param filenameFormat
 58  
          *            Format string used to format the file name, as defined by
 59  
          *            {@link MessageFormat}
 60  
          * @param firstNumber
 61  
          *            Number to use for the first stream
 62  
          */
 63  
         public FileOutputStreamFactory(File directory, String filenameFormat, int firstNumber) {
 64  0
                 this(directory, new MessageFormat(filenameFormat), firstNumber);
 65  0
         }
 66  
 
 67  
         /**
 68  
          * Create a new file output stream factory that creates files in a given
 69  
          * directory in a given format.
 70  
          * 
 71  
          * @param directory
 72  
          *            Directory to put the files into.
 73  
          * @param filenameFormat
 74  
          *            {@link Format} used to format the file name
 75  
          * @param firstNumber
 76  
          *            Number to use for the first stream
 77  
          */
 78  0
         public FileOutputStreamFactory(File directory, Format filenameFormat, int firstNumber) {
 79  0
                 this.directory = directory;
 80  0
                 this.filenameFormat = filenameFormat;
 81  0
                 this.firstNumber = firstNumber;
 82  0
         }
 83  
 
 84  
         @Override
 85  
         public OutputStream createOutputStream(int index) throws FileNotFoundException {
 86  0
                 String filename = filenameFormat.format(new Object[] { new Integer(index + firstNumber) });
 87  0
                 return new FileOutputStream(new File(directory, filename));
 88  
         }
 89  
 }