Java Code Examples for com.helger.commons.io.stream.StreamHelper#getBuffered()

The following examples show how to use com.helger.commons.io.stream.StreamHelper#getBuffered() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XMLResourceBundleControl.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Override
public ResourceBundle newBundle (@Nonnull final String sBaseName,
                                 @Nonnull final Locale aLocale,
                                 @Nonnull final String sFormat,
                                 @Nonnull final ClassLoader aClassLoader,
                                 final boolean bReload) throws IOException
{
  ValueEnforcer.notNull (sBaseName, "BaseName");
  ValueEnforcer.notNull (aLocale, "Locale");
  ValueEnforcer.notNull (sFormat, "Format");
  ValueEnforcer.notNull (aClassLoader, "ClassLoader");

  // We can only handle XML
  if (sFormat.equals (FORMAT_XML))
  {
    final String sBundleName = toBundleName (sBaseName, aLocale);
    final String sResourceName = toResourceName (sBundleName, sFormat);
    final URL aResourceUrl = ClassLoaderHelper.getResource (aClassLoader, sResourceName);
    if (aResourceUrl != null)
      try (final InputStream aIS = StreamHelper.getBuffered (URLResource.getInputStream (aResourceUrl)))
      {
        return new XMLResourceBundle (aIS);
      }
  }
  return null;
}
 
Example 2
Source File: JsonReader.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Set a {@link Reader} as JSON source. Internally it is ensured, that it is
 * buffered.
 *
 * @param aReader
 *        The Reader to be used. May not be <code>null</code>.
 * @return this for chaining
 */
@Nonnull
public Builder setSource (@Nonnull @WillClose final Reader aReader)
{
  ValueEnforcer.notNull (aReader, "Reader");
  if (m_aReader != null)
    LOGGER.warn ("Another source is already present - this may cause a resource leak, because the old source is not closed automatically");

  m_aReader = aReader;

  // Use buffered?
  if (m_bUseBufferedReader)
    m_aReader = StreamHelper.getBuffered (m_aReader);

  // Don't close?
  if (m_bDontCloseSource)
    m_aReader = new NonClosingReader (m_aReader);
  return this;
}
 
Example 3
Source File: PropertiesHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nullable
public static NonBlockingProperties loadProperties (@Nonnull @WillClose final InputStream aIS)
{
  ValueEnforcer.notNull (aIS, "InputStream");

  final InputStream aBufferedIS = StreamHelper.getBuffered (aIS);
  try
  {
    final NonBlockingProperties aProps = new NonBlockingProperties ();
    aProps.load (aBufferedIS);
    return aProps;
  }
  catch (final IOException ex)
  {
    return null;
  }
  finally
  {
    StreamHelper.close (aBufferedIS);
    StreamHelper.close (aIS);
  }
}
 
Example 4
Source File: PropertiesHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nullable
public static NonBlockingProperties loadProperties (@Nonnull @WillClose final Reader aReader)
{
  ValueEnforcer.notNull (aReader, "Reader");

  final Reader aBufferedReader = StreamHelper.getBuffered (aReader);
  try
  {
    final NonBlockingProperties aProps = new NonBlockingProperties ();
    aProps.load (aBufferedReader);
    return aProps;
  }
  catch (final IOException ex)
  {
    return null;
  }
  finally
  {
    StreamHelper.close (aBufferedReader);
    StreamHelper.close (aReader);
  }
}
 
Example 5
Source File: CSVReader.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs {@link CSVReader} with supplied {@link CSVParser}.
 *
 * @param aReader
 *        the reader to an underlying CSV source.
 * @param aParser
 *        the parser to use to parse input
 * @param bKeepCR
 *        <code>true</code> to keep carriage returns in data read,
 *        <code>false</code> otherwise
 */
public CSVReader (@Nonnull @WillCloseWhenClosed final Reader aReader,
                  @Nonnull final CSVParser aParser,
                  final boolean bKeepCR)
{
  ValueEnforcer.notNull (aReader, "Reader");
  ValueEnforcer.notNull (aParser, "Parser");

  Reader aInternallyBufferedReader = StreamHelper.getBuffered (aReader);
  if (bKeepCR)
    m_aLineReader = new CSVLineReaderKeepCR (aInternallyBufferedReader);
  else
  {
    if (!(aInternallyBufferedReader instanceof NonBlockingBufferedReader))
    {
      // It is buffered, but we need it to support readLine
      aInternallyBufferedReader = new NonBlockingBufferedReader (aInternallyBufferedReader);
    }
    m_aLineReader = new CSVLineReaderNonBlockingBufferedReader ((NonBlockingBufferedReader) aInternallyBufferedReader);
  }
  m_aReader = aInternallyBufferedReader;
  m_aParser = aParser;
  m_bKeepCR = bKeepCR;
}
 
Example 6
Source File: CSSCharStream.java    From ph-css with Apache License 2.0 6 votes vote down vote up
private CSSCharStream (@Nonnull final Reader aReader,
                       @Nonnegative final int nStartLine,
                       @Nonnegative final int nStartColumn,
                       @Nonnegative final int nBufferSize)
{
  ValueEnforcer.isGE0 (nBufferSize, "BufferSize");
  // Using a buffered reader gives a minimal speedup
  m_aReader = StreamHelper.getBuffered (ValueEnforcer.notNull (aReader, "Reader"));
  m_nLine = ValueEnforcer.isGE0 (nStartLine, "StartLine");
  m_nColumn = ValueEnforcer.isGE0 (nStartColumn, "StartColumn") - 1;

  m_nAvailable = nBufferSize;
  m_nBufsize = nBufferSize;
  m_aBuffer = new char [nBufferSize];
  m_aBufLine = new int [nBufferSize];
  m_aBufColumn = new int [nBufferSize];
  m_aNextCharBuf = new char [DEFAULT_BUF_SIZE];
}
 
Example 7
Source File: CSSTokenizer.java    From ph-css with Apache License 2.0 5 votes vote down vote up
public static void main (final String [] args) throws IOException, CSSTokenizeException
{
  final File f = new File ("src/test/resources/testfiles/css30/good/pure-min.css");
  try (InputStream aIS = StreamHelper.getBuffered (FileHelper.getInputStream (f)))
  {
    new CSSTokenizer ().setDebugMode (false).tokenize (aIS, t -> {
      System.out.println (t);
    });
  }
}
 
Example 8
Source File: CharsetHelper.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
/**
 * If a BOM is present in the {@link InputStream} it is read and if possible
 * the charset is automatically determined from the BOM.
 *
 * @param aIS
 *        The input stream to use. May not be <code>null</code>.
 * @return Never <code>null</code>. Always use the input stream contained in
 *         the returned object and never the one passed in as a parameter,
 *         because the returned IS is a push-back InputStream that has a
 *         couple of bytes already buffered!
 */
@Nonnull
public static InputStreamAndCharset getInputStreamAndCharsetFromBOM (@Nonnull @WillNotClose final InputStream aIS)
{
  ValueEnforcer.notNull (aIS, "InputStream");

  // Check for BOM
  final int nMaxBOMBytes = EUnicodeBOM.getMaximumByteCount ();
  @WillNotClose
  final NonBlockingPushbackInputStream aPIS = new NonBlockingPushbackInputStream (StreamHelper.getBuffered (aIS),
                                                                                  nMaxBOMBytes);
  try
  {
    // Try to read as many bytes as necessary to determine all supported BOMs
    final byte [] aBOM = new byte [nMaxBOMBytes];
    final int nReadBOMBytes = aPIS.read (aBOM);
    EUnicodeBOM eBOM = null;
    Charset aDeterminedCharset = null;
    if (nReadBOMBytes > 0)
    {
      // Some byte BOMs were read - determine
      eBOM = EUnicodeBOM.getFromBytesOrNull (ArrayHelper.getCopy (aBOM, 0, nReadBOMBytes));
      if (eBOM == null)
      {
        // Unread the whole BOM
        aPIS.unread (aBOM, 0, nReadBOMBytes);
        // aDeterminedCharset stays null
      }
      else
      {
        if (LOGGER.isDebugEnabled ())
          LOGGER.debug ("Found " + eBOM + " on " + aIS.getClass ().getName ());

        // Unread the unnecessary parts of the BOM
        final int nBOMBytes = eBOM.getByteCount ();
        if (nBOMBytes < nReadBOMBytes)
          aPIS.unread (aBOM, nBOMBytes, nReadBOMBytes - nBOMBytes);

        // Use the Charset of the BOM - maybe null!
        aDeterminedCharset = eBOM.getCharset ();
      }
    }
    return new InputStreamAndCharset (aPIS, eBOM, aDeterminedCharset);
  }
  catch (final IOException ex)
  {
    LOGGER.error ("Failed to determine BOM", ex);
    StreamHelper.close (aPIS);
    throw new UncheckedIOException (ex);
  }
}
 
Example 9
Source File: CSSInputStream.java    From ph-css with Apache License 2.0 4 votes vote down vote up
public CSSInputStream (@Nonnull final InputStream aIS)
{
  super (StreamHelper.getBuffered (aIS), 1024);
  ValueEnforcer.notNull (aIS, "InputStream");
}
 
Example 10
Source File: IHasOutputStream.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get the output stream to read from the object. Each time this method is
 * call, a new {@link OutputStream} needs to be created. Internally invokes
 * {@link #getOutputStream(EAppend)}.
 *
 * @param eAppend
 *        appending mode. May not be <code>null</code>.
 * @return <code>null</code> if resolving failed.
 * @since 9.1.8
 */
@Nullable
default OutputStream getBufferedOutputStream (@Nonnull final EAppend eAppend)
{
  final OutputStream aOS = getOutputStream (eAppend);
  return aOS == null ? null : StreamHelper.getBuffered (aOS);
}
 
Example 11
Source File: IHasWriter.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get a buffered writer to write to an object. Each time this method is call,
 * a new {@link Writer} needs to be created!
 *
 * @return <code>null</code> if resolving failed.
 * @since 9.1.8
 */
@Nullable
default Writer getBufferedWriter ()
{
  final Writer aWriter = getWriter ();
  return aWriter == null ? null : StreamHelper.getBuffered (aWriter);
}
 
Example 12
Source File: IHasReader.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get a buffered reader to read from the object. Each time this method is
 * call, a new {@link Reader} needs to be created!
 *
 * @return <code>null</code> if resolving failed.
 * @since 9.1.8
 */
@Nullable
default Reader getBufferedReader ()
{
  final Reader aReader = getReader ();
  return aReader == null ? null : StreamHelper.getBuffered (aReader);
}
 
Example 13
Source File: IHasInputStream.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get a buffered input stream to read from the object. Each time this method
 * is called, a new {@link InputStream} needs to be created. Internally
 * invokes {@link #getInputStream()}.
 *
 * @return <code>null</code> if resolving failed.
 * @since 9.1.8
 */
@Nullable
default InputStream getBufferedInputStream ()
{
  final InputStream aIS = getInputStream ();
  return aIS == null ? null : StreamHelper.getBuffered (aIS);
}
 
Example 14
Source File: IHasOutputStreamAndWriter.java    From ph-commons with Apache License 2.0 2 votes vote down vote up
/**
 * Get a buffered {@link Writer} based on this output stream provider using
 * the given charset.
 *
 * @param aCharset
 *        The charset to use. May not be <code>null</code>.
 * @param eAppend
 *        Appending mode. May not be <code>null</code>.
 * @return <code>null</code> if no output stream could be retrieved.
 * @since 9.1.8
 */
@Nullable
default Writer getBufferedWriter (@Nonnull final Charset aCharset, @Nonnull final EAppend eAppend)
{
  return StreamHelper.getBuffered (getWriter (aCharset, eAppend));
}
 
Example 15
Source File: IHasInputStreamAndReader.java    From ph-commons with Apache License 2.0 2 votes vote down vote up
/**
 * Get a buffered {@link Reader} based on this input stream provider using the
 * given charset.
 *
 * @param aCharset
 *        The charset to use. May not be <code>null</code>.
 * @return <code>null</code> if no input stream could be retrieved.
 * @since 9.1.8
 */
@Nullable
default Reader getBufferedReader (@Nonnull final Charset aCharset)
{
  return StreamHelper.getBuffered (getReader (aCharset));
}