CloseShieldedInputStream.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.util;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. /**
  19. * Prevent the underlying input stream to close.
  20. * @author Joe Cheng, Zoey76
  21. */
  22. public class CloseShieldedInputStream extends InputStream
  23. {
  24. private InputStream _in = null;
  25. /**
  26. * Instantiates a new close shielded input stream.
  27. * @param in the in
  28. */
  29. public CloseShieldedInputStream(InputStream in)
  30. {
  31. _in = in;
  32. }
  33. /**
  34. * {@inheritDoc}
  35. */
  36. @Override
  37. public void close()
  38. {
  39. _in = null;
  40. }
  41. /**
  42. * {@inheritDoc}
  43. */
  44. @Override
  45. public int read() throws IOException
  46. {
  47. if (_in == null)
  48. {
  49. throw new IOException("Stream is null!");
  50. }
  51. return _in.read();
  52. }
  53. /**
  54. * {@inheritDoc}
  55. */
  56. @Override
  57. public int read(byte b[]) throws IOException
  58. {
  59. if (_in == null)
  60. {
  61. throw new IOException("Stream is null!");
  62. }
  63. return _in.read(b);
  64. }
  65. /**
  66. * {@inheritDoc}
  67. */
  68. @Override
  69. public int read(byte b[], int off, int len) throws IOException
  70. {
  71. if (_in == null)
  72. {
  73. throw new IOException("Stream is null!");
  74. }
  75. return _in.read(b, off, len);
  76. }
  77. /**
  78. * {@inheritDoc}
  79. */
  80. @Override
  81. public long skip(long n) throws IOException
  82. {
  83. if (_in == null)
  84. {
  85. throw new IOException("Stream is null!");
  86. }
  87. return _in.skip(n);
  88. }
  89. /**
  90. * {@inheritDoc}
  91. */
  92. @Override
  93. public synchronized void mark(int readlimit)
  94. {
  95. if (_in != null)
  96. {
  97. _in.mark(readlimit);
  98. }
  99. }
  100. /**
  101. * {@inheritDoc}
  102. */
  103. @Override
  104. public boolean markSupported()
  105. {
  106. if (_in == null)
  107. {
  108. return false;
  109. }
  110. return _in.markSupported();
  111. }
  112. /**
  113. * {@inheritDoc}
  114. */
  115. @Override
  116. public synchronized void reset() throws IOException
  117. {
  118. if (_in == null)
  119. {
  120. throw new IOException("Stream is null!");
  121. }
  122. _in.reset();
  123. }
  124. /**
  125. * Gets the underlying stream.
  126. * @return the underlying stream
  127. */
  128. public InputStream getUnderlyingStream()
  129. {
  130. return _in;
  131. }
  132. }