PageRenderTime 2893ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/org/dynmap/web/BoundInputStream.java

http://github.com/webbukkit/dynmap
Java | 62 lines | 52 code | 10 blank | 0 comment | 3 complexity | 4f99408f9f03c62a402995b03196f4df MD5 | raw file
Possible License(s): Apache-2.0
  1. package org.dynmap.web;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.logging.Logger;
  5. public class BoundInputStream extends InputStream {
  6. protected static final Logger log = Logger.getLogger("Minecraft");
  7. protected static final String LOG_PREFIX = "[dynmap] ";
  8. private InputStream base;
  9. private long bound;
  10. public BoundInputStream(InputStream base, long bound) {
  11. this.base = base;
  12. this.bound = bound;
  13. }
  14. @Override
  15. public int read() throws IOException {
  16. if (bound <= 0) return -1;
  17. int r = base.read();
  18. if (r >= 0)
  19. bound--;
  20. return r;
  21. }
  22. @Override
  23. public int available() throws IOException {
  24. return (int)Math.min(base.available(), bound);
  25. }
  26. @Override
  27. public boolean markSupported() {
  28. return false;
  29. }
  30. @Override
  31. public int read(byte[] b, int off, int len) throws IOException {
  32. if (bound <= 0) return -1;
  33. len = (int)Math.min(bound, len);
  34. int r = base.read(b, off, len);
  35. bound -= r;
  36. return r;
  37. }
  38. @Override
  39. public int read(byte[] b) throws IOException {
  40. return read(b, 0, b.length);
  41. }
  42. @Override
  43. public long skip(long n) throws IOException {
  44. long r = base.skip(Math.min(bound, n));
  45. bound -= r;
  46. return r;
  47. }
  48. @Override
  49. public void close() throws IOException {
  50. base.close();
  51. }
  52. }