View Javadoc
1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2009  Mickael Guessant
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18   */
19  package davmail.exchange;
20  
21  import java.io.BufferedReader;
22  import java.io.IOException;
23  import java.io.Reader;
24  
25  /**
26   * ICS Buffered Reader.
27   * Read events by line, handle multiple line elements
28   */
29  public class ICSBufferedReader extends BufferedReader {
30      protected String nextLine;
31      protected final StringBuilder currentLine = new StringBuilder(75);
32  
33      /**
34       * Create an ICS reader on the provided reader
35       *
36       * @param in input reader
37       * @throws IOException on error
38       */
39      public ICSBufferedReader(Reader in) throws IOException {
40          super(in);
41          nextLine = super.readLine();
42      }
43  
44      /**
45       * Read a line from input reader, unwrap long lines.
46       */
47      @Override
48      public String readLine() throws IOException {
49          if (nextLine == null) {
50              return null;
51          } else {
52              currentLine.setLength(0);
53              currentLine.append(nextLine);
54              nextLine = super.readLine();
55              while (nextLine != null && !(nextLine.length() == 0) &&
56                      (nextLine.charAt(0) == ' ' || nextLine.charAt(0) == '\t'
57                              // workaround for broken items with \n as first line character
58                              || nextLine.charAt(0) == '\\'
59                              // workaround for Exchange 2010 bug
60                              || nextLine.charAt(0) == ':')) {
61                  // Timezone ends with \n => next line starts with :
62                  if (nextLine.charAt(0) == ':') {
63                      currentLine.append(nextLine);
64                  } else {
65                      currentLine.append(nextLine.substring(1));
66                  }
67                  nextLine = super.readLine();
68              }
69              return currentLine.toString();
70          }
71      }
72  }