@Override public Optional<String> hintOfCurrentInputFileNameForLogging() { return this.getHintOfCurrentInputFileNameForLogging(); }
@Test public void testEmptyStream() throws IOException { InputStreamFileInput subject = new InputStreamFileInput( runtime.getBufferAllocator(), provider(new ByteArrayInputStream(new byte[0]))); assertEquals(true, subject.nextFile()); assertEquals(null, subject.poll()); subject.close(); }
@Test public void testEmptyProvider() throws IOException { InputStreamFileInput subject = new InputStreamFileInput( runtime.getBufferAllocator(), provider(new InputStream[0])); assertEquals(false, subject.nextFile()); subject.close(); }
@Test public void testPollFirstException() throws IOException { InputStreamFileInput subject = new InputStreamFileInput( runtime.getBufferAllocator(), provider(new ByteArrayInputStream("abcdef".getBytes("UTF-8")))); try { subject.poll(); fail(); } catch (IllegalStateException ile) { // OK } subject.close(); }
private FileInput fileInput(String... lines) throws Exception { StringBuilder sb = new StringBuilder(); for (String line : lines) { sb.append(line).append("\n"); } ByteArrayInputStream in = new ByteArrayInputStream(sb.toString().getBytes("UTF-8")); return new InputStreamFileInput(runtime.getBufferAllocator(), provider(in)); }
@Test public void testProviderCloseException() { InputStreamFileInput subject = new InputStreamFileInput( runtime.getBufferAllocator(), new InputStreamFileInput.Provider() { @Override public InputStream openNext() throws IOException { return new ByteArrayInputStream(new byte[0]); } @Override public void close() throws IOException { throw new IOException("emulated exception"); } }); try { subject.close(); fail(); } catch (RuntimeException re) { // OK } }
@Test public void testSingleProvider() throws IOException { InputStreamFileInput subject = new InputStreamFileInput( runtime.getBufferAllocator(), provider(new ByteArrayInputStream("abcdef".getBytes("UTF-8")))); assertEquals(true, subject.nextFile()); assertEquals("abcdef", bufferToString(subject.poll())); assertEquals(null, subject.poll()); subject.close(); }
@Test public void testProviderOpenNextException() { InputStreamFileInput subject = new InputStreamFileInput( runtime.getBufferAllocator(), new InputStreamFileInput.Provider() { @Override public InputStream openNext() throws IOException { throw new IOException("emulated exception"); } @Override public void close() throws IOException { } }); try { subject.nextFile(); fail(); } catch (RuntimeException re) { // OK } subject.close(); }
@Override public FileInput open(TaskSource taskSource, FileInput fileInput) { PluginTask task = taskSource.loadTask(PluginTask.class); final FileInputInputStream files = new FileInputInputStream(fileInput); return new InputStreamFileInput( task.getBufferAllocator(), new InputStreamFileInput.Provider() { // Implement openNextWithHints() instead of openNext() to show file name at parser plugin loaded by FileInputPlugin // Because when using decoder, parser plugin can't get file name. @Override public InputStreamWithHints openNextWithHints() throws IOException { if (!files.nextFile()) { return null; } return new InputStreamWithHints( new BZip2CompressorInputStream(files, true), fileInput.hintOfCurrentInputFileNameForLogging().orElse(null) ); } @Override public void close() throws IOException { files.close(); } }); } }
@Test public void testMultipleProvider() throws IOException { InputStreamFileInput subject = new InputStreamFileInput( runtime.getBufferAllocator(), provider( new ByteArrayInputStream("abcdef".getBytes("UTF-8")), new ByteArrayInputStream("ghijkl".getBytes("UTF-8")), new ByteArrayInputStream("mnopqr".getBytes("UTF-8")))); assertEquals(true, subject.nextFile()); assertEquals("abcdef", bufferToString(subject.poll())); assertEquals(true, subject.nextFile()); assertEquals("ghijkl", bufferToString(subject.poll())); assertEquals(true, subject.nextFile()); assertEquals("mnopqr", bufferToString(subject.poll())); subject.close(); }
@Override public FileInput open(TaskSource taskSource, FileInput fileInput) { PluginTask task = taskSource.loadTask(PluginTask.class); final FileInputInputStream files = new FileInputInputStream(fileInput); return new InputStreamFileInput( task.getBufferAllocator(), new InputStreamFileInput.Provider() { // Implement openNextWithHints() instead of openNext() to show file name at parser plugin loaded by FileInputPlugin // Because when using decoder, parser plugin can't get file name. @Override public InputStreamWithHints openNextWithHints() throws IOException { if (!files.nextFile()) { return null; } return new InputStreamWithHints( new GZIPInputStream(files, 8 * 1024), fileInput.hintOfCurrentInputFileNameForLogging().orElse(null) ); } @Override public void close() throws IOException { files.close(); } }); } }
@Test public void testInputStreamReadException() { InputStreamFileInput subject = new InputStreamFileInput( runtime.getBufferAllocator(), new InputStreamFileInput.Provider() { @Override public InputStream openNext() throws IOException { return new InputStream() { @Override public int read() throws IOException { throw new IOException("emulated exception"); } }; } @Override public void close() throws IOException { } }); assertEquals(true, subject.nextFile()); try { subject.poll(); fail(); } catch (RuntimeException re) { // OK } subject.close(); }