/hudson-utils/src/main/java/org/hudsonci/utils/io/OffsetLimitInputStream.java

http://github.com/hudson/hudson · Java · 110 lines · 59 code · 22 blank · 29 comment · 2 complexity · b1a0243878e8bc1671a78a2ae21cc3bf MD5 · raw file

  1. /**
  2. * The MIT License
  3. *
  4. * Copyright (c) 2010-2011 Sonatype, Inc. All rights reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package org.hudsonci.utils.io;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import static com.google.common.base.Preconditions.checkNotNull;
  28. /**
  29. * Provides read-offset and read-limit for an underlying input-stream.
  30. *
  31. * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
  32. * @since 2.1.0
  33. */
  34. public class OffsetLimitInputStream
  35. extends InputStream
  36. {
  37. public static final int UNLIMITED = -1;
  38. private final InputStream delegate;
  39. private final long offset;
  40. private final long length;
  41. private long count = 0;
  42. public OffsetLimitInputStream(final InputStream delegate, final long offset, final long length) throws IOException {
  43. this.delegate = checkNotNull(delegate);
  44. this.offset = offset;
  45. this.length = length;
  46. delegate.skip(offset);
  47. }
  48. public InputStream getDelegate() {
  49. return delegate;
  50. }
  51. public long getOffset() {
  52. return offset;
  53. }
  54. public long getLength() {
  55. return length;
  56. }
  57. public long getCount() {
  58. return count;
  59. }
  60. @Override
  61. public int read() throws IOException {
  62. if (length > UNLIMITED && count > length) {
  63. return -1;
  64. }
  65. count++;
  66. return getDelegate().read();
  67. }
  68. @Override
  69. public int available() throws IOException {
  70. return getDelegate().available();
  71. }
  72. @Override
  73. public void close() throws IOException {
  74. getDelegate().close();
  75. }
  76. @Override
  77. public synchronized void mark(final int readlimit) {
  78. getDelegate().mark(readlimit);
  79. }
  80. @Override
  81. public synchronized void reset() throws IOException {
  82. getDelegate().reset();
  83. }
  84. @Override
  85. public boolean markSupported() {
  86. return getDelegate().markSupported();
  87. }
  88. }