DfsOutputStream.java

  1. /*
  2.  * Copyright (C) 2011, 2012 Google Inc. and others. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.internal.storage.dfs;

  11. import java.io.IOException;
  12. import java.io.OutputStream;
  13. import java.nio.ByteBuffer;

  14. import org.eclipse.jgit.internal.storage.pack.PackExt;

  15. /**
  16.  * Output stream to create a file on the DFS.
  17.  *
  18.  * @see DfsObjDatabase#writeFile(DfsPackDescription, PackExt)
  19.  */
  20. public abstract class DfsOutputStream extends OutputStream {
  21.     /**
  22.      * Get the recommended alignment for writing.
  23.      * <p>
  24.      * Starting a write at multiples of the blockSize is more efficient than
  25.      * starting a write at any other position. If 0 or -1 the channel does not
  26.      * have any specific block size recommendation.
  27.      * <p>
  28.      * Channels should not recommend large block sizes. Sizes up to 1-4 MiB may
  29.      * be reasonable, but sizes above that may be horribly inefficient.
  30.      *
  31.      * @return recommended alignment size for randomly positioned reads. Does
  32.      *         not need to be a power of 2.
  33.      */
  34.     public int blockSize() {
  35.         return 0;
  36.     }

  37.     /** {@inheritDoc} */
  38.     @Override
  39.     public void write(int b) throws IOException {
  40.         write(new byte[] { (byte) b });
  41.     }

  42.     /** {@inheritDoc} */
  43.     @Override
  44.     public abstract void write(byte[] buf, int off, int len) throws IOException;

  45.     /**
  46.      * Read back a portion of already written data.
  47.      * <p>
  48.      * The writing position of the output stream is not affected by a read.
  49.      *
  50.      * @param position
  51.      *            offset to read from.
  52.      * @param buf
  53.      *            buffer to populate. Up to {@code buf.remaining()} bytes will
  54.      *            be read from {@code position}.
  55.      * @return number of bytes actually read.
  56.      * @throws java.io.IOException
  57.      *             reading is not supported, or the read cannot be performed due
  58.      *             to DFS errors.
  59.      */
  60.     public abstract int read(long position, ByteBuffer buf) throws IOException;
  61. }