Clover coverage report - ActiveIO - 1.0
Coverage timestamp: Fri Apr 22 2005 14:27:22 PDT
file stats: LOC: 246   Methods: 27
NCLOC: 167   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
AppendedPacket.java 73.5% 66.7% 66.7% 68.3%
coverage coverage
 1   
 /**
 2   
  * 
 3   
  * Copyright 2004 Hiram Chirino
 4   
  * 
 5   
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 6   
  * use this file except in compliance with the License. You may obtain a copy of
 7   
  * 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, WITHOUT
 13   
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 14   
  * License for the specific language governing permissions and limitations under
 15   
  * the License.
 16   
  */
 17   
 package org.activeio.packet;
 18   
 
 19   
 import java.io.DataOutput;
 20   
 import java.io.IOException;
 21   
 import java.io.OutputStream;
 22   
 import java.lang.reflect.Constructor;
 23   
 
 24   
 import org.activeio.Packet;
 25   
 
 26   
 /**
 27   
  * Appends two packets together.
 28   
  * 
 29   
  * @version $Revision$
 30   
  */
 31   
 final public class AppendedPacket implements Packet {
 32   
 
 33   
     private final Packet first;
 34   
     private final Packet last;
 35   
 
 36   
     private final int capacity;
 37   
     private final int firstCapacity;
 38   
 
 39  30
     static public Packet join(Packet first, Packet last) {
 40  30
         if( first.hasRemaining() ) {
 41  30
             if( last.hasRemaining() ) {
 42   
                 
 43   
                 //TODO: this might even be a rejoin of the same continous buffer.
 44   
                 //It would be good if we detected that and avoided just returned the buffer.
 45   
                 
 46  24
                 return new AppendedPacket(first.slice(), last.slice());               
 47   
             } else {
 48  6
                 return first.slice();
 49   
             }
 50   
         } else {
 51  0
             if( last.hasRemaining() ) {
 52  0
                 return last.slice();                
 53   
             } else {
 54  0
                 return EmptyPacket.EMPTY_PACKET;
 55   
             }            
 56   
         }
 57   
     }
 58   
     
 59   
     /**
 60   
      * @deprecated use {@see #join(Packet, Packet)} instead.
 61   
      */
 62  26
     public AppendedPacket(Packet first, Packet second) {
 63  26
         this.first = first;
 64  26
         this.last = second;
 65  26
         this.firstCapacity = first.capacity();
 66  26
         this.capacity = first.capacity()+last.capacity();
 67  26
         clear();        
 68   
     }
 69   
         
 70  24
     public void position(int position) {
 71  24
         if( position <= firstCapacity ) {
 72  22
             last.position(0);
 73  22
             first.position(position);
 74   
         } else {
 75  2
             last.position(position-firstCapacity);
 76  2
             first.position(firstCapacity);
 77   
         }
 78   
     }
 79   
     
 80  22
     public void limit(int limit) {
 81  22
         if( limit <= firstCapacity ) {
 82  6
             last.limit(0);
 83  6
             first.limit(limit);
 84   
         } else {
 85  16
             last.limit(limit-firstCapacity);
 86  16
             first.limit(firstCapacity);
 87   
         }
 88   
     }
 89   
 
 90  2
     public Packet slice() {
 91  2
         return join(first,last);
 92   
     }
 93   
 
 94  2
     public Packet duplicate() {
 95  2
         return new AppendedPacket(first.duplicate(), last.duplicate());               
 96   
     }
 97   
 
 98  0
     public Object duplicate(ClassLoader cl) throws IOException {
 99  0
         try {
 100  0
             Class pclazz = cl.loadClass(Packet.class.getName());
 101  0
             Class clazz = cl.loadClass(AppendedPacket.class.getName());
 102  0
             Constructor constructor = clazz.getConstructor(new Class[]{pclazz, pclazz});
 103  0
             return constructor.newInstance(new Object[]{first.duplicate(cl), last.duplicate(cl)});
 104   
         } catch (Throwable e) {
 105  0
             throw (IOException)new IOException("Could not duplicate packet in a different classloader: "+e).initCause(e);
 106   
         }
 107   
     }
 108   
     
 109  6
     public void flip() {
 110  6
         limit(position());
 111  6
         position(0);
 112   
     }
 113   
 
 114  22
     public int position() {
 115  22
         return first.position()+last.position();
 116   
     }
 117   
     
 118  16
     public int limit() {
 119  16
         return first.limit()+last.limit();
 120   
     }    
 121   
 
 122  6
     public int remaining() {
 123  6
         return first.remaining()+last.remaining();
 124   
     }
 125   
 
 126  2
     public void rewind() {
 127  2
         first.rewind();
 128  2
         last.rewind();
 129   
     }
 130   
 
 131  6
     public boolean hasRemaining() {
 132  6
         return first.hasRemaining()||last.hasRemaining();
 133   
     }
 134   
 
 135  28
     public void clear() {
 136  28
         first.clear();
 137  28
         last.clear();        
 138   
     }
 139   
 
 140  4
     public int capacity() {
 141  4
         return capacity;
 142   
     }
 143   
 
 144  0
     public void writeTo(OutputStream out) throws IOException {
 145  0
         first.writeTo(out);
 146  0
         last.writeTo(out);
 147   
     }
 148   
     
 149  0
     public void writeTo(DataOutput out) throws IOException {
 150  0
         first.writeTo(out);
 151  0
         last.writeTo(out);
 152   
     }
 153   
 
 154   
 
 155   
     /**
 156   
      * @see org.activeio.Packet#read()
 157   
      */
 158  514
     public int read() {
 159  514
         if( first.hasRemaining() ) {
 160  256
             return first.read();
 161  258
         } else if( last.hasRemaining() ) {
 162  256
             return last.read();
 163   
         } else {
 164  2
             return -1;
 165   
         }
 166   
     }
 167   
 
 168   
     /**
 169   
      * @see org.activeio.Packet#read(byte[], int, int)
 170   
      */
 171  22
     public int read(byte[] data, int offset, int length) {        
 172   
         
 173  22
         int rc1 = first.read(data, offset, length);        
 174  22
         if( rc1==-1 ) {
 175  12
             int rc2 = last.read(data, offset, length);
 176  12
             return ( rc2==-1 ) ? -1 : rc2;
 177   
         } else {
 178  10
             int rc2 = last.read(data, offset+rc1, length-rc1);
 179  10
             return ( rc2==-1 ) ? rc1 : rc1+rc2;
 180   
         }
 181   
 
 182   
     }
 183   
 
 184   
     /**
 185   
      * @see org.activeio.Packet#write(int)
 186   
      */
 187  514
     public boolean write(int data) {
 188  514
         if( first.hasRemaining() ) {
 189  256
             return first.write(data);
 190  258
         } else if( last.hasRemaining() ) {
 191  256
             return last.write(data);
 192   
         } else {
 193  2
             return false;
 194   
         }
 195   
     }
 196   
 
 197   
     /**
 198   
      * @see org.activeio.Packet#write(byte[], int, int)
 199   
      */
 200  22
     public int write(byte[] data, int offset, int length) {
 201  22
         int rc1 = first.write(data, offset, length);        
 202  22
         if( rc1==-1 ) {
 203  12
             int rc2 = last.write(data, offset, length);
 204  12
             return ( rc2==-1 ) ? -1 : rc2;
 205   
         } else {
 206  10
             int rc2 = last.write(data, offset+rc1, length-rc1);
 207  10
             return ( rc2==-1 ) ? rc1 : rc1+rc2;
 208   
         }
 209   
     }
 210   
 
 211  0
     public int read(Packet dest) {        
 212  0
         int rc = first.read(dest);
 213  0
         rc += last.read(dest);
 214  0
         return rc;
 215   
     }    
 216   
     
 217  0
     public String toString() {
 218  0
         return "{position="+position()+",limit="+limit()+",capacity="+capacity()+"}";
 219   
     }
 220   
 
 221  0
     public Object narrow(Class target) {
 222  0
         if( target.isAssignableFrom(getClass()) ) {
 223  0
             return this;
 224   
         }
 225  0
         Object object = first.narrow(target);
 226  0
         if( object == null )
 227  0
             object = last.narrow(target);
 228  0
         return object;
 229   
     }
 230   
 
 231  0
     public ByteSequence asByteSequence() {      
 232   
         // TODO: implement me
 233  0
         return null;
 234   
     }
 235   
 
 236  0
     public byte[] sliceAsBytes() {
 237   
         // TODO: implement me
 238  0
         return null;
 239   
     }
 240   
 
 241  0
     public void dispose() {
 242  0
         first.dispose();
 243  0
         last.dispose();
 244   
     }
 245   
 
 246   
 }