View Javadoc
1   /*
2    * Copyright 2013-2016 Brian Thomas Matthews
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.btmatthews.maven.plugins.ldap.ldif;
18  
19  import com.btmatthews.maven.plugins.ldap.FormatReader;
20  import com.unboundid.ldif.LDIFChangeRecord;
21  import com.unboundid.ldif.LDIFException;
22  import com.unboundid.ldif.LDIFReader;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  
27  /**
28   * This {@link FormatReader} reads LDIF change records from an underlying input stream.
29   *
30   * @author <a href="mailto:brian@btmatthews.com">Brian Matthews</a>
31   * @since 1.2.0
32   */
33  public final class LDIFFormatReader implements FormatReader {
34  
35      /**
36       * The object that reads and parses LDIF change records from the
37       * underlying input stream.
38       */
39      private final LDIFReader reader;
40  
41      /**
42       * Initialize the {@link LDIFFormatReader} by creating an {@link LDIFReader} that
43       * will read and parse LDIF change records from the {@code inputStream}.
44       *
45       * @param inputStream The underlying input stream.
46       */
47      public LDIFFormatReader(final InputStream inputStream) {
48          reader = new LDIFReader(inputStream);
49      }
50  
51      /**
52       * Read the next change record from the underlying LDIF input stream.
53       *
54       * @return The next change record or {@code null} if the end of the input stream has been reached.
55       * @throws IOException   If there was an error reading from the input stream.
56       * @throws LDIFException If there was an error parsing the data read from the input stream.
57       */
58      public LDIFChangeRecord nextRecord() throws IOException, LDIFException {
59          return reader.readChangeRecord();
60      }
61  
62      /**
63       * Close the {@link LDIFFormatReader} by closing the {@link LDIFReader} object used to
64       * read and parse the LDIF change records.
65       *
66       * @throws IOException If there was a problem closing the {@link LDIFReader}.
67       */
68      public void close() throws IOException {
69          reader.close();
70      }
71  }