001 /*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License"). You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at
010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012 * See the License for the specific language governing permissions
013 * and limitations under the License.
014 *
015 * When distributing Covered Code, include this CDDL HEADER in each
016 * file and include the License file at
017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
018 * add the following below this CDDL HEADER, with the fields enclosed
019 * by brackets "[]" replaced with your own identifying information:
020 * Portions Copyright [yyyy] [name of copyright owner]
021 *
022 * CDDL HEADER END
023 *
024 *
025 * Copyright 2006-2008 Sun Microsystems, Inc.
026 */
027 package org.opends.server.replication.plugin;
028
029
030
031 import java.io.IOException;
032 import java.io.InputStream;
033
034
035 /**
036 * This class creates an input stream that can be used to read entries generated
037 * by SynchroLDIF as if they were being read from another source like a file.
038 */
039 public class ReplLDIFInputStream
040 extends InputStream
041 {
042 // Indicates whether this input stream has been closed.
043 private boolean closed;
044
045 // The domain associated to this import.
046 ReplicationDomain domain;
047
048 private byte[] bytes;
049
050 private int index;
051
052 /**
053 * Creates a new ReplLDIFInputStream that will import entries
054 * for a synchronzation domain.
055 *
056 * @param domain The replication domain
057 */
058 public ReplLDIFInputStream(ReplicationDomain domain)
059 {
060 this.domain = domain;
061 closed = false;
062 }
063
064 /**
065 * Closes this input stream so that no more data may be read from it.
066 */
067 public void close()
068 {
069 closed = true;
070 }
071
072 /**
073 * Reads data from this input stream.
074 *
075 * @param b The array into which the data should be read.
076 * @param off The position in the array at which point the data read may be
077 * placed.
078 * @param len The maximum number of bytes that may be read into the
079 * provided array.
080 *
081 * @return The number of bytes read from the input stream into the provided
082 * array, or -1 if the end of the stream has been reached.
083 *
084 * @throws IOException If a problem has occurred while generating data for
085 * use by this input stream.
086 */
087 public int read(byte[] b, int off, int len)
088 throws IOException
089 {
090 if (closed)
091 return -1;
092
093 int receivedLength;
094 int copiedLength;
095
096 if (bytes == null)
097 {
098 // First time this method is called or the previous entry was
099 // finished. Read a new entry and return it.
100 bytes = domain.receiveEntryBytes();
101
102 if (bytes==null)
103 {
104 closed = true;
105 return -1;
106 }
107
108 receivedLength = bytes.length;
109 index = 0;
110 }
111 else
112 {
113 // Some data was left from the previous entry, feed the read with this
114 // data.
115 receivedLength = bytes.length - index;
116 }
117
118 if (receivedLength <= len)
119 {
120 copiedLength = receivedLength;
121 }
122 else
123 {
124 copiedLength = len;
125 }
126
127 for (int i =0; i<copiedLength; i++)
128 {
129 b[off+i] = bytes[index+i];
130 }
131 index += copiedLength;
132
133 if (index == bytes.length)
134 bytes = null;
135
136 return copiedLength;
137 }
138
139 /**
140 * Reads a single byte of data from this input stream.
141 *
142 * @return The byte read from the input stream, or -1 if the end of the
143 * stream has been reached.
144 *
145 * @throws IOException If a problem has occurred while generating data for
146 * use by this input stream.
147 */
148 public int read()
149 throws IOException
150 {
151 // This method is not supposed to be called to make an LDIF import
152 // for replication.
153 throw new IOException("Not implemented");
154 }
155 }
156