Subversion Repositories programming

Rev

Rev 218 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
210 ira 1
/*******************************************************************************
2
 * File: P2_EncDec.java
3
 * Author: Ira W. Snyder (devel@irasnyder.com)
219 ira 4
 * License: GNU General Public License v2
210 ira 5
 * Class: CS380 - Computer Networking
6
 *
7
 * Assignment: Project #2
8
 * Date Last Modified: 2006-02-04
9
 *
10
 * Purpose: Encode / Decode a number to / from 4B5B Encoding.
11
 *
12
 ******************************************************************************/
13
 
209 ira 14
import java.io.*;
15
import java.util.*;
16
 
17
public class P2_EncDec
18
{
19
    public static final String[][] encodingTable = {
20
        { "0000", "11110" },
21
        { "0001", "01001" },
22
        { "0010", "10100" },
23
        { "0011", "10101" },
24
        { "0100", "01010" },
25
        { "0101", "01011" },
26
        { "0110", "01110" },
27
        { "0111", "01111" },
28
        { "1000", "10010" },
29
        { "1001", "10011" },
30
        { "1010", "10110" },
31
        { "1011", "10111" },
32
        { "1100", "11010" },
33
        { "1101", "11011" },
34
        { "1110", "11100" },
35
        { "1111", "11101" }};
36
 
37
 
210 ira 38
    /**
39
     * Method: convert4B()
40
     * Purpose: Convert a number from it's 4B representation to the
41
     * corresponding 5B representation.
42
     */
215 ira 43
    public static byte[] convert4B (byte[] _4BNum)
209 ira 44
    {
215 ira 45
        String s = convertToString (_4BNum);
46
 
209 ira 47
        int i = encodingTable.length - 1;
48
 
49
        for (; i >= 0; i--)
215 ira 50
            if (encodingTable[i][0].equals(s))
51
                return convertToByteArray(encodingTable[i][1]);
209 ira 52
 
53
        throw new IllegalArgumentException();
54
    }
55
 
210 ira 56
    /**
57
     * Method: convert5B()
58
     * Purpose: Convert a number from it's 5B representation to the
59
     * corresponding 4B representation.
60
     */
215 ira 61
    public static byte[] convert5B (byte[] _5BNum)
209 ira 62
    {
63
        int i = encodingTable.length - 1;
215 ira 64
        String s = convertToString (_5BNum);
209 ira 65
 
66
        for (; i>=0; i--)
215 ira 67
            if (encodingTable[i][1].equals(s))
68
                return convertToByteArray(encodingTable[i][0]);
209 ira 69
 
70
        throw new IllegalArgumentException();
71
    }
215 ira 72
 
218 ira 73
    /**
74
     * Method: convertToByteArray()
75
     * Purpose: Convert a String to a byte[], useful for sending
76
     * over the network in a Datagram.
77
     */
215 ira 78
    public static byte[] convertToByteArray (String num)
79
    {
80
        int i;
81
        byte[] ret = new byte[num.length()];
82
        byte temp;
83
 
84
        for (i=0; i<ret.length; i++)
85
        {
86
            if (num.charAt(i) == '0')
87
                temp = 0;
88
            else if (num.charAt(i) == '1')
89
                temp = 1;
90
            else
91
                throw new IllegalArgumentException();
92
 
93
            ret[i] = temp;
94
        }
95
 
96
        return ret;
97
    }
98
 
218 ira 99
    /**
100
     * Method: convertToString()
101
     * Purpose: Convert a byte[] to a String. This is really useful for
102
     * performing table lookups in the Encoder / Decoder.
103
     */
215 ira 104
    public static String convertToString (byte[] num)
105
    {
106
        int i;
107
        String s = new String();
108
 
109
        for (i=0; i<num.length; i++)
110
            s = new String (s + num[i]);
111
 
112
        return s;
113
    }
209 ira 114
}
115