1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/net/fortuna/ical4j/data/FoldingWriter.java Tue Feb 10 18:12:00 2015 +0100 1.3 @@ -0,0 +1,155 @@ 1.4 +/** 1.5 + * Copyright (c) 2012, Ben Fortuna 1.6 + * All rights reserved. 1.7 + * 1.8 + * Redistribution and use in source and binary forms, with or without 1.9 + * modification, are permitted provided that the following conditions 1.10 + * are met: 1.11 + * 1.12 + * o Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 1.15 + * o Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 1.19 + * o Neither the name of Ben Fortuna nor the names of any other contributors 1.20 + * may be used to endorse or promote products derived from this software 1.21 + * without specific prior written permission. 1.22 + * 1.23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.24 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.25 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.26 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 1.27 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.28 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.29 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.30 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.31 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.32 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.33 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.34 + */ 1.35 +package net.fortuna.ical4j.data; 1.36 + 1.37 +import java.io.FilterWriter; 1.38 +import java.io.IOException; 1.39 +import java.io.Writer; 1.40 + 1.41 +import org.apache.commons.logging.Log; 1.42 +import org.apache.commons.logging.LogFactory; 1.43 + 1.44 +/** 1.45 + * <pre> 1.46 + * $Id$ [Apr 6, 2004] 1.47 + * </pre> 1.48 + * 1.49 + * A writer that performs iCalendar folding as it writes. 1.50 + * @author Ben Fortuna 1.51 + */ 1.52 +public class FoldingWriter extends FilterWriter { 1.53 + 1.54 + /** 1.55 + * reduced to 73 to be consistent with Apple iCal.. 1.56 + */ 1.57 + public static final int REDUCED_FOLD_LENGTH = 73; 1.58 + 1.59 + /** 1.60 + * Lines of text SHOULD NOT be longer than 75 octets, excluding the line break. 1.61 + */ 1.62 + public static final int MAX_FOLD_LENGTH = 75; 1.63 + 1.64 + private static final char[] FOLD_PATTERN = { '\r', '\n', ' ' }; 1.65 + 1.66 + private final Log log = LogFactory.getLog(FoldingWriter.class); 1.67 + 1.68 + private int lineLength; 1.69 + 1.70 + private final int foldLength; 1.71 + 1.72 + /** 1.73 + * @param writer a writer to write output to 1.74 + * @param foldLength the maximum line length 1.75 + */ 1.76 + public FoldingWriter(final Writer writer, final int foldLength) { 1.77 + super(writer); 1.78 + this.foldLength = Math.min(foldLength, MAX_FOLD_LENGTH); 1.79 + } 1.80 + 1.81 + /** 1.82 + * @param writer a writer to write output to 1.83 + */ 1.84 + public FoldingWriter(final Writer writer) { 1.85 + this(writer, REDUCED_FOLD_LENGTH); 1.86 + } 1.87 + 1.88 + /** 1.89 + * {@inheritDoc} 1.90 + */ 1.91 + public final void write(final int c) throws IOException { 1.92 + 1.93 + /* 1.94 + * super.write(c); if (c == '\n') { lineLength = 0; } else { lineLength += 1; } if (lineLength >= FOLD_LENGTH) { 1.95 + * super.write(FOLD_PATTERN); } 1.96 + */ 1.97 + write(new char[] { (char) c }, 0, 1); 1.98 + } 1.99 + 1.100 + /** 1.101 + * {@inheritDoc} 1.102 + */ 1.103 + public final void write(final char[] buffer, final int offset, 1.104 + final int length) throws IOException { 1.105 + final int maxIndex = offset + length - 1; 1.106 + for (int i = offset; i <= maxIndex; i++) { 1.107 + 1.108 + // debugging.. 1.109 + if (log.isTraceEnabled()) { 1.110 + log.trace("char [" + buffer[i] + "], line length [" 1.111 + + lineLength + "]"); 1.112 + } 1.113 + 1.114 + // check for fold first so we don't unnecessarily fold after 1.115 + // no more data.. 1.116 + if (lineLength >= foldLength) { 1.117 + super.write(FOLD_PATTERN, 0, FOLD_PATTERN.length); 1.118 + 1.119 + // re-initialise to 1 to account for the space in fold pattern.. 1.120 + lineLength = 1; 1.121 + } 1.122 + 1.123 + super.write(buffer[i]); 1.124 + 1.125 + if (buffer[i] == '\r' || buffer[i] == '\n') { 1.126 + lineLength = 0; 1.127 + } 1.128 + else { 1.129 + lineLength += 1; 1.130 + } 1.131 + } 1.132 + } 1.133 + 1.134 + /** 1.135 + * {@inheritDoc} 1.136 + */ 1.137 + public final void write(final String str, final int off, final int len) 1.138 + throws IOException { 1.139 + write(str.toCharArray(), off, len); 1.140 + } 1.141 + 1.142 + /* 1.143 + * (non-Javadoc) 1.144 + * @see java.io.FilterWriter#write(java.lang.String, int, int) public void write(String arg0, int arg1, int arg2) 1.145 + * throws IOException { super.write(arg0, arg1, arg2); if (arg0.indexOf('\n') >= 0) { lineLength = 0; } else { 1.146 + * lineLength += 1; } fold(); } 1.147 + */ 1.148 + 1.149 + /* 1.150 + * (non-Javadoc) 1.151 + * @see java.io.Writer#write(java.lang.String) public void write(String arg0) throws IOException { 1.152 + * /* if (lineLength + 1.153 + * arg0.length() >= FOLD_LENGTH) { super.write(arg0.substring(0,FOLD_LENGTH-lineLength-1)); 1.154 + * super.write(FOLD_PATTERN); super.write(arg0.substring(FOLD_LENGTH-lineLength)); } else { super.write(arg0); } if 1.155 + * (arg0.indexOf('\n') >= 0) { lineLength = 0; } else { lineLength += 1; } fold(); char[] chars = 1.156 + * arg0.toCharArray(); for (int i=0; i <chars.length; i++) { write(chars[i]); } } 1.157 + */ 1.158 +}