Java Code Examples for com.helger.commons.io.resource.IReadableResource#getInputStream()

The following examples show how to use com.helger.commons.io.resource.IReadableResource#getInputStream() . 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: BenchmarkLevenshteinDistance.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
private static ICommonsList <String> _readWordList (final IReadableResource aRes,
                                                    final Charset aCharset) throws IOException
{
  final ICommonsList <String> ret = new CommonsArrayList <> ();
  final NonBlockingBufferedReader aBR = new NonBlockingBufferedReader (new InputStreamReader (aRes.getInputStream (),
                                                                                              aCharset));
  String sLine;
  int nIdx = 0;
  while ((sLine = aBR.readLine ()) != null)
  {
    nIdx++;
    if ((nIdx % 3) == 0)
    {
      ret.add (sLine);
      if (ret.size () >= 100)
        break;
    }
  }
  StreamHelper.close (aBR);
  return ret;
}
 
Example 2
Source File: ConfigFileBuilder.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public ConfigFile build ()
{
  if (m_aPaths.isEmpty ())
    throw new IllegalStateException ("No config file path was provided!");

  IReadableResource aRes = null;
  ISettings aSettings = null;
  for (final String sConfigPath : m_aPaths)
  {
    // Support reading?
    if (m_aResProvider.supportsReading (sConfigPath))
    {
      // Convert to resource
      aRes = m_aResProvider.getReadableResource (sConfigPath);
      if (aRes != null)
      {
        // Open stream
        final InputStream aIS = aRes.getInputStream ();
        if (aIS != null)
        {
          // Read settings
          aSettings = m_aSPP.readSettings (aIS);
          if (aSettings != null)
            break;
        }
      }
    }
  }

  if (aSettings == null)
    if (LOGGER.isWarnEnabled ())
      LOGGER.warn ("Failed to resolve config file paths: " + m_aPaths);

  return new ConfigFile (aSettings != null ? aRes : null, aSettings);
}
 
Example 3
Source File: CachingTransformStreamSourceTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasic ()
{
  final IReadableResource aRes = new ClassPathResource ("xml/test1.xslt");
  assertTrue (aRes.exists ());
  CachingTransformStreamSource src = new CachingTransformStreamSource (aRes);
  InputStream is = src.getInputStream ();
  assertNotNull (is);
  StreamHelper.close (is);
  is = src.getInputStream ();
  assertNotNull (is);
  StreamHelper.close (is);
  assertEquals (aRes.getResourceID (), src.getSystemId ());
  assertNull (src.getPublicId ());

  src = new CachingTransformStreamSource ((IHasInputStream) aRes);
  is = src.getInputStream ();
  assertNotNull (is);
  StreamHelper.close (is);
  is = src.getInputStream ();
  assertNotNull (is);
  StreamHelper.close (is);
  assertNull (src.getSystemId ());
  assertNull (src.getPublicId ());

  src = new CachingTransformStreamSource (aRes.getInputStream ());
  is = src.getInputStream ();
  assertNotNull (is);
  StreamHelper.close (is);
  is = src.getInputStream ();
  assertNotNull (is);
  StreamHelper.close (is);
  assertNull (src.getSystemId ());
  assertNull (src.getPublicId ());

  CommonsTestHelper.testToStringImplementation (src);
}
 
Example 4
Source File: CachingSAXInputSourceTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasic ()
{
  final IReadableResource aRes = new ClassPathResource ("xml/list.xml");

  CachingSAXInputSource is = new CachingSAXInputSource (aRes);
  assertEquals (aRes.getResourceID (), is.getSystemId ());
  assertNotNull (StreamHelper.getAllBytes (is.getByteStream ()));

  is = new CachingSAXInputSource ((IHasInputStream) aRes);
  assertNull (is.getSystemId ());
  assertNotNull (StreamHelper.getAllBytes (is.getByteStream ()));

  is = new CachingSAXInputSource (aRes, "sysid");
  assertEquals ("sysid", is.getSystemId ());
  assertNotNull (StreamHelper.getAllBytes (is.getByteStream ()));

  is = new CachingSAXInputSource (aRes.getInputStream ());
  assertNull (is.getSystemId ());
  assertNotNull (StreamHelper.getAllBytes (is.getByteStream ()));

  is = new CachingSAXInputSource (aRes.getInputStream (), "sysid");
  assertEquals ("sysid", is.getSystemId ());
  assertNotNull (StreamHelper.getAllBytes (is.getByteStream ()));

  CommonsTestHelper.testToStringImplementation (is);
}
 
Example 5
Source File: PropertiesHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nullable
public static NonBlockingProperties loadProperties (@Nonnull final IReadableResource aRes)
{
  ValueEnforcer.notNull (aRes, "Resource");

  final InputStream aIS = aRes.getInputStream ();
  if (aIS == null)
    return null;
  return loadProperties (aIS);
}
 
Example 6
Source File: BenchmarkTrie.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
private static ICommonsList <String> _readWordList (final IReadableResource aRes,
                                                    final Charset aCharset) throws IOException
{
  final ICommonsList <String> ret = new CommonsArrayList <> ();
  final BufferedReader aBR = new BufferedReader (new InputStreamReader (aRes.getInputStream (), aCharset));
  String sLine;
  while ((sLine = aBR.readLine ()) != null)
  {
    ret.add (sLine);
    if (ret.size () > 999)
      break;
  }
  StreamHelper.close (aBR);
  return ret;
}
 
Example 7
Source File: CachingTransformStreamSource.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
public CachingTransformStreamSource (@Nonnull final IReadableResource aResource)
{
  this (aResource.getInputStream (), aResource.getResourceID ());
}
 
Example 8
Source File: CachingSAXInputSource.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
public CachingSAXInputSource (@Nonnull final IReadableResource aRes)
{
  this (aRes.getInputStream (), aRes.getResourceID ());
}