Clover coverage report - Maven Clover report
Coverage timestamp: Sun Aug 20 2006 04:01:04 PDT
file stats: LOC: 159   Methods: 5
NCLOC: 100   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
XTextEncoder.java 0% 0% 0% 0%
coverage
 1    /**
 2    *
 3    * Copyright 2003-2004 The Apache Software Foundation
 4    *
 5    * Licensed under the Apache License, Version 2.0 (the "License");
 6    * you may not use this file except in compliance with the License.
 7    * 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   
 18    package org.apache.geronimo.mail.util;
 19   
 20    import java.io.IOException;
 21    import java.io.OutputStream;
 22   
 23    public class XTextEncoder
 24    implements Encoder
 25    {
 26    protected final byte[] encodingTable =
 27    {
 28    (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
 29    (byte)'8', (byte)'9', (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F'
 30    };
 31   
 32    /*
 33    * set up the decoding table.
 34    */
 35    protected final byte[] decodingTable = new byte[128];
 36   
 37  0 protected void initialiseDecodingTable()
 38    {
 39  0 for (int i = 0; i < encodingTable.length; i++)
 40    {
 41  0 decodingTable[encodingTable[i]] = (byte)i;
 42    }
 43    }
 44   
 45  0 public XTextEncoder()
 46    {
 47  0 initialiseDecodingTable();
 48    }
 49   
 50    /**
 51    * encode the input data producing an XText output stream.
 52    *
 53    * @return the number of bytes produced.
 54    */
 55  0 public int encode(
 56    byte[] data,
 57    int off,
 58    int length,
 59    OutputStream out)
 60    throws IOException
 61    {
 62  0 int bytesWritten = 0;
 63   
 64  0 for (int i = off; i < (off + length); i++)
 65    {
 66  0 int v = data[i] & 0xff;
 67    // character tha must be encoded? Prefix with a '+' and encode in hex.
 68  0 if (v < 33 || v > 126 || v == '+' || v == '+') {
 69  0 out.write((byte)'+');
 70  0 out.write(encodingTable[(v >>> 4)]);
 71  0 out.write(encodingTable[v & 0xf]);
 72  0 bytesWritten += 3;
 73    }
 74    else {
 75    // add unchanged.
 76  0 out.write((byte)v);
 77  0 bytesWritten++;
 78    }
 79    }
 80   
 81  0 return bytesWritten;
 82    }
 83   
 84   
 85    /**
 86    * decode the xtext encoded byte data writing it to the given output stream
 87    *
 88    * @return the number of bytes produced.
 89    */
 90  0 public int decode(
 91    byte[] data,
 92    int off,
 93    int length,
 94    OutputStream out)
 95    throws IOException
 96    {
 97  0 byte[] bytes;
 98  0 byte b1, b2;
 99  0 int outLen = 0;
 100   
 101  0 int end = off + length;
 102   
 103  0 int i = off;
 104  0 while (i < end)
 105    {
 106  0 byte v = data[i++];
 107    // a plus is a hex character marker, need to decode a hex value.
 108  0 if (v == '+') {
 109  0 b1 = decodingTable[data[i++]];
 110  0 b2 = decodingTable[data[i++]];
 111  0 out.write((b1 << 4) | b2);
 112    }
 113    else {
 114    // copied over unchanged.
 115  0 out.write(v);
 116    }
 117    // always just one byte added
 118  0 outLen++;
 119    }
 120   
 121  0 return outLen;
 122    }
 123   
 124    /**
 125    * decode the xtext encoded String data writing it to the given output stream.
 126    *
 127    * @return the number of bytes produced.
 128    */
 129  0 public int decode(
 130    String data,
 131    OutputStream out)
 132    throws IOException
 133    {
 134  0 byte[] bytes;
 135  0 byte b1, b2, b3, b4;
 136  0 int length = 0;
 137   
 138  0 int end = data.length();
 139   
 140  0 int i = 0;
 141  0 while (i < end)
 142    {
 143  0 char v = data.charAt(i++);
 144  0 if (v == '+') {
 145  0 b1 = decodingTable[data.charAt(i++)];
 146  0 b2 = decodingTable[data.charAt(i++)];
 147   
 148  0 out.write((b1 << 4) | b2);
 149    }
 150    else {
 151  0 out.write((byte)v);
 152    }
 153  0 length++;
 154    }
 155   
 156  0 return length;
 157    }
 158    }
 159