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