Coverage Report - net.sf.jmatchparser.util.charset.MapBasedEncoder
 
Classes in this File Line Coverage Branch Coverage Complexity
MapBasedEncoder
71%
10/14
66%
4/6
3,5
 
 1  
 /*
 2  
  * Copyright (c) 2009 - 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.charset;
 34  
 
 35  
 import java.nio.ByteBuffer;
 36  
 import java.nio.CharBuffer;
 37  
 import java.nio.charset.Charset;
 38  
 import java.nio.charset.CharsetEncoder;
 39  
 import java.nio.charset.CoderResult;
 40  
 import java.util.Map;
 41  
 
 42  
 class MapBasedEncoder extends CharsetEncoder {
 43  
 
 44  
         private final Map<Character, byte[]> encoderMap;
 45  
 
 46  
         MapBasedEncoder(Charset cs, Map<Character, byte[]> encoderMap, float avgBytesPerChar, float maxBytesPerChar, byte[] replacement) {
 47  3
                 super(cs, avgBytesPerChar, maxBytesPerChar, replacement);
 48  3
                 this.encoderMap = encoderMap;
 49  3
         }
 50  
 
 51  
         @Override
 52  
         protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) {
 53  778
                 while (in.remaining() > 0) {
 54  770
                         byte[] bs = encoderMap.get(in.get());
 55  770
                         if (bs == null) {
 56  0
                                 in.position(in.position() - 1);
 57  0
                                 return CoderResult.unmappableForLength(1);
 58  770
                         } else if (out.remaining() < bs.length) {
 59  0
                                 in.position(in.position() - 1);
 60  0
                                 return CoderResult.OVERFLOW;
 61  
                         }
 62  770
                         out.put(bs);
 63  770
                 }
 64  8
                 return CoderResult.UNDERFLOW;
 65  
         }
 66  
 }