示例#1
0
  /**
   * Checks that the contents of this byte source are equal to the contents of the given byte
   * source.
   *
   * @throws IOException if an I/O error occurs in the process of reading from this source or {@code
   *     other}
   */
  public boolean contentEquals(ByteSource other) throws IOException {
    checkNotNull(other);

    byte[] buf1 = new byte[BUF_SIZE];
    byte[] buf2 = new byte[BUF_SIZE];

    Closer closer = Closer.create();
    try {
      InputStream in1 = closer.register(openStream());
      InputStream in2 = closer.register(other.openStream());
      while (true) {
        int read1 = ByteStreams.read(in1, buf1, 0, BUF_SIZE);
        int read2 = ByteStreams.read(in2, buf2, 0, BUF_SIZE);
        if (read1 != read2 || !Arrays.equals(buf1, buf2)) {
          return false;
        } else if (read1 != BUF_SIZE) {
          return true;
        }
      }
    } catch (Throwable e) {
      throw closer.rethrow(e);
    } finally {
      closer.close();
    }
  }
  private ByteSource createUsersMock(Map<String, String> users) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    for (Map.Entry<String, String> user : users.entrySet()) {
      BatchInput.User.Builder builder = BatchInput.User.newBuilder();
      builder.setLogin(user.getKey()).setName(user.getValue()).build().writeDelimitedTo(out);
    }
    ByteSource source = mock(ByteSource.class);
    when(source.openStream()).thenReturn(new ByteArrayInputStream(out.toByteArray()));
    return source;
  }
示例#3
0
 @Override
 public int execute(ExecutionContext context) {
   try (InputStream sourceStream = source.openStream()) {
     context
         .getProjectFilesystem()
         .copyToPath(sourceStream, outputPath, StandardCopyOption.REPLACE_EXISTING);
     return 0;
   } catch (IOException e) {
     LOG.error(e, "Couldn't copy bytes to %s", outputPath);
     e.printStackTrace(context.getStdErr());
     return 1;
   }
 }
  public ExampleRecordCursor(List<ExampleColumnHandle> columnHandles, ByteSource byteSource) {
    this.columnHandles = columnHandles;

    fieldToColumnIndex = new int[columnHandles.size()];
    for (int i = 0; i < columnHandles.size(); i++) {
      ExampleColumnHandle columnHandle = columnHandles.get(i);
      fieldToColumnIndex[i] = columnHandle.getOrdinalPosition();
    }

    try (CountingInputStream input = new CountingInputStream(byteSource.openStream())) {
      lines = byteSource.asCharSource(UTF_8).readLines().iterator();
      totalBytes = input.getCount();
    } catch (IOException e) {
      throw Throwables.propagate(e);
    }
  }
  @Test
  public void testInputStreamError() throws IOException {
    ByteSource source = mock(ByteSource.class);

    WSLoaderResult<ByteSource> res = new WSLoaderResult<>(source, true);

    when(wsLoader.loadSource("/batch/users?logins=fmallet,sbrandhof")).thenReturn(res);

    InputStream errorInputStream = mock(InputStream.class);
    Mockito.doThrow(IOException.class).when(errorInputStream).read();
    when(source.openStream()).thenReturn(errorInputStream);

    exception.expect(IllegalStateException.class);
    exception.expectMessage("Unable to get user details from server");

    userRepo.load(Arrays.asList("fmallet", "sbrandhof"));
  }
 @Override
 public InputStream openStream() throws IOException {
   return new GZIPInputStream(containedByteSource.openStream());
 }