View Javadoc

1   package edu.iris.dmc.seedcodec;
2   
3   
4   /***
5    * Codec.java
6    *
7    *
8    * Created: Thu Nov 21 13:01:20 2002
9    *
10   * @author <a href="mailto:crotwell@seis.sc.edu">Philip Crotwell</a>
11   * @version
12   */
13  
14  public class Codec implements B1000Types {
15      public Codec() {
16  
17      }
18  
19      /*** Decompresses the data into the best java primitive type for the given
20      compression and returns it.
21      */
22      public DecompressedData decompress(int type,
23                         byte[] b,
24                         int numSamples,
25                         boolean swapBytes)
26      throws CodecException,  UnsupportedCompressionType
27      {
28      DecompressedData out;
29      int[] itemp;
30      short[] stemp;
31      float[] ftemp;
32      double[] dtemp;
33      int offset = 0;
34      switch ( type ) {
35      case SHORT:
36              // 16 bit values
37              if (b.length < 2 * numSamples) {
38          throw new IllegalArgumentException("Not enough bytes for "+
39                             numSamples+
40                             " 16 bit data points, only "+b.length+
41                             " bytes.");
42          }
43          stemp = new short[numSamples];
44              for (int i=0; i<stemp.length; i++) {
45                  stemp[i] = Utility.bytesToShort(b[offset],
46                             b[offset+1],
47                             swapBytes);
48                  offset += 2;
49              }
50          out = new DecompressedData(stemp);
51          break;
52          case 2:
53              // 24 bit values
54              if (b.length < 3 * numSamples) {
55          throw new IllegalArgumentException("Not enough bytes for "+
56                             numSamples+
57                             " 24 bit data points, only "+b.length+
58                             " bytes.");
59          }
60              itemp = new int[numSamples];
61              for (int i=0; i<numSamples; i++) {
62                  itemp[i] = Utility.bytesToInt(b[offset],
63                           b[offset+1],
64                           b[offset+2],
65                           swapBytes);
66                  offset += 3;
67              }
68          out = new DecompressedData(itemp);
69              break;
70          case 3:
71              // 32 bit integers
72              if (b.length < 4 * numSamples) {
73          throw new IllegalArgumentException("Not enough bytes for "+
74                             numSamples+
75                             " 32 bit data points, only "+b.length+
76                             " bytes.");
77          }
78              itemp = new int[numSamples];
79              for (int i=0; i<numSamples; i++) {
80                  itemp[i] = Utility.bytesToInt(b[offset],
81                           b[offset+1],
82                           b[offset+2],
83                           b[offset+3],
84                           swapBytes);
85                  offset += 4;
86              }
87          out = new DecompressedData(itemp);
88              break;
89          case 4:
90              // 32 bit floats
91              if (b.length < 4 * numSamples) {
92          throw new IllegalArgumentException("Not enough bytes for "+
93                             numSamples+
94                             " 32 bit data points, only "+b.length+
95                             " bytes.");
96          }
97              ftemp = new float[numSamples];
98              for (int i=0; i<numSamples; i++) {
99                  ftemp[i] = Float.intBitsToFloat(Utility.bytesToInt(b[offset],
100                                    b[offset+1],
101                                    b[offset+2],
102                                    b[offset+3],
103                                    swapBytes));
104                 offset += 4;
105             }
106         out = new DecompressedData(ftemp);
107             break;
108         case 5:
109             // 64 bit doubles
110             if (b.length < 8 * numSamples) {
111         throw new IllegalArgumentException("Not enough bytes for "+
112                            numSamples+
113                            " 64 bit data points, only "+b.length+
114                            " bytes.");
115         }
116         // ToDo .. implement this type....
117         throw new UnsupportedCompressionType("Type "+type+" is not supported at this time.");
118             //break;
119         case 10:
120             // steim 1
121             itemp = Steim1.decode(b,
122                   numSamples,
123                   swapBytes,
124                   0);
125         out = new DecompressedData(itemp);
126             break;
127         case 11:
128             // steim 2
129             itemp = Steim2.decode(b,
130                        numSamples,
131                        swapBytes,
132                        0);
133         out = new DecompressedData(itemp);
134             break;
135         case 15:
136             // usnsn
137             itemp = USNSN.decode(b,
138                        numSamples,
139                        swapBytes,
140                        0);
141         out = new DecompressedData(itemp);
142             break;
143         default:
144             // unknown format????
145         throw new UnsupportedCompressionType("Type "+type+" is not supported at this time.");
146     } // end of switch ()
147     return out;
148     }
149 
150     /*** returns an integer that represent the java primitive that the data
151      *  will decompresses to. This is to allow for SEED types 4 and 5, float
152      *  and double, which cannot be represented as int without a loss of
153      *  precision.
154      */
155     public int getDecompressedType(int type)
156     throws UnsupportedCompressionType
157     {
158     if (type == INT24 || type == INTEGER || type == STEIM1 || type == STEIM2) {
159         return INTEGER;
160     } else  if (type == SHORT ) {
161         return SHORT;
162     } else  if (type == FLOAT) {
163         return FLOAT;
164     } else if ( type == DOUBLE) {
165         return DOUBLE;
166     } // end of if ()
167     // ????
168     throw new UnsupportedCompressionType("Type "+type+" is not supported at this time.");
169     }
170 
171 }// Codec