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.

FastByteArrayInputStream.java 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package com.vaadin.sass.util;
  2. import java.io.InputStream;
  3. /**
  4. * ByteArrayInputStream implementation that does not synchronize methods.
  5. */
  6. public class FastByteArrayInputStream extends InputStream {
  7. /**
  8. * Our byte buffer
  9. */
  10. protected byte[] buf = null;
  11. /**
  12. * Number of bytes that we can read from the buffer
  13. */
  14. protected int count = 0;
  15. /**
  16. * Number of bytes that have been read from the buffer
  17. */
  18. protected int pos = 0;
  19. public FastByteArrayInputStream(byte[] buf, int count) {
  20. this.buf = buf;
  21. this.count = count;
  22. }
  23. @Override
  24. public final int available() {
  25. return count - pos;
  26. }
  27. @Override
  28. public final int read() {
  29. return (pos < count) ? (buf[pos++] & 0xff) : -1;
  30. }
  31. @Override
  32. public final int read(byte[] b, int off, int len) {
  33. if (pos >= count) {
  34. return -1;
  35. }
  36. if ((pos + len) > count) {
  37. len = (count - pos);
  38. }
  39. System.arraycopy(buf, pos, b, off, len);
  40. pos += len;
  41. return len;
  42. }
  43. @Override
  44. public final long skip(long n) {
  45. if ((pos + n) > count) {
  46. n = count - pos;
  47. }
  48. if (n < 0) {
  49. return 0;
  50. }
  51. pos += n;
  52. return n;
  53. }
  54. }