@Override
  public void save(
      ImageReference imageReference, Path outputPath, Consumer<Long> writtenByteCountListener)
      throws InterruptedException, IOException {
    Process dockerProcess = docker("save", imageReference.toString());

    try (InputStream stdout = new BufferedInputStream(dockerProcess.getInputStream());
        OutputStream fileStream = new BufferedOutputStream(Files.newOutputStream(outputPath));
        NotifyingOutputStream notifyingFileStream =
            new NotifyingOutputStream(fileStream, writtenByteCountListener)) {
      ByteStreams.copy(stdout, notifyingFileStream);
    }

    if (dockerProcess.waitFor() != 0) {
      throw new IOException(
          "'docker save' command failed with error: " + getStderrOutput(dockerProcess));
    }
  }
@Test
  public void testSave_fail() throws InterruptedException {
    DockerClient testDockerClient = makeDockerSaveClient();
    Mockito.when(mockProcess.waitFor()).thenReturn(1);

    Mockito.when(mockProcess.getErrorStream())
        .thenReturn(new ByteArrayInputStream("error".getBytes(StandardCharsets.UTF_8)));

    try {
      testDockerClient.save(
          ImageReference.of(null, "testimage", null),
          temporaryFolder.getRoot().toPath().resolve("out.tar"),
          ignored -> {});
      Assert.fail("docker save should have failed");

    } catch (IOException ex) {
      Assert.assertEquals("'docker save' command failed with error: error", ex.getMessage());
    }
  }