Subversion Repositories programming

Rev

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