KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > util > UnicodeFilterWriter


1 /*
2  * NEMESIS-FORUM.
3  * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  *
7  * Copyright (C) 2001 Yasna.com. All rights reserved.
8  *
9  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
10  *
11  * NEMESIS-FORUM. is free software; you can redistribute it and/or
12  * modify it under the terms of the Apache Software License, Version 1.1,
13  * or (at your option) any later version.
14  *
15  * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
16  * application are parts of NEMESIS-FORUM and are distributed under
17  * same terms of licence.
18  *
19  *
20  * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
21  * and software developed by CoolServlets.com (http://www.coolservlets.com).
22  * and software developed by Yasna.com (http://www.yasna.com).
23  *
24  */

25
26 package org.nemesis.forum.util;
27
28 import java.io.IOException JavaDoc;
29 import java.io.Writer JavaDoc;
30
31 /**
32  * Class for writing non-null unicode characters to character streams.<p>
33  *
34  * Example use:<p>
35  *
36  * <code>Writer out = new UnicodeWriter(new FileWriter("blah.txt"));</code>
37  */

38 public class UnicodeFilterWriter extends Writer JavaDoc {
39
40     // Underlying writer
41
private Writer JavaDoc out;
42
43     /**
44      * Create a new UnicodeWriter
45      *
46      * @param out A Writer.
47      */

48     public UnicodeFilterWriter( Writer JavaDoc out ) {
49         super(out);
50         this.out = out;
51     }
52
53     /**
54      * Check each character in the character array for the null character. If
55      * the null character is found ignore it (don't write to underlying character
56      * stream).
57      *
58      * @param cbuf The character array
59      * @param offset Offset from which to start writing characters
60      * @param length Number of characters to write
61      */

62     public void write(char[] cbuf, int offset, int length) throws IOException JavaDoc {
63         synchronized( lock ) {
64             int loopEnd = (offset+length);
65             for( int i=offset; i<loopEnd; i++ ) {
66                 switch(cbuf[i]) {
67                     // null character case
68
case '\u0000':
69                         break;
70                     case '\u0018':
71                         break;
72                     // everything else
73
default:
74                         out.write(cbuf[i]);
75                         break;
76                 }
77             }
78         }
79     }
80
81     /**
82      * Flush the stream
83      */

84     public void flush() throws IOException JavaDoc {
85         synchronized (lock) {
86             out.flush();
87         }
88     }
89
90     /**
91      * Close the stream
92      */

93     public void close() throws IOException JavaDoc {
94         synchronized (lock) {
95             if (out == null) {
96                 return;
97             }
98             out.close();
99             out = null;
100         }
101     }
102
103 }
104
Popular Tags