com.helger.commons.io.stream.StreamHelper Java Examples

The following examples show how to use com.helger.commons.io.stream.StreamHelper. 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: GZIPCodec.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
public void encode (@Nullable final byte [] aDecodedBuffer,
                    @Nonnegative final int nOfs,
                    @Nonnegative final int nLen,
                    @Nonnull @WillNotClose final OutputStream aOS)
{
  if (aDecodedBuffer == null || nLen == 0)
    return;

  try (final GZIPOutputStream aEncodeOS = new GZIPOutputStream (new NonClosingOutputStream (aOS)))
  {
    if (StreamHelper.copyInputStreamToOutputStream (new NonBlockingByteArrayInputStream (aDecodedBuffer, nOfs, nLen),
                                                    aEncodeOS)
                    .isFailure ())
      throw new EncodeException ("Failed to GZIP encode!");
  }
  catch (final IOException ex)
  {
    throw new EncodeException ("Failed to GZIP encode", ex);
  }
}
 
Example #2
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 #3
Source File: GraphVizHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Invoked the external process "neato" from the GraphViz package. Attention:
 * this spans a sub-process!
 *
 * @param sFileType
 *        The file type to be generated. E.g. "png" - see neato help for
 *        details. May neither be <code>null</code> nor empty.
 * @param sDOT
 *        The DOT file to be converted to an image. May neither be
 *        <code>null</code> nor empty.
 * @return The byte buffer that keeps the converted image. Never
 *         <code>null</code>.
 * @throws IOException
 *         In case some IO error occurs
 * @throws InterruptedException
 *         If the sub-process did not terminate correctly!
 */
@Nonnull
public static NonBlockingByteArrayOutputStream getGraphAsImageWithGraphVizNeato (@Nonnull @Nonempty final String sFileType,
                                                                                 @Nonnull final String sDOT) throws IOException,
                                                                                                             InterruptedException
{
  ValueEnforcer.notEmpty (sFileType, "FileType");
  ValueEnforcer.notEmpty (sDOT, "DOT");

  final ProcessBuilder aPB = new ProcessBuilder ("neato", "-T" + sFileType).redirectErrorStream (false);
  final Process p = aPB.start ();
  // Set neato stdin
  p.getOutputStream ().write (sDOT.getBytes (StandardCharsets.UTF_8));
  p.getOutputStream ().close ();
  // Read neato stdout
  final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream ();
  StreamHelper.copyInputStreamToOutputStream (p.getInputStream (), aBAOS);
  p.waitFor ();
  return aBAOS;
}
 
Example #4
Source File: SettingsPersistenceXML.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nonnull
public ESuccess writeSettings (@Nonnull final ISettings aSettings, @Nonnull @WillClose final OutputStream aOS)
{
  ValueEnforcer.notNull (aSettings, "Settings");
  ValueEnforcer.notNull (aOS, "OutputStream");

  try
  {
    // Inside try so that OS is closed
    ValueEnforcer.notNull (aSettings, "Settings");

    // No event manager invocation on writing
    final SettingsMicroDocumentConverter <T> aConverter = new SettingsMicroDocumentConverter <> (m_aSettingsFactory);
    final IMicroDocument aDoc = new MicroDocument ();
    aDoc.appendChild (aConverter.convertToMicroElement (GenericReflection.uncheckedCast (aSettings),
                                                        getWriteNamespaceURI (),
                                                        getWriteElementName ()));

    // auto-closes the stream
    return MicroWriter.writeToStream (aDoc, aOS, m_aXWS);
  }
  finally
  {
    StreamHelper.close (aOS);
  }
}
 
Example #5
Source File: MicroReader.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nullable
public static IMicroDocument readMicroXML (@WillClose @Nullable final InputStream aIS,
                                           @Nullable final ISAXReaderSettings aSettings)
{
  if (aIS == null)
    return null;

  try
  {
    return readMicroXML (InputSourceFactory.create (aIS), aSettings);
  }
  finally
  {
    StreamHelper.close (aIS);
  }
}
 
Example #6
Source File: MicroReader.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nullable
public static IMicroDocument readMicroXML (@WillClose @Nullable final Reader aReader,
                                           @Nullable final ISAXReaderSettings aSettings)
{
  if (aReader == null)
    return null;

  try
  {
    return readMicroXML (InputSourceFactory.create (aReader), aSettings);
  }
  finally
  {
    StreamHelper.close (aReader);
  }
}
 
Example #7
Source File: MicroWriter.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Write a Micro Node to a {@link Writer}.
 *
 * @param aNode
 *        The node to be serialized. May be any kind of node (incl.
 *        documents). May not be <code>null</code>.
 * @param aWriter
 *        The writer to write to. May not be <code>null</code>. The writer is
 *        closed anyway directly after the operation finishes (on success and
 *        on error).
 * @param aSettings
 *        The settings to be used for the creation. May not be
 *        <code>null</code>.
 * @return {@link ESuccess}
 */
@Nonnull
public static ESuccess writeToWriter (@Nonnull final IMicroNode aNode,
                                      @Nonnull @WillClose final Writer aWriter,
                                      @Nonnull final IXMLWriterSettings aSettings)
{
  ValueEnforcer.notNull (aNode, "Node");
  ValueEnforcer.notNull (aWriter, "Writer");
  ValueEnforcer.notNull (aSettings, "Settings");

  try
  {
    final MicroSerializer aSerializer = new MicroSerializer (aSettings);
    aSerializer.write (aNode, aWriter);
    return ESuccess.SUCCESS;
  }
  finally
  {
    StreamHelper.close (aWriter);
  }
}
 
Example #8
Source File: XMLMapHandler.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Write the passed map to the passed output stream using the predefined XML
 * layout.
 *
 * @param aMap
 *        The map to be written. May not be <code>null</code>.
 * @param aOS
 *        The output stream to write to. The stream is closed independent of
 *        success or failure. May not be <code>null</code>.
 * @return {@link ESuccess#SUCCESS} when everything went well,
 *         {@link ESuccess#FAILURE} otherwise.
 */
@Nonnull
public static ESuccess writeMap (@Nonnull final Map <String, String> aMap, @Nonnull @WillClose final OutputStream aOS)
{
  ValueEnforcer.notNull (aMap, "Map");
  ValueEnforcer.notNull (aOS, "OutputStream");

  try
  {
    final IMicroDocument aDoc = createMapDocument (aMap);
    return MicroWriter.writeToStream (aDoc, aOS, XMLWriterSettings.DEFAULT_XML_SETTINGS);
  }
  finally
  {
    StreamHelper.close (aOS);
  }
}
 
Example #9
Source File: XMLListHandler.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Write the passed collection to the passed output stream using the
 * predefined XML layout.
 *
 * @param aCollection
 *        The map to be written. May not be <code>null</code>.
 * @param aOS
 *        The output stream to write to. The stream is closed independent of
 *        success or failure. May not be <code>null</code>.
 * @return {@link ESuccess#SUCCESS} when everything went well,
 *         {@link ESuccess#FAILURE} otherwise.
 */
@Nonnull
public static ESuccess writeList (@Nonnull final Collection <String> aCollection,
                                  @Nonnull @WillClose final OutputStream aOS)
{
  ValueEnforcer.notNull (aCollection, "Collection");
  ValueEnforcer.notNull (aOS, "OutputStream");

  try
  {
    final IMicroDocument aDoc = createListDocument (aCollection);
    return MicroWriter.writeToStream (aDoc, aOS, XMLWriterSettings.DEFAULT_XML_SETTINGS);
  }
  finally
  {
    StreamHelper.close (aOS);
  }
}
 
Example #10
Source File: StringInputStreamProviderTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testBOM ()
{
  // BOM is emitted
  byte [] aBytes = StreamHelper.getAllBytes (new StringInputStreamProvider ("abc", StandardCharsets.UTF_16));
  assertArrayEquals (new byte [] { (byte) 0xfe, (byte) 0xff, 0, 'a', 0, 'b', 0, 'c' }, aBytes);

  // No BOM is emitted!
  aBytes = StreamHelper.getAllBytes (new StringInputStreamProvider ("abc", StandardCharsets.UTF_16BE));
  assertArrayEquals (new byte [] { 0, 'a', 0, 'b', 0, 'c' }, aBytes);

  // No BOM is emitted!
  aBytes = StreamHelper.getAllBytes (new StringInputStreamProvider ("abc", StandardCharsets.UTF_16LE));
  assertArrayEquals (new byte [] { 'a', 0, 'b', 0, 'c', 0 }, aBytes);

  // No BOM is emitted!
  aBytes = StreamHelper.getAllBytes (new StringInputStreamProvider ("abc", StandardCharsets.UTF_8));
  assertArrayEquals (new byte [] { 'a', 'b', 'c' }, aBytes);
}
 
Example #11
Source File: FileSystemByteStreamProviderTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testAll ()
{
  final FileSystemByteStreamProvider aFSSR = new FileSystemByteStreamProvider (new File ("."));
  final InputStream aIS = aFSSR.getInputStream ("pom.xml");
  assertNotNull (aIS);
  StreamHelper.close (aIS);

  final OutputStream aOS = aFSSR.getOutputStream ("$deleteme.txt", EAppend.DEFAULT);
  assertNotNull (aOS);
  StreamHelper.close (aOS);
  assertTrue (FileOperations.deleteFile (new File ("$deleteme.txt")).isSuccess ());

  CommonsTestHelper.testDefaultImplementationWithEqualContentObject (new FileSystemByteStreamProvider (new File (".")),
                                                                     new FileSystemByteStreamProvider (new File (".")));
  CommonsTestHelper.testDefaultImplementationWithEqualContentObject (new FileSystemByteStreamProvider (new File (".")),
                                                                     new FileSystemByteStreamProvider ("."));
  CommonsTestHelper.testDefaultImplementationWithDifferentContentObject (new FileSystemByteStreamProvider (new File (".")),
                                                                         new FileSystemByteStreamProvider (new File ("..")));
}
 
Example #12
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 #13
Source File: IJAXBWriter.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Write the passed object to a {@link Writer}.
 *
 * @param aObject
 *        The object to be written. May not be <code>null</code>.
 * @param aWriter
 *        The writer to write to. Will always be closed. May not be
 *        <code>null</code>.
 * @return {@link ESuccess}
 */
@Nonnull
default ESuccess write (@Nonnull final JAXBTYPE aObject, @Nonnull @WillClose final Writer aWriter)
{
  try
  {
    if (USE_JAXB_CHARSET_FIX)
    {
      return write (aObject, SafeXMLStreamWriter.create (aWriter, getXMLWriterSettings ()));
    }
    return write (aObject, TransformResultFactory.create (aWriter));
  }
  finally
  {
    // Needs to be manually closed
    StreamHelper.close (aWriter);
  }
}
 
Example #14
Source File: GZIPCodec.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
public void decode (@Nullable final byte [] aEncodedBuffer,
                    @Nonnegative final int nOfs,
                    @Nonnegative final int nLen,
                    @Nonnull @WillNotClose final OutputStream aOS)
{
  if (aEncodedBuffer == null || nLen == 0)
    return;

  try (final GZIPInputStream aDecodeIS = new GZIPInputStream (new NonBlockingByteArrayInputStream (aEncodedBuffer,
                                                                                                   nOfs,
                                                                                                   nLen)))
  {
    if (StreamHelper.copyInputStreamToOutputStream (aDecodeIS, aOS).isFailure ())
      throw new DecodeException ("Failed to GZIP decode!");
  }
  catch (final IOException ex)
  {
    throw new DecodeException ("Failed to GZIP encode", ex);
  }
}
 
Example #15
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 #16
Source File: FlateCodec.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
public void decode (@Nullable final byte [] aEncodedBuffer,
                    @Nonnegative final int nOfs,
                    @Nonnegative final int nLen,
                    @Nonnull @WillNotClose final OutputStream aOS)
{
  if (aEncodedBuffer == null || nLen == 0)
    return;

  if (!isZlibHead (aEncodedBuffer, nOfs, nLen))
    LOGGER.warn ("ZLib header not found");

  try (
      final InflaterInputStream aDecodeIS = new InflaterInputStream (new NonBlockingByteArrayInputStream (aEncodedBuffer,
                                                                                                          nOfs,
                                                                                                          nLen)))
  {
    if (StreamHelper.copyInputStreamToOutputStream (aDecodeIS, aOS).isFailure ())
      throw new DecodeException ("Failed to flate decode!");
  }
  catch (final IOException ex)
  {
    throw new DecodeException ("Failed to flate decode", ex);
  }
}
 
Example #17
Source File: FlateCodec.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
public void encode (@Nullable final byte [] aDecodedBuffer,
                    @Nonnegative final int nOfs,
                    @Nonnegative final int nLen,
                    @Nonnull @WillNotClose final OutputStream aOS)
{
  if (aDecodedBuffer == null || nLen == 0)
    return;

  try (final DeflaterOutputStream aEncodeOS = new DeflaterOutputStream (new NonClosingOutputStream (aOS)))
  {
    if (StreamHelper.copyInputStreamToOutputStream (new NonBlockingByteArrayInputStream (aDecodedBuffer, nOfs, nLen),
                                                    aEncodeOS)
                    .isFailure ())
      throw new EncodeException ("Failed to flate encode!");
  }
  catch (final IOException ex)
  {
    throw new EncodeException ("Failed to flate encode", ex);
  }
}
 
Example #18
Source File: URLHelperTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore ("Works only when being online")
public void testGetInputStream ()
{
  try
  {
    final InputStream aIS = URLHelper.getInputStream (new URL ("http://www.orf.at"), 3000, -1, null, null);
    final byte [] aContent = StreamHelper.getAllBytes (aIS);
    LOGGER.info ("Read " + aContent.length + " bytes");
  }
  catch (final Throwable t)
  {
    // ignore
    LOGGER.info ("Failed to GET: " + t.getMessage ());
  }
}
 
Example #19
Source File: CSSReader.java    From ph-css with Apache License 2.0 6 votes vote down vote up
/**
 * Check if the passed reader can be resembled to valid CSS content. This is
 * accomplished by fully parsing the CSS each time the method is called. This
 * is similar to calling
 * {@link #readFromStream(IHasInputStream, Charset, ECSSVersion)} and checking
 * for a non-<code>null</code> result.
 *
 * @param aReader
 *        The reader to use. May not be <code>null</code>.
 * @param eVersion
 *        The CSS version to use. May not be <code>null</code>.
 * @return <code>true</code> if the CSS is valid according to the version,
 *         <code>false</code> if not
 */
public static boolean isValidCSS (@Nonnull @WillClose final Reader aReader, @Nonnull final ECSSVersion eVersion)
{
  ValueEnforcer.notNull (aReader, "Reader");
  ValueEnforcer.notNull (eVersion, "Version");

  try
  {
    final CSSCharStream aCharStream = new CSSCharStream (aReader);
    final CSSNode aNode = _readStyleSheet (aCharStream,
                                           eVersion,
                                           getDefaultParseErrorHandler (),
                                           new DoNothingCSSParseExceptionCallback (),
                                           false);
    return aNode != null;
  }
  finally
  {
    StreamHelper.close (aReader);
  }
}
 
Example #20
Source File: FileSystemResourceTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testAccess ()
{
  FileSystemResource fr = new FileSystemResource ("pom.xml");
  assertTrue (fr.exists ());
  assertTrue (fr.getResourceID ().endsWith ("pom.xml"));
  assertTrue (fr.getPath ().endsWith ("pom.xml"));
  StreamHelper.close (fr.getReader (StandardCharsets.ISO_8859_1));
  final byte [] aBytes = StreamHelper.getAllBytes (fr);
  assertTrue (aBytes.length > 0);
  assertNotNull (fr.getAsURL ());
  assertNotNull (fr.getAsFile ());

  CommonsTestHelper.testDefaultImplementationWithEqualContentObject (fr, new FileSystemResource ("pom.xml"));
  CommonsTestHelper.testDefaultImplementationWithEqualContentObject (fr, fr.getReadableCloneForPath ("pom.xml"));
  CommonsTestHelper.testDefaultImplementationWithEqualContentObject (fr, fr.getWritableCloneForPath ("pom.xml"));
  CommonsTestHelper.testDefaultImplementationWithDifferentContentObject (fr, new FileSystemResource ("pom.xml2"));

  fr = new FileSystemResource ("this file does not exist");
  assertFalse (fr.exists ());
  assertNull (fr.getInputStream ());
  assertNull (fr.getReader (StandardCharsets.ISO_8859_1));
}
 
Example #21
Source File: JsonReader.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Read the Json from the passed {@link InputStream}.
 *
 * @param aIS
 *        The input stream to use. May not be <code>null</code>.
 * @param aFallbackCharset
 *        The charset to be used in case no BOM is present. May not be
 *        <code>null</code>.
 * @param aCustomExceptionCallback
 *        An optional custom exception handler that can be used to collect the
 *        unrecoverable parsing errors. May be <code>null</code>.
 * @return <code>null</code> if reading failed, the Json declarations
 *         otherwise.
 * @deprecated Since v9.3.3 - use {@link #builder()} instead.
 */
@Nullable
@Deprecated
public static IJson readFromStream (@Nonnull final InputStream aIS,
                                    @Nonnull final Charset aFallbackCharset,
                                    @Nullable final IJsonParseExceptionCallback aCustomExceptionCallback)
{
  try
  {
    return builder ().setSource (aIS, aFallbackCharset).setCustomExceptionCallback (aCustomExceptionCallback).read ();
  }
  finally
  {
    StreamHelper.close (aIS);
  }
}
 
Example #22
Source File: ClassPathResourceTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidRootWithPrefixLong ()
{
  final ClassPathResource aCPISP = new ClassPathResource ("classpath:test1.txt");
  assertTrue (aCPISP.exists ());
  assertNotNull (aCPISP.getReadableCloneForPath ("folder/test3-surely not existing.txt"));
  assertNotNull (aCPISP.getAsFile ());
  assertNotNull (aCPISP.getAsURL ());
  assertEquals ("test1.txt", aCPISP.getPath ());
  assertNotNull (aCPISP.getResourceID ());

  final InputStream aIS1 = aCPISP.getInputStream ();
  assertNotNull (aIS1);
  try
  {
    final InputStream aIS2 = aCPISP.getInputStream ();
    assertNotNull (aIS2);
    try
    {
      assertNotSame (aIS1, aIS2);
    }
    finally
    {
      StreamHelper.close (aIS2);
    }
  }
  finally
  {
    StreamHelper.close (aIS1);
  }
}
 
Example #23
Source File: SimpleFileIO.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static ESuccess writeFile (@Nonnull final File aFile,
                                  @Nonnull final String sContent,
                                  @Nonnull final Charset aCharset)
{
  final OutputStream aFOS = FileHelper.getOutputStream (aFile);
  return aFOS == null ? ESuccess.FAILURE : StreamHelper.writeStream (aFOS, sContent, aCharset);
}
 
Example #24
Source File: Issue35Test.java    From ph-css with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue ()
{
  final String css = StreamHelper.getAllBytesAsString (new FileSystemResource ("src/test/resources/testfiles/css30/good/issue35.css"),
                                                       StandardCharsets.UTF_8);
  final CSSReaderSettings aSettings = new CSSReaderSettings ().setCSSVersion (ECSSVersion.LATEST)
                                                              .setBrowserCompliantMode (false);
  final CascadingStyleSheet cascadingStyleSheet = CSSReader.readFromStringStream (css, aSettings);
  assertNotNull (cascadingStyleSheet);
  final CSSWriter writer = new CSSWriter (new CSSWriterSettings (ECSSVersion.LATEST, true));
  assertNotNull (writer.getCSSAsString (cascadingStyleSheet));
}
 
Example #25
Source File: FileHelperTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetInputStream ()
{
  try
  {
    FileHelper.getInputStream ((File) null);
    fail ();
  }
  catch (final NullPointerException ex)
  {}

  final InputStream aIS = FileHelper.getInputStream (new File ("pom.xml"));
  assertNotNull (aIS);
  StreamHelper.close (aIS);
}
 
Example #26
Source File: ClassPathResource.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
private void writeObject (@Nonnull final ObjectOutputStream aOOS) throws IOException
{
  if (m_aClassLoader != null)
    throw new IOException ("Cannot serialize a ClassPathResource that has a specific ClassLoader!");
  StreamHelper.writeSafeUTF (aOOS, m_sPath);
  // Don't write the rest! After serialization the URL must be resolved again!
}
 
Example #27
Source File: JavaClassLoaderFuncTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
private static void _assertNotNull (@WillClose final InputStream aIS)
{
  try
  {
    assertNotNull (aIS);
  }
  finally
  {
    StreamHelper.close (aIS);
  }
}
 
Example #28
Source File: ClassLoaderHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Get the input stream of the passed resource using the specified class
 * loader only. This is a sanity wrapper around
 * <code>classLoader.getResourceAsStream (sPath)</code>.
 *
 * @param aClassLoader
 *        The class loader to be used. May not be <code>null</code>.
 * @param sPath
 *        The path to be resolved. May neither be <code>null</code> nor empty.
 *        Internally it is ensured that the provided path does NOT start with
 *        a slash.
 * @return <code>null</code> if the path could not be resolved using the
 *         specified class loader.
 */
@Nullable
public static InputStream getResourceAsStream (@Nonnull final ClassLoader aClassLoader,
                                               @Nonnull @Nonempty final String sPath)
{
  ValueEnforcer.notNull (aClassLoader, "ClassLoader");
  ValueEnforcer.notEmpty (sPath, "Path");

  // Ensure the path does NOT starts with a "/"
  final String sPathWithoutSlash = _getPathWithoutLeadingSlash (sPath);

  // returns null if not found
  final InputStream aIS = aClassLoader.getResourceAsStream (sPathWithoutSlash);
  return StreamHelper.checkForInvalidFilterInputStream (aIS);
}
 
Example #29
Source File: ClassPathResourceTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidRootWithPrefixShort ()
{
  final ClassPathResource aCPISP = new ClassPathResource ("cp:test1.txt");
  assertTrue (aCPISP.exists ());
  assertNotNull (aCPISP.getReadableCloneForPath ("folder/test3-surely not existing.txt"));
  assertNotNull (aCPISP.getAsFile ());
  assertNotNull (aCPISP.getAsURL ());
  assertEquals ("test1.txt", aCPISP.getPath ());
  assertNotNull (aCPISP.getResourceID ());

  final InputStream aIS1 = aCPISP.getInputStream ();
  assertNotNull (aIS1);
  try
  {
    final InputStream aIS2 = aCPISP.getInputStream ();
    assertNotNull (aIS2);
    try
    {
      assertNotSame (aIS1, aIS2);
    }
    finally
    {
      StreamHelper.close (aIS2);
    }
  }
  finally
  {
    StreamHelper.close (aIS1);
  }
}
 
Example #30
Source File: CharsetHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static InputStreamReader getReaderByBOM (@Nonnull final InputStream aIS,
                                                @Nonnull final Charset aFallbackCharset)
{
  ValueEnforcer.notNull (aIS, "InputStream");
  ValueEnforcer.notNull (aFallbackCharset, "FallbackCharset");

  // Determine BOM/Charset
  final InputStreamAndCharset aISAndBOM = getInputStreamAndCharsetFromBOM (aIS);

  // Create the reader with the current position of the InputStream and the
  // correct charset
  final Charset aEffectiveCharset = aISAndBOM.getCharset (aFallbackCharset);
  return StreamHelper.createReader (aISAndBOM.getInputStream (), aEffectiveCharset);
}