Exemplo n.º 1
0
  public OutputStream newOutputStream(Path path, StandardOpenOption... options) throws IOException {
    final FileOpened result =
        request(new FileNewOutputStream(path.toString(), Arrays.asList(options)), FileOpened.class);

    final int handle = result.getHandle();

    return new BufferedOutputStream(
        new OutputStream() {
          @Override
          public void write(int b) throws IOException {
            throw new IOException("write(int): not implemented");
          }

          @Override
          public void write(byte[] b, int off, int len) throws IOException {
            final byte[] buffer = new byte[len];
            System.arraycopy(b, off, buffer, 0, len);
            request(new FileWrite(handle, buffer), Acknowledge.class);
          }

          @Override
          public void flush() throws IOException {
            request(new FileFlush(handle), Acknowledge.class);
          }

          @Override
          public void close() throws IOException {
            request(new FileClose(handle), Acknowledge.class);
          }
        },
        BUFFER_SIZE);
  }
Exemplo n.º 2
0
  public InputStream newInputStream(Path path, StandardOpenOption... options) throws IOException {
    final FileOpened result =
        request(new FileNewInputStream(path.toString(), Arrays.asList(options)), FileOpened.class);

    final int handle = result.getHandle();

    /* BufferedInputStream should _never_ use {@link InputStream#read()}, making sure we don't
     * have to implement that method. Reading one byte at-a-time over the network would be a
     * folly */
    return new BufferedInputStream(
        new InputStream() {
          @Override
          public int read() throws IOException {
            throw new IOException("read(): not implemented");
          }

          @Override
          public int read(byte[] b, int off, int len) throws IOException {
            final FileReadResult data = request(new FileRead(handle, len), FileReadResult.class);
            final byte[] bytes = data.getData();
            System.arraycopy(bytes, 0, b, off, bytes.length);
            return bytes.length;
          }

          @Override
          public void close() throws IOException {
            request(new FileClose(handle), Acknowledge.class);
          }
        },
        BUFFER_SIZE);
  }