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

The following examples show how to use com.helger.commons.io.stream.StreamHelper#getAllBytes() . 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: Base64Test.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncodeWithBreakLines ()
{
  final byte [] aSource = StringHelper.getRepeated ('a', 100).getBytes (StandardCharsets.ISO_8859_1);
  String sEncoded = Base64.safeEncodeBytes (aSource, Base64.DO_BREAK_LINES);
  assertEquals ("YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFh\n" +
                "YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYQ==",
                sEncoded);

  // Check that it can be read again
  byte [] aReadBytes = StreamHelper.getAllBytes (new Base64InputStream (new NonBlockingByteArrayInputStream (sEncoded.getBytes (Base64.PREFERRED_ENCODING))));
  assertArrayEquals (aSource, aReadBytes);

  sEncoded = Base64.safeEncodeBytes (aSource, Base64.DO_BREAK_LINES | Base64.DO_NEWLINE_CRLF);
  assertEquals ("YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFh\r\n" +
                "YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYQ==",
                sEncoded);

  // Check that it can be read again
  aReadBytes = StreamHelper.getAllBytes (new Base64InputStream (new NonBlockingByteArrayInputStream (sEncoded.getBytes (Base64.PREFERRED_ENCODING))));
  assertArrayEquals (aSource, aReadBytes);
}
 
Example 2
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 3
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 4
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 5
Source File: CachingTransformStreamSource.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
public CachingTransformStreamSource (@Nonnull @WillClose final InputStream aIS, @Nullable final String sSystemID)
{
  super (new NonBlockingByteArrayInputStream (StreamHelper.getAllBytes (aIS)), sSystemID);
}
 
Example 6
Source File: CachingSAXInputSource.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nonnull
private static NonBlockingByteArrayInputStream _getCachedInputStream (@Nonnull @WillClose final InputStream aIS)
{
  return new NonBlockingByteArrayInputStream (StreamHelper.getAllBytes (aIS));
}
 
Example 7
Source File: SimpleFileIO.java    From ph-commons with Apache License 2.0 2 votes vote down vote up
/**
 * Get the content of the file as a byte array.
 *
 * @param aFile
 *        The file to read. May be <code>null</code>.
 * @return <code>null</code> if the passed file is <code>null</code> or if the
 *         passed file does not exist.
 */
@Nullable
public static byte [] getAllFileBytes (@Nullable final File aFile)
{
  return aFile == null ? null : StreamHelper.getAllBytes (FileHelper.getInputStream (aFile));
}