View Javadoc

1   /*
2    * $Id: FoldingWriter.java [Apr 6, 2004]
3    *
4    * Copyright (c) 2004, Ben Fortuna
5    * All rights reserved.
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions
9    * are met:
10   *
11   * 	o Redistributions of source code must retain the above copyright
12   * notice, this list of conditions and the following disclaimer.
13   *
14   * 	o Redistributions in binary form must reproduce the above copyright
15   * notice, this list of conditions and the following disclaimer in the
16   * documentation and/or other materials provided with the distribution.
17   *
18   * 	o Neither the name of Ben Fortuna nor the names of any other contributors
19   * may be used to endorse or promote products derived from this software
20   * without specific prior written permission.
21   *
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
26   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   */
34  package net.fortuna.ical4j.data;
35  
36  import java.io.FilterWriter;
37  import java.io.IOException;
38  import java.io.Writer;
39  
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  /***
44   * A writer that performs iCalendar folding as it writes.
45   *
46   * @author benf
47   */
48  public class FoldingWriter extends FilterWriter {
49  
50      // Lines of text SHOULD NOT be longer than 75 octets, excluding the line break.
51      // reduced to 73 to be consistent with iCal..
52      private static final int FOLD_LENGTH = 73;
53  
54      private static final char[] FOLD_PATTERN = {'\r', '\n', ' '};
55  
56      private static Log log = LogFactory.getLog(FoldingWriter.class);
57  
58      private int lineLength;
59  
60      /***
61       * @param arg0 a writer to write output to
62       */
63      public FoldingWriter(final Writer arg0) {
64          super(arg0);
65  
66          lineLength = 0;
67      }
68  
69      /*
70       * (non-Javadoc)
71       *
72       * @see java.io.FilterWriter#write(int)
73       *
74       */
75      public final void write(final int c) throws IOException {
76  
77          /*
78          super.write(c);
79  
80          if (c == '\n') {
81              lineLength = 0;
82          }
83          else {
84              lineLength += 1;
85          }
86  
87          if (lineLength >= FOLD_LENGTH) {
88              super.write(FOLD_PATTERN);
89          }
90          */
91          write(new char[] {(char) c}, 0, 1);
92      }
93  
94  
95      /* (non-Javadoc)
96       * @see java.io.FilterWriter#write(char[], int, int)
97       */
98      public void write(char[] buffer, int offset, int length) throws IOException {
99          for (int i = offset; i < length; i++) {
100 
101             // debugging..
102             log.debug("char [" + buffer[i] + "], line length [" + lineLength + "]");
103 
104             // check for fold first so we don't unnecessarily fold after
105             // no more data..
106             if (lineLength > FOLD_LENGTH) {
107                 super.write(FOLD_PATTERN, 0, FOLD_PATTERN.length);
108 
109                 // re-initialise to 1 to account for the space in fold pattern..
110                 lineLength = 1;
111             }
112 
113             super.write(new char[] {buffer[i]}, 0, 1);
114 
115             if (buffer[i] == '\n') {
116                 lineLength = 0;
117             }
118             else {
119                 lineLength += 1;
120             }
121         }
122     }
123 
124 
125     /* (non-Javadoc)
126      * @see java.io.FilterWriter#write(java.lang.String, int, int)
127      */
128     public void write(String str, int off, int len) throws IOException {
129         write(str.toCharArray(), off, len);
130     }
131 
132     /*
133      * (non-Javadoc)
134      *
135      * @see java.io.FilterWriter#write(java.lang.String, int, int)
136      *
137      * public void write(String arg0, int arg1, int arg2) throws IOException {
138      *
139      * super.write(arg0, arg1, arg2);
140      *
141      * if (arg0.indexOf('\n') >= 0) {
142      *
143      * lineLength = 0; } else {
144      *
145      * lineLength += 1; }
146      *
147      * fold(); }
148      */
149 
150     /*
151      * (non-Javadoc)
152      *
153      * @see java.io.Writer#write(java.lang.String)
154      *
155      * public void write(String arg0) throws IOException {
156      *  /* if (lineLength + arg0.length() >= FOLD_LENGTH) {
157      *
158      * super.write(arg0.substring(0,FOLD_LENGTH-lineLength-1));
159      *
160      * super.write(FOLD_PATTERN);
161      *
162      * super.write(arg0.substring(FOLD_LENGTH-lineLength)); } else {
163      *
164      * super.write(arg0); }
165      *
166      * if (arg0.indexOf('\n') >= 0) {
167      *
168      * lineLength = 0; } else {
169      *
170      * lineLength += 1; }
171      *
172      * fold();
173      *
174      *
175      * char[] chars = arg0.toCharArray();
176      *
177      * for (int i=0; i <chars.length; i++) {
178      *
179      * write(chars[i]); } }
180      */
181 }