KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > oreilly > servlet > multipart > MacBinaryDecoderOutputStream


1 // Copyright (C) 1999-2001 by Jason Hunter <jhunter_AT_acm_DOT_org>.
2
// All rights reserved. Use of this class is limited.
3
// Please see the LICENSE for more information.
4

5 package com.oreilly.servlet.multipart;
6
7 import java.io.OutputStream JavaDoc;
8 import java.io.FilterOutputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10
11 /**
12  * A <code>MacBinaryDecoderOutput</code> filters MacBinary files to normal
13  * files on the fly; optimized for speed more than readability.
14  *
15  * @author Jason Hunter
16  */

17 public class MacBinaryDecoderOutputStream extends FilterOutputStream JavaDoc {
18   private int bytesFiltered = 0;
19   private int dataForkLength = 0;
20
21   public MacBinaryDecoderOutputStream(OutputStream JavaDoc out) {
22     super(out);
23   }
24
25   public void write(int b) throws IOException JavaDoc {
26     // Bytes 83 through 86 are a long representing the data fork length
27
// Check <= 86 first to short circuit early in the common case
28
if (bytesFiltered <= 86 && bytesFiltered >= 83) {
29       int leftShift = (86 - bytesFiltered) * 8;
30       dataForkLength = dataForkLength | (b & 0xff) << leftShift;
31     }
32
33     // Bytes 128 up to (128 + dataForkLength - 1) are the data fork
34
else if (bytesFiltered < (128 + dataForkLength) && bytesFiltered >= 128) {
35       out.write(b);
36     }
37
38     bytesFiltered++;
39   }
40
41   public void write(byte b[]) throws IOException JavaDoc {
42     write(b, 0, b.length);
43   }
44
45   public void write(byte b[], int off, int len) throws IOException JavaDoc {
46     // If the write is for content past the end of the data fork, ignore
47
if (bytesFiltered >= (128 + dataForkLength)) {
48       bytesFiltered += len;
49     }
50     // If the write is entirely within the data fork, write it directly
51
else if (bytesFiltered >= 128 &&
52              (bytesFiltered + len) <= (128 + dataForkLength)) {
53       out.write(b, off, len);
54       bytesFiltered += len;
55     }
56     // Otherwise, do the write a byte at a time to get the logic above
57
else {
58       for (int i = 0 ; i < len ; i++) {
59         write(b[off + i]);
60       }
61     }
62   }
63 }
64
Popular Tags