You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReadableChannel.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (C) 2011, Google Inc.
  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.nio.channels.ReadableByteChannel;
  46. /**
  47. * Readable random access byte channel from a file.
  48. */
  49. public interface ReadableChannel extends ReadableByteChannel {
  50. /**
  51. * Get the current position of the channel.
  52. *
  53. * @return r current offset.
  54. * @throws java.io.IOException
  55. * the channel's current position cannot be obtained.
  56. */
  57. long position() throws IOException;
  58. /**
  59. * Seek the current position of the channel to a new offset.
  60. *
  61. * @param newPosition
  62. * position to move the channel to. The next read will start from
  63. * here. This should be a multiple of the {@link #blockSize()}.
  64. * @throws java.io.IOException
  65. * the position cannot be updated. This may be because the
  66. * channel only supports block aligned IO and the current
  67. * position is not block aligned.
  68. */
  69. void position(long newPosition) throws IOException;
  70. /**
  71. * Get the total size of the channel.
  72. * <p>
  73. * Prior to reading from a channel the size might not yet be known.
  74. * Implementors may return -1 until after the first read method call. Once a
  75. * read has been completed, the underlying file size should be available.
  76. *
  77. * @return r total size of the channel; -1 if not yet available.
  78. * @throws java.io.IOException
  79. * the size cannot be determined.
  80. */
  81. long size() throws IOException;
  82. /**
  83. * Get the recommended alignment for reads.
  84. * <p>
  85. * Starting a read at multiples of the blockSize is more efficient than
  86. * starting a read at any other position. If 0 or -1 the channel does not
  87. * have any specific block size recommendation.
  88. * <p>
  89. * Channels should not recommend large block sizes. Sizes up to 1-4 MiB may
  90. * be reasonable, but sizes above that may be horribly inefficient. The
  91. * {@link org.eclipse.jgit.internal.storage.dfs.DfsBlockCache} favors the
  92. * alignment suggested by the channel rather than the configured size under
  93. * the assumption that reads are very expensive and the channel knows what
  94. * size is best to access it with.
  95. *
  96. * @return recommended alignment size for randomly positioned reads. Does
  97. * not need to be a power of 2.
  98. */
  99. int blockSize();
  100. /**
  101. * Recommend the channel maintain a read-ahead buffer.
  102. * <p>
  103. * A read-ahead buffer of approximately {@code bufferSize} in bytes may be
  104. * allocated and used by the channel to smooth out latency for read.
  105. * <p>
  106. * Callers can continue to read in smaller than {@code bufferSize} chunks.
  107. * With read-ahead buffering enabled read latency may fluctuate in a pattern
  108. * of one slower read followed by {@code (bufferSize / readSize) - 1} fast
  109. * reads satisfied by the read-ahead buffer. When summed up overall time to
  110. * read the same contiguous range should be lower than if read-ahead was not
  111. * enabled, as the implementation can combine reads to increase throughput.
  112. * <p>
  113. * To avoid unnecessary IO callers should only enable read-ahead if the
  114. * majority of the channel will be accessed in order.
  115. * <p>
  116. * Implementations may chose to read-ahead using asynchronous APIs or
  117. * background threads, or may simply aggregate reads using a buffer.
  118. * <p>
  119. * This read ahead stays in effect until the channel is closed or the buffer
  120. * size is set to 0.
  121. *
  122. * @param bufferSize
  123. * requested size of the read ahead buffer, in bytes.
  124. * @throws java.io.IOException
  125. * if the read ahead cannot be adjusted.
  126. */
  127. void setReadAheadBytes(int bufferSize) throws IOException;
  128. }