/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at legal-notices/CDDLv1_0.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2006-2009 Sun Microsystems, Inc.
* Portions Copyright 2015 ForgeRock AS
*/
/**
* This class creates an input stream that can be used to read entries generated
* by MakeLDIF as if they were being read from another source like a file. It
* has a fixed-size queue that dictates how many entries may be held in memory
* at any given time.
*/
public class MakeLDIFInputStream
extends InputStream
implements EntryWriter
{
/** Indicates whether all of the entries have been generated. */
private boolean allGenerated;
/** Indicates whether this input stream has been closed. */
private boolean closed;
/**
* The byte array output stream that will be used to convert entries to byte
* arrays with their LDIF representations.
*/
/**
* The byte array that will hold the LDIF representation of the next entry to
* be read.
*/
/** The IOException that should be thrown the next time a read is requested. */
/** The LDIF writer that will be used to write the entries to LDIF. */
/** The queue used to hold generated entries until they can be read. */
/** The background thread being used to actually generate the entries. */
/** The template file to use to generate the entries. */
/**
* Creates a new MakeLDIF input stream that will generate entries based on the
* provided template file.
*
* @param templateFile The template file to use to generate the entries.
*/
{
this.templateFile = templateFile;
allGenerated = false;
closed = false;
ioException = null;
entryBytes = null;
try
{
}
catch (IOException ioe)
{
// This should never happen.
ioException = ioe;
}
}
/**
* Closes this input stream so that no more data may be read from it.
*/
public void close()
{
closed = true;
ioException = null;
}
/**
* Reads a single byte of data from this input stream.
*
* @return The byte read from the input stream, or -1 if the end of the
* stream has been reached.
*
* @throws IOException If a problem has occurred while generating data for
* use by this input stream.
*/
public int read()
throws IOException
{
if (closed)
{
return -1;
}
else if (ioException != null)
{
throw ioException;
}
&& !getNextEntry())
{
closed = true;
return -1;
}
}
/**
* Reads data from this input stream.
*
* @param b The array into which the data should be read.
* @param off The position in the array at which point the data read may be
* placed.
* @param len The maximum number of bytes that may be read into the
* provided array.
*
* @return The number of bytes read from the input stream into the provided
* array, or -1 if the end of the stream has been reached.
*
* @throws IOException If a problem has occurred while generating data for
* use by this input stream.
*/
throws IOException
{
if (closed)
{
return -1;
}
else if (ioException != null)
{
throw ioException;
}
&& !getNextEntry())
{
closed = true;
return -1;
}
return bytesRead;
}
/** {@inheritDoc} */
throws IOException, MakeLDIFException
{
while (! closed)
{
try
{
{
return true;
}
} catch (InterruptedException ie) {}
}
return false;
}
/** {@inheritDoc} */
public void closeEntryWriter()
{
allGenerated = true;
}
/**
* Sets the I/O exception that should be thrown on any subsequent calls to
* <CODE>available</CODE> or <CODE>read</CODE>.
*
* @param ioException The I/O exception that should be thrown.
*/
{
this.ioException = ioException;
}
/**
* Retrieves the next entry and puts it in the entry byte buffer.
*
* @return <CODE>true</CODE> if the next entry is available, or
* <CODE>false</CODE> if there are no more entries or if the input
* stream has been closed.
*/
private boolean getNextEntry()
{
{
if (closed)
{
return false;
}
else if (allGenerated)
{
{
return false;
}
}
else
{
try
{
} catch (InterruptedException ie) {}
}
}
try
{
ldifWriter.flush();
}
catch (LDIFException le)
{
// This should never happen.
return false;
}
catch (IOException ioe)
{
// Neither should this.
ioException = ioe;
return false;
}
return true;
}
}