View Javadoc
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  
11  package org.eclipse.jgit.internal.storage.dfs;
12  
13  import java.io.IOException;
14  import java.io.OutputStream;
15  import java.nio.ByteBuffer;
16  
17  import org.eclipse.jgit.internal.storage.pack.PackExt;
18  
19  /**
20   * Output stream to create a file on the DFS.
21   *
22   * @see DfsObjDatabase#writeFile(DfsPackDescription, PackExt)
23   */
24  public abstract class DfsOutputStream extends OutputStream {
25  	/**
26  	 * Get the recommended alignment for writing.
27  	 * <p>
28  	 * Starting a write at multiples of the blockSize is more efficient than
29  	 * starting a write at any other position. If 0 or -1 the channel does not
30  	 * have any specific block size recommendation.
31  	 * <p>
32  	 * Channels should not recommend large block sizes. Sizes up to 1-4 MiB may
33  	 * be reasonable, but sizes above that may be horribly inefficient.
34  	 *
35  	 * @return recommended alignment size for randomly positioned reads. Does
36  	 *         not need to be a power of 2.
37  	 */
38  	public int blockSize() {
39  		return 0;
40  	}
41  
42  	/** {@inheritDoc} */
43  	@Override
44  	public void write(int b) throws IOException {
45  		write(new byte[] { (byte) b });
46  	}
47  
48  	/** {@inheritDoc} */
49  	@Override
50  	public abstract void write(byte[] buf, int off, int len) throws IOException;
51  
52  	/**
53  	 * Read back a portion of already written data.
54  	 * <p>
55  	 * The writing position of the output stream is not affected by a read.
56  	 *
57  	 * @param position
58  	 *            offset to read from.
59  	 * @param buf
60  	 *            buffer to populate. Up to {@code buf.remaining()} bytes will
61  	 *            be read from {@code position}.
62  	 * @return number of bytes actually read.
63  	 * @throws java.io.IOException
64  	 *             reading is not supported, or the read cannot be performed due
65  	 *             to DFS errors.
66  	 */
67  	public abstract int read(long position, ByteBuffer buf) throws IOException;
68  }