File: CompressMethod.java

package info (click to toggle)
libjlha-java 0.0.20050504-9
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,448 kB
  • ctags: 1,411
  • sloc: java: 10,791; xml: 156; makefile: 11
file content (451 lines) | stat: -rw-r--r-- 17,451 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//start of CompressMethod.java
//TEXT_STYLE:CODE=Shift_JIS(Japanese):RET_CODE=CRLF

/**
 * CompressMethod.java
 * 
 * Copyright (C) 2001-2002  Michel Ishizuka  All rights reserved.
 * 
 * ȉ̏ɓӂȂ΃\[XƃoCi`̍ĔzzƎgp
 * ύX̗Lɂ炸‚B
 * 
 * PD\[XR[h̍ĔzzɂĒ쌠\ ̏̃Xg
 *     щL̐ێȂĂ͂ȂȂB
 * 
 * QDoCi`̍ĔzzɂĒ쌠\ ̏̃Xg
 *     щL̐gp ̑̔zz
 *     ܂ގɋLqȂ΂ȂȂB
 * 
 * ̃\tgEFA͐Β˔ڂɂĖۏ؂Œ񋟂A̖
 * IBłƂۏ؁AilLƂۏ؂ɂƂǂ܂炸A
 * Ȃ閾IшÎIȕۏ؂ȂB
 * Β˔ڂ ̃\tgEFA̎gpɂ钼ړIAԐړIA
 * IAȁAT^IȁA邢͕KRIȑQ(gpɂf[^
 * AƖ̒f〈܂Ăv̈⎸A֐i
 * T[rX̓l邪AĂꂾɌ肳Ȃ
 * Q)ɑ΂āAȂ鎖Ԃ̌ƂȂƂĂA_̐
 * C△ߎӔC܂ ȂӔC낤ƂAƂꂪs
 * ŝׂ߂łƂĂA܂͂̂悤ȑQ̉”\
 * ĂƂĂ؂̐ӔC𕉂Ȃ̂ƂB
 */

package jp.gr.java_conf.dangan.util.lha;

//import classes and interfaces
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Hashtable;
import java.util.Properties;

//import exceptions
import java.lang.NullPointerException;
import java.lang.IllegalArgumentException;


/**
 * LHÅe萔`B
 * 
 * <pre>
 * -- revision history --
 * $Log: CompressMethod.java,v $
 * Revision 1.1  2002/12/08 00:00:00  dangan
 * [change]
 *     NX LhaConstants  CompressMethod ւƕύXB
 *
 * Revision 1.0  2002/07/24 00:00:00  dangan
 * add to version control
 * [change]
 *     LhaUtil  connectExtractInputStream  connectDecoder Ƃ
 *     connectCompressOutputStream  connectEncoder ƂĈpB
 *     LhaUtil  CompressMethodTo????????? pB
 * [maintanance]
 *     \[X
 *     ^up~
 *     CZX̏C
 *
 * </pre>
 * 
 * @author  $Author: dangan $
 * @version $Revision: 1.1 $
 */
public class CompressMethod{


    //------------------------------------------------------------------
    //  class field
    //------------------------------------------------------------------
    //  compress method identifier
    //------------------------------------------------------------------
    //  public static final String LH0
    //  public static final String LH1
    //  public static final String LH2
    //  public static final String LH3
    //  public static final String LH4
    //  public static final String LH5
    //  public static final String LH6
    //  public static final String LH7
    //  public static final String LHD
    //  public static final String LZS
    //  public static final String LZ4
    //  public static final String LZ5
    //------------------------------------------------------------------
    /**
     * k`B
     * LH0  k "-lh0-" łB
     */
    public static final String LH0 = "-lh0-";

    /**
     * k`B
     * LH1 ͑Oi 4LoCg̎Aőv60oCg
     * LZSS@Ai KInt}@gp邱ƂӖ
     * "-lh1-" łB
     */
    public static final String LH1 = "-lh1-";

    /**
     * k`B
     * LH2 ͑Oi 8LoCg̎Aőv256oCg
     * LZSS@Ai KInt}@gp邱ƂӖ
     * "-lh2-" łB
     * ̈k@ LH1  LH5 ւ̉ǓrŎI
     * gꂽA݂͎gpĂȂB
     */
    public static final String LH2 = "-lh2-";

    /**
     * k`B
     * LH3 ͑Oi 8LoCg̎Aőv256oCg
     * LZSS@Ai ÓInt}@gp邱ƂӖ
     * "-lh3-" łB
     * ̈k@ LH1  LH5 ւ̉ǓrŎI
     * gꂽA݂͎gpĂȂB
     */
    public static final String LH3 = "-lh3-";

    /**
     * k`B
     * LH4 ͑Oi 4LoCg̎Aőv256oCg
     * LZSS@Ai ÓInt}@gp邱ƂӖ
     * "-lh4-" łB
     * ̈k@ 1990NO̔͂ȃ}VňksہA
     * LH5ks̃VXe𓾂ȂɎg
     * ꂽA݂͖wǎgpĂȂB
     */
    public static final String LH4 = "-lh4-";

    /**
     * k`B
     * LH5 ͑Oi 8LoCg̎Aőv256oCg
     * LZSS@Ai ÓInt}@gp邱ƂӖ
     * "-lh5-" łB
     * ݁ALHAŕWŎgp鈳k@łB
     */
    public static final String LH5 = "-lh5-";

    /**
     * k`B
     * LH6 ͑Oi 32LoCg̎Aőv256oCg
     * LZSS@Ai ÓInt}@gp邱ƂӖ
     * "-lh6-" łB
     * "-lh6-" Ƃ LH7 ̈k@̎Ɏgp
     * B̂߁ALHA̎ł쐬ɂɂ "-lh6-"
     * ̕gpȂ LH7 `ňkĂ̂
     * ݂炵B
     * ܂ ̈k@͊JĂ 10N߂o‚
     * ̏ꏊ ̈k@ňkꂽɂ͓o^Ȃ
     * ]܂ƂĂB
     */
    public static final String LH6 = "-lh6-";

    /**
     * k`B
     * LH7 ͑Oi 64LoCg̎Aőv256oCg
     * LZSS@Ai ÓInt}@gp邱ƂӖ
     * "-lh7-" łB
     * ܂ ̈k@͊JĂ 10N߂o‚
     * ̏ꏊ ̈k@ňkꂽɂ͓o^Ȃ
     * ]܂ƂĂB
     */
    public static final String LH7 = "-lh7-";

    /**
     * k`B
     * LHD ͖kŁAfBNgi[Ă邱Ƃ
     * "-lhd-" łB
     */
    public static final String LHD = "-lhd-";

    /**
     * k`B
     * LZS  2LoCg̎Aőv17oCg 
     * LZSS@gp邱Ƃ "-lzs-" łB
     * "-lzs-"  LHA쐬OɃW[ł
     * Larc ̌`łǍ݊ɔzĒ`
     * ꂽB݂͖wǎgpĂȂB
     */
    public static final String LZS = "-lzs-";

    /**
     * k`B
     * LZ4  k "-lz4-" łB
     * "-lz4-"  LHA쐬OɃW[ł
     * Larc ̌`łǍ݊ɔzĒ`
     * ꂽB݂͖wǎgpĂȂB
     */
    public static final String LZ4 = "-lz4-";

    /**
     * k`B
     * LZ5  4LoCg̎Aőv17oCg 
     * LZSS@gp邱Ƃ "-lz5-" łB
     * "-lz5-"  LHA쐬OɃW[ł
     * Larc ̌`łǍ݊ɔzĒ`
     * ꂽB݂͖wǎgpĂȂB
     */
    public static final String LZ5 = "-lz5-";


    //------------------------------------------------------------------
    //  constructor
    //------------------------------------------------------------------
    //  private CompressMethod()
    //------------------------------------------------------------------
    /**
     * ftHgRXgN^gps
     */
    private CompressMethod(){ }


    //------------------------------------------------------------------
    //  convert to LZSS parameter
    //------------------------------------------------------------------
    //  public static int toDictionarySize( String method )
    //  public static int toThreshold( String method )
    //  public static int toMaxMatch( String method )
    //------------------------------------------------------------------
    /**
     * k@ʎq TCY𓾂B
     * 
     * @param method k@ʎq
     * 
     * @return TCY
     */
    public static int toDictionarySize( String method ){
        if( CompressMethod.LZS.equalsIgnoreCase( method ) ){
            return  2048;
        }else if( CompressMethod.LZ5.equalsIgnoreCase( method ) ){
            return  4096;
        }else if( CompressMethod.LH1.equalsIgnoreCase( method ) ){
            return  4096;
        }else if( CompressMethod.LH2.equalsIgnoreCase( method ) ){
            return  8192;
        }else if( CompressMethod.LH3.equalsIgnoreCase( method ) ){
            return  8192;
        }else if( CompressMethod.LH4.equalsIgnoreCase( method ) ){
            return  4096;
        }else if( CompressMethod.LH5.equalsIgnoreCase( method ) ){
            return  8192;
        }else if( CompressMethod.LH6.equalsIgnoreCase( method ) ){
            return 32768;
        }else if( CompressMethod.LH7.equalsIgnoreCase( method ) ){
            return 65536;
        }else if( CompressMethod.LZ4.equalsIgnoreCase( method ) ){
            throw new IllegalArgumentException( method + " means no compress." );
        }else if( CompressMethod.LH0.equalsIgnoreCase( method ) ){
            throw new IllegalArgumentException( method + " means no compress." );
        }else if( CompressMethod.LHD.equalsIgnoreCase( method ) ){
            throw new IllegalArgumentException( method + " means no compress." );
        }else if( method == null ){
            throw new NullPointerException( "method" );
        }else{
            throw new IllegalArgumentException( "Unknown compress method. " + method );
        }
    }

    /**
     * k@ʎq k/񈳏k臒l𓾂B
     * 
     * @param method k@ʎq
     * 
     * @return k/񈳏k
     */
    public static int toThreshold( String method ){
        if( CompressMethod.LZS.equalsIgnoreCase( method ) ){
            return 2;
        }else if( CompressMethod.LZ5.equalsIgnoreCase( method ) ){
            return 3;
        }else if( CompressMethod.LH1.equalsIgnoreCase( method ) ){
            return 3;
        }else if( CompressMethod.LH2.equalsIgnoreCase( method ) ){
            return 3;
        }else if( CompressMethod.LH3.equalsIgnoreCase( method ) ){
            return 3;
        }else if( CompressMethod.LH4.equalsIgnoreCase( method ) ){
            return 3;
        }else if( CompressMethod.LH5.equalsIgnoreCase( method ) ){
            return 3;
        }else if( CompressMethod.LH6.equalsIgnoreCase( method ) ){
            return 3;
        }else if( CompressMethod.LH7.equalsIgnoreCase( method ) ){
            return 3;
        }else if( CompressMethod.LZ4.equalsIgnoreCase( method ) ){
            throw new IllegalArgumentException( method + " means no compress." );
        }else if( CompressMethod.LH0.equalsIgnoreCase( method ) ){
            throw new IllegalArgumentException( method + " means no compress." );
        }else if( CompressMethod.LHD.equalsIgnoreCase( method ) ){
            throw new IllegalArgumentException( method + " means no compress." );
        }else if( method == null ){
            throw new NullPointerException( "method" );
        }else{
            throw new IllegalArgumentException( "Unknown compress method. " + method );
        }
    }

    /**
     * k@ʎq őv𓾂B
     * 
     * @param method k@ʎq
     * 
     * @return őv
     */
    public static int toMaxMatch( String method ){
        if( CompressMethod.LZS.equalsIgnoreCase( method ) ){
            return  17;
        }else if( CompressMethod.LZ5.equalsIgnoreCase( method ) ){
            return  18;
        }else if( CompressMethod.LH1.equalsIgnoreCase( method ) ){
            return  60;
        }else if( CompressMethod.LH2.equalsIgnoreCase( method ) ){
            return 256;
        }else if( CompressMethod.LH3.equalsIgnoreCase( method ) ){
            return 256;
        }else if( CompressMethod.LH4.equalsIgnoreCase( method ) ){
            return 256;
        }else if( CompressMethod.LH5.equalsIgnoreCase( method ) ){
            return 256;
        }else if( CompressMethod.LH6.equalsIgnoreCase( method ) ){
            return 256;
        }else if( CompressMethod.LH7.equalsIgnoreCase( method ) ){
            return 256;
        }else if( CompressMethod.LZ4.equalsIgnoreCase( method ) ){
            throw new IllegalArgumentException( method + " means no compress." );
        }else if( CompressMethod.LH0.equalsIgnoreCase( method ) ){
            throw new IllegalArgumentException( method + " means no compress." );
        }else if( CompressMethod.LHD.equalsIgnoreCase( method ) ){
            throw new IllegalArgumentException( method + " means no compress." );
        }else if( method == null ){
            throw new NullPointerException( "method" );
        }else{
            throw new IllegalArgumentException( "Unknown compress method. " + method );
        }
    }


    //------------------------------------------------------------------
    //  shared method
    //------------------------------------------------------------------
    //  connect encoder/decoder
    //------------------------------------------------------------------
    //  public static OutputStream connectEncoder( OutputStream out,
    //                 String method, Properties property )
    //  public static InputStream connectDecoder( InputStream in,
    //                 String method, Properties property, long length )
    //------------------------------------------------------------------
    /**
     * property ɐݒ肳ꂽ𗘗p
     * method ̈k@Ńf[^kAoutɏo͂Xg[\zB
     * 
     * @param out      kf[^o͐̃Xg[
     * @param method   k@ʎq
     * @param property ek`ɑΉ̐܂܂vpeB
     * 
     * @return method ̈k@Ńf[^kAoutɏo͂Xg[
     */
    public static OutputStream connectEncoder( OutputStream out,
                                               String       method,
                                               Properties   property ){

        String key = "lha." + CompressMethod.getCore( method ) + ".encoder";

        String generator = property.getProperty( key );
        if( generator == null ){
            generator = LhaProperty.getProperty( key );
        }

        String packages = property.getProperty( "lha.packages" );
        if( packages == null ){
            packages = LhaProperty.getProperty( "lha.packages" );
        }

        Hashtable substitute = new Hashtable();
        substitute.put( "out", out );

        return (OutputStream)LhaProperty.parse( generator,
                                                substitute,
                                                packages );
    }

    /**
     * property ɐݒ肳ꂽ𗘗p
     * in  method ̈k@ňkꂽf[^𓀂
     * ̓Xg[\zB
     * 
     * @param in       kf[^Xg[
     * @param method   k@ʎq
     * @param property ek`ɑΉ̐܂܂vpeB
     * 
     * @return in  method ̈k@ňkꂽf[^𓀂
     *         ̓Xg[\zB
     */
    public static InputStream connectDecoder( InputStream in,
                                              String      method,
                                              Properties  property,
                                              long        length ){

        String key = "lha." + CompressMethod.getCore( method ) + ".decoder";

        String generator = property.getProperty( key );
        if( generator == null ){
            generator = LhaProperty.getProperty( key );
        }

        String packages = property.getProperty( "lha.packages" );
        if( packages == null ){
            packages = LhaProperty.getProperty( "lha.packages" );
        }

        Hashtable substitute = new Hashtable();
        substitute.put( "in",     in );
        substitute.put( "length", new Long( length ) );

        return (InputStream)LhaProperty.parse( generator,
                                               substitute,
                                               packages );
    }


    //------------------------------------------------------------------
    //  local method
    //------------------------------------------------------------------
    //  private static String getCore( String method )
    //------------------------------------------------------------------
    /**
     * k@ʎq ̑O '-' 苎
     * LhaProperty ̃L[ lha.???.encoder / lha.???.decoder 
     *  ??? ɓ镶𐶐B
     * 
     * @param method k@ʎq
     * 
     * @return L[̒SɎg镶
     */
    private static String getCore( String method ){
        if( method.startsWith( "-" ) && method.endsWith( "-" ) ){
            return method.substring( 1, method.lastIndexOf( '-' ) ).toLowerCase();
        }else{
            throw new IllegalArgumentException( "" );
        }
    }

}
//end of CompressMethod.java