KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > UTF8BOMSkipper


1 /*
2  * (C) Copyright 2005, Andy Clark. All rights reserved.
3  *
4  * This file is distributed under an Apache style license. Please
5  * refer to the LICENSE file for specific details.
6  */

7  
8 package test;
9
10 import java.io.*;
11
12 /**
13  * This class is an input stream filter that skips the first
14  * three bytes read if they match the UTF-8 byte order mark,
15  * 0xEFBBBF. The UTF-8 BOM is most often generated by Windows®
16  * tools.
17  *
18  * @author Andy Clark
19  */

20 public class UTF8BOMSkipper
21     extends FilterInputStream {
22
23     //
24
// Data
25
//
26

27     /** Start of reading. */
28     private boolean fStart = true;
29
30     /** Byte offset. */
31     private int fOffset;
32
33     /** First three bytes. */
34     private int[] fFirst3Bytes;
35
36     //
37
// Constructors
38
//
39

40     /** Constructs a UTF-8 BOM skipper. */
41     public UTF8BOMSkipper(InputStream stream) {
42         super(stream);
43     } // <init>(InputStream)
44

45     //
46
// InputStream methods
47
//
48

49     /** Returns the next byte. */
50     public int read() throws IOException {
51
52         // read first three bytes in order to skip UTF-8 BOM, if present
53
if (fStart) {
54             fStart = false;
55             int b1 = super.read();
56             int b2 = super.read();
57             int b3 = super.read();
58             if (b1 != 0xEF || b2 != 0xBB || b3 != 0xBF) {
59                 fFirst3Bytes = new int[3];
60                 fFirst3Bytes[0] = b1;
61                 fFirst3Bytes[1] = b2;
62                 fFirst3Bytes[2] = b3;
63             }
64         }
65
66         // return read bytes
67
if (fFirst3Bytes != null) {
68             int b = fFirst3Bytes[fOffset++];
69             if (fOffset == fFirst3Bytes.length) {
70                 fFirst3Bytes = null;
71             }
72             return b;
73         }
74
75         // return next char
76
return super.read();
77
78     } // read():int
79

80     /** Reads bytes into specified buffer and returns total bytes read. */
81     public int read(byte[] buffer, int offset, int length) throws IOException {
82
83         if (fStart || fFirst3Bytes != null) {
84             for (int i = 0; i < length; i++) {
85                 int b = this.read();
86                 if (b == -1) {
87                     return i > 0 ? i : -1;
88                 }
89                 buffer[offset + i] = (byte)b;
90             }
91             return length;
92         }
93
94         return super.read(buffer, offset, length);
95
96     } // read(byte[],int,int):int
97

98     /** Mark is not supported for this input stream. */
99     public boolean markSupported() {
100         return false;
101     } // markSupported():boolean
102

103     /** Returns the number of bytes available. */
104     public int available() throws IOException {
105         if (fFirst3Bytes != null) {
106             return fFirst3Bytes.length - fOffset;
107         }
108         return super.available();
109     } // available():int
110

111 } // class UTF8BOMSkipper
112
Popular Tags