Coverage Report - org.apache.commons.flatfile.util.RepeatingInputStream
 
Classes in this File Line Coverage Branch Coverage Complexity
RepeatingInputStream
67%
8/12
50%
2/4
0
RepeatingInputStream$1
N/A
N/A
0
RepeatingInputStream$LimitedOutput
100%
9/9
100%
2/2
0
RepeatingInputStream$RepeatArray
0%
0/7
0%
0/2
0
RepeatingInputStream$RepeatOneByte
100%
5/5
N/A
0
 
 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.commons.flatfile.util;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.io.InputStream;
 21  
 import java.util.HashMap;
 22  
 
 23  
 /**
 24  
  * Provides factory methods to return InputStreams that return a repeating ordered
 25  
  * sequence of bytes, optionally limiting output to some maximum total size.
 26  
  * @version $Revision: 758023 $ $Date: 2009-03-24 16:09:19 -0500 (Tue, 24 Mar 2009) $
 27  
  */
 28  0
 public abstract class RepeatingInputStream {
 29  
 
 30  
     /**
 31  
      * An InputStream that repeats a single byte forever.
 32  
      */
 33  9
     private static class RepeatOneByte extends InputStream {
 34  
         private byte b;
 35  
 
 36  
         /**
 37  
          * Create a new RepeatOneByte instance.
 38  
          * @param b byte to repeat
 39  
          */
 40  9
         private RepeatOneByte(byte b) {
 41  9
             this.b = b;
 42  9
         }
 43  
 
 44  
         /**
 45  
          * {@inheritDoc}
 46  
          */
 47  
         public int read() throws IOException {
 48  677
             return b;
 49  
         }
 50  
     }
 51  
 
 52  
     /**
 53  
      * An InputStream that repeats a byte[] forever.
 54  
      */
 55  0
     private static class RepeatArray extends InputStream {
 56  
         private byte[] b;
 57  
         private int pos;
 58  
 
 59  
         /**
 60  
          * Create a new RepeatArray instance.
 61  
          * @param b byte[] to repeat
 62  
          */
 63  0
         private RepeatArray(byte[] b) {
 64  0
             this.b = b;
 65  0
         }
 66  
 
 67  
         /**
 68  
          * {@inheritDoc}
 69  
          */
 70  
         public synchronized int read() throws IOException {
 71  
             try {
 72  0
                 return b[pos++];
 73  
             } finally {
 74  0
                 if (pos == b.length) {
 75  0
                     pos = 0;
 76  
                 }
 77  
             }
 78  
         }
 79  
     }
 80  
 
 81  
     /**
 82  
      * InputStream that limits the content of another InputStream.
 83  
      */
 84  21
     private static class LimitedOutput extends InputStream {
 85  
         private static final int EOF = -1;
 86  
 
 87  
         private InputStream source;
 88  
         private int bytesToReturn;
 89  
         private int bytesReturned;
 90  
 
 91  
         /**
 92  
          * Create a new LimitedOutput instance.
 93  
          * @param source wrapped InputStream
 94  
          * @param bytesToReturn int max
 95  
          */
 96  21
         private LimitedOutput(InputStream source, int bytesToReturn) {
 97  21
             this.source = source;
 98  21
             this.bytesToReturn = bytesToReturn;
 99  21
         }
 100  
 
 101  
         /**
 102  
          * {@inheritDoc}
 103  
          */
 104  
         public int read() throws IOException {
 105  55
             if (bytesReturned == bytesToReturn) {
 106  13
                 return EOF;
 107  
             }
 108  
             try {
 109  42
                 return source.read();
 110  
             } finally {
 111  42
                 bytesReturned++;
 112  
             }
 113  
         }
 114  
     }
 115  
 
 116  
     /**
 117  
      * Holds cached instances of single-byte RepeatingInputStreams.
 118  
      */
 119  
     // go ahead and init b/c we know we'll want a single-byte instance:
 120  1
     private static HashMap<Byte, InputStream> INSTANCE_MAP = new HashMap<Byte, InputStream>();
 121  
 
 122  
     /**
 123  
      * Get an InputStream that will return the specified byte forever.
 124  
      * @param b byte to repeat
 125  
      * @return InputStream
 126  
      */
 127  
     public static synchronized InputStream getInstance(byte b) {
 128  34
         Byte bigByte = new Byte(b);
 129  34
         InputStream result = INSTANCE_MAP.get(bigByte);
 130  34
         if (result == null) {
 131  9
             result = new RepeatOneByte(b);
 132  9
             INSTANCE_MAP.put(bigByte, result);
 133  
         }
 134  34
         return result;
 135  
     }
 136  
 
 137  
     /**
 138  
      * Get an InputStream that will return the specified byte[] forever.
 139  
      * @param b byte[] to repeat
 140  
      * @return InputStream
 141  
      */
 142  
     public static InputStream getInstance(byte[] b) {
 143  0
         return b.length == 1 ? getInstance(b[0]) : new RepeatArray(b);
 144  
     }
 145  
 
 146  
     /**
 147  
      * Get an InputStream that will return a single byte a limited number of times.
 148  
      * @param b byte to repeat
 149  
      * @param bytesToReturn int
 150  
      * @return InputStream
 151  
      */
 152  
     public static synchronized InputStream getInstance(byte b, int bytesToReturn) {
 153  21
         return new LimitedOutput(getInstance(b), bytesToReturn);
 154  
     }
 155  
 
 156  
     /**
 157  
      * Get an InputStream that will return the specified byte sequence
 158  
      * until a total of <code>bytesToReturn</code> bytes have been returned.
 159  
      * @param b byte[] to repeat
 160  
      * @param bytesToReturn int
 161  
      * @return InputStream
 162  
      */
 163  
     public static synchronized InputStream getInstance(byte[] b,
 164  
             int bytesToReturn) {
 165  0
         return new LimitedOutput(getInstance(b), bytesToReturn);
 166  
     }
 167  
 
 168  
 }