選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DfsOutputStream.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (C) 2011, 2012 Google Inc. and others.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.internal.storage.dfs;
  44. import java.io.IOException;
  45. import java.io.InputStream;
  46. import java.io.OutputStream;
  47. import java.nio.ByteBuffer;
  48. import org.eclipse.jgit.internal.storage.pack.PackExt;
  49. /**
  50. * Output stream to create a file on the DFS.
  51. *
  52. * @see DfsObjDatabase#writeFile(DfsPackDescription, PackExt)
  53. */
  54. public abstract class DfsOutputStream extends OutputStream {
  55. /**
  56. * Get the recommended alignment for writing.
  57. * <p>
  58. * Starting a write at multiples of the blockSize is more efficient than
  59. * starting a write at any other position. If 0 or -1 the channel does not
  60. * have any specific block size recommendation.
  61. * <p>
  62. * Channels should not recommend large block sizes. Sizes up to 1-4 MiB may
  63. * be reasonable, but sizes above that may be horribly inefficient.
  64. *
  65. * @return recommended alignment size for randomly positioned reads. Does
  66. * not need to be a power of 2.
  67. */
  68. public int blockSize() {
  69. return 0;
  70. }
  71. @Override
  72. public void write(int b) throws IOException {
  73. write(new byte[] { (byte) b });
  74. }
  75. @Override
  76. public abstract void write(byte[] buf, int off, int len) throws IOException;
  77. /**
  78. * Read back a portion of already written data.
  79. * <p>
  80. * The writing position of the output stream is not affected by a read.
  81. *
  82. * @param position
  83. * offset to read from.
  84. * @param buf
  85. * buffer to populate. Up to {@code buf.remaining()} bytes will
  86. * be read from {@code position}.
  87. * @return number of bytes actually read.
  88. * @throws IOException
  89. * reading is not supported, or the read cannot be performed due
  90. * to DFS errors.
  91. */
  92. public abstract int read(long position, ByteBuffer buf) throws IOException;
  93. /**
  94. * Open a stream to read back a portion of already written data.
  95. * <p>
  96. * The writing position of the output stream is not affected by a read. The
  97. * input stream can be read up to the current writing position. Closing the
  98. * returned stream has no effect on the underlying {@link DfsOutputStream}.
  99. *
  100. * @param position
  101. * offset to read from.
  102. * @return new input stream
  103. */
  104. public InputStream openInputStream(final long position) {
  105. return new ReadBackStream(position);
  106. }
  107. private class ReadBackStream extends InputStream {
  108. private final ByteBuffer buf;
  109. private long position;
  110. private ReadBackStream(long position) {
  111. int bs = blockSize();
  112. this.position = position;
  113. buf = ByteBuffer.allocate(bs > 0 ? bs : 8192);
  114. buf.position(buf.limit());
  115. }
  116. @Override
  117. public int read(byte[] b, int off, int len) throws IOException {
  118. int cnt = 0;
  119. while (0 < len) {
  120. if (!buf.hasRemaining()) {
  121. buf.rewind();
  122. int nr = DfsOutputStream.this.read(position, buf);
  123. if (nr < 0) {
  124. buf.position(buf.limit());
  125. break;
  126. }
  127. position += nr;
  128. buf.flip();
  129. }
  130. int n = Math.min(len, buf.remaining());
  131. buf.get(b, off, n);
  132. off += n;
  133. len -= n;
  134. cnt += n;
  135. }
  136. if (cnt == 0 && len > 0) return -1;
  137. return cnt;
  138. }
  139. @Override
  140. public int read() throws IOException {
  141. byte[] b = new byte[1];
  142. int n = read(b);
  143. return n == 1 ? b[0] & 0xff : -1;
  144. }
  145. }
  146. }