Coverage Report - org.apache.commons.flatfile.FieldSupport
 
Classes in this File Line Coverage Branch Coverage Complexity
FieldSupport
71%
5/7
N/A
0
FieldSupport$1
N/A
N/A
0
FieldSupport$Overflow
100%
5/5
N/A
0
FieldSupport$Overflow$1
100%
4/4
100%
2/2
0
FieldSupport$Underflow
75%
3/4
N/A
0
FieldSupport$Underflow$1
100%
4/4
100%
2/2
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;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.io.InputStream;
 21  
 
 22  
 /**
 23  
  * Support and basic field options.
 24  
  * @version $Revision: 758023 $ $Date: 2009-03-24 16:09:19 -0500 (Tue, 24 Mar 2009) $
 25  
  */
 26  128
 public abstract class FieldSupport extends EntitySupport {
 27  
 
 28  
     /** Serialization version */
 29  
     private static final long serialVersionUID = -8940832296518637934L;
 30  
 
 31  
     /**
 32  
      * Overflow behavior enumerated type.
 33  
      */
 34  6
     public static enum Overflow implements FieldOption {
 35  
         /** Error on overflow */
 36  1
         ERROR() {
 37  
             /**
 38  
              * {@inheritDoc}
 39  
              */
 40  1
             protected void check(byte[] b, int len) {
 41  93
                 if (b.length > len) {
 42  7
                     throw new IllegalArgumentException(new String(b) + " too large for field ("
 43  
                             + len + ")");
 44  
                 }
 45  86
             }
 46  
         },
 47  
 
 48  
         /** Truncate on overflow (same as IGNORE) */
 49  1
         TRUNCATE,
 50  
 
 51  
         /** Ignore overflow (same as TRUNCATE) */
 52  1
         IGNORE;
 53  
 
 54  
         /**
 55  
          * Check the overflow of the field
 56  
          * @param b value
 57  
          * @param len field length
 58  
          */
 59  
         protected void check(byte[] b, int len) {
 60  22
         }
 61  
     }
 62  
 
 63  
     /**
 64  
      * Underflow behavior enumerated type.
 65  
      */
 66  4
     public static enum Underflow implements FieldOption {
 67  
         /** Error on Underflow */
 68  1
         ERROR() {
 69  
             /**
 70  
              * {@inheritDoc}
 71  
              */
 72  1
             protected void check(byte[] b, int len) {
 73  64
                 if (b.length < len) {
 74  2
                     throw new IllegalArgumentException(new String(b) + " too small for field ("
 75  
                             + len + ")");
 76  
                 }
 77  62
             }
 78  
         },
 79  
 
 80  
         /** Ignore Underflow */
 81  1
         IGNORE;
 82  
 
 83  
         /**
 84  
          * Check the underflow of the field
 85  
          * @param b value
 86  
          * @param len field length
 87  
          */
 88  
         protected void check(byte[] b, int len) {
 89  0
         }
 90  
     }
 91  
 
 92  
     /**
 93  
      * Convenience #readFrom alias.
 94  
      * @param is InputStream for <code>readFrom</code>.
 95  
      */
 96  
     protected void dieOnExceptionRead(InputStream is) {
 97  
         try {
 98  121
             readFrom(is);
 99  0
         } catch (IOException e) {
 100  0
             throw new RuntimeException(e);
 101  121
         }
 102  121
     }
 103  
 }