@Test
  public void testReadArray1SPP8BPS() throws IOException {
    // 1 sample per pixel, 8 bits per sample (gray/indexed)
    byte[] data = {(byte) 0xff, 0, 0, 0, 0x7f, 1, 4, -4, 0x00, 127, 127, -127};

    InputStream stream =
        new HorizontalDeDifferencingStream(
            new ByteArrayInputStream(data), 4, 1, 8, ByteOrder.BIG_ENDIAN);

    byte[] result = new byte[data.length];
    new DataInputStream(stream).readFully(result);

    assertArrayEquals(
        new byte[] {
          (byte) 0xff,
          (byte) 0xff,
          (byte) 0xff,
          (byte) 0xff,
          0x7f,
          (byte) 0x80,
          (byte) 0x84,
          (byte) 0x80,
          0x00,
          0x7f,
          (byte) 0xfe,
          0x7f,
        },
        result);

    // EOF
    assertEquals(-1, stream.read(new byte[16]));
    assertEquals(-1, stream.read());
  }
  @Test
  public void testRead1SPP8BPS() throws IOException {
    // 1 sample per pixel, 8 bits per sample (gray/indexed)
    byte[] data = {(byte) 0xff, 0, 0, 0, 0x7f, 1, 4, -4, 0x00, 127, 127, -127};

    InputStream stream =
        new HorizontalDeDifferencingStream(
            new ByteArrayInputStream(data), 4, 1, 8, ByteOrder.BIG_ENDIAN);

    // Row 1
    assertEquals(0xff, stream.read());
    assertEquals(0xff, stream.read());
    assertEquals(0xff, stream.read());
    assertEquals(0xff, stream.read());

    // Row 2
    assertEquals(0x7f, stream.read());
    assertEquals(0x80, stream.read());
    assertEquals(0x84, stream.read());
    assertEquals(0x80, stream.read());

    // Row 3
    assertEquals(0x00, stream.read());
    assertEquals(0x7f, stream.read());
    assertEquals(0xfe, stream.read());
    assertEquals(0x7f, stream.read());

    // EOF
    assertEquals(-1, stream.read());
  }
  @Test
  public void testRead1SPP4BPS() throws IOException {
    // 1 sample per pixel, 4 bits per sample (gray/indexed)
    byte[] data = {
      (byte) 0xf0, 0x00, 0x00, 0x00, 0x70, 0x11, 0x44, (byte) 0xcc, 0x00, 0x01, 0x10, (byte) 0xe0
    };

    InputStream stream =
        new HorizontalDeDifferencingStream(
            new ByteArrayInputStream(data), 8, 1, 4, ByteOrder.BIG_ENDIAN);

    // Row 1
    assertEquals(0xff, stream.read());
    assertEquals(0xff, stream.read());
    assertEquals(0xff, stream.read());
    assertEquals(0xff, stream.read());

    // Row 2
    assertEquals(0x77, stream.read());
    assertEquals(0x89, stream.read());
    assertEquals(0xd1, stream.read());
    assertEquals(0xd9, stream.read());

    // Row 3
    assertEquals(0x00, stream.read());
    assertEquals(0x01, stream.read());
    assertEquals(0x22, stream.read());
    assertEquals(0x00, stream.read());

    // EOF
    assertEquals(-1, stream.read());
  }
  private void runStreamTest(final int pLength) throws Exception {
    byte[] data = createData(pLength);
    ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
    OutputStream out = new EncoderStream(outBytes, createEncoder(), true);

    try {
      // Provoke failure for encoders that doesn't take array offset properly into account
      int off = (data.length + 1) / 2;
      out.write(data, 0, off);
      if (data.length > off) {
        out.write(data, off, data.length - off);
      }
    } finally {
      out.close();
    }

    byte[] encoded = outBytes.toByteArray();

    //        System.err.println("encoded.length: " + encoded.length);
    //        System.err.println("encoded: " + Arrays.toString(encoded));

    byte[] decoded =
        FileUtil.read(
            new DecoderStream(new ByteArrayInputStream(encoded), createCompatibleDecoder()));
    assertTrue(Arrays.equals(data, decoded));

    InputStream in =
        new DecoderStream(new ByteArrayInputStream(encoded), createCompatibleDecoder());
    outBytes = new ByteArrayOutputStream();

    try {
      FileUtil.copy(in, outBytes);
    } finally {
      outBytes.close();
      in.close();
    }

    decoded = outBytes.toByteArray();
    assertTrue(Arrays.equals(data, decoded));
  }
  @Test
  public void testRead1SPP2BPS() throws IOException {
    // 1 sample per pixel, 2 bits per sample (gray/indexed)
    byte[] data = {
      (byte) 0xc0, 0x00, 0x00, 0x00, 0x71, 0x11, 0x44, (byte) 0xcc,
    };

    InputStream stream =
        new HorizontalDeDifferencingStream(
            new ByteArrayInputStream(data), 16, 1, 2, ByteOrder.BIG_ENDIAN);

    // Row 1
    assertEquals(0xff, stream.read());
    assertEquals(0xff, stream.read());
    assertEquals(0xff, stream.read());
    assertEquals(0xff, stream.read());

    // Row 2
    assertEquals(0x41, stream.read());
    assertEquals(0x6b, stream.read());
    assertEquals(0x05, stream.read());
    assertEquals(0x0f, stream.read());

    // EOF
    assertEquals(-1, stream.read());
  }
예제 #6
0
    @Override
    public HTTPResponse call() throws Exception {
      final HttpURLConnection hc = (HttpURLConnection) uri.toURL().openConnection();
      hc.setReadTimeout(SOCKET_TIMEOUT);
      try {
        while (!stop) {
          try {
            final int code = hc.getResponseCode();

            final InputStream input = hc.getInputStream();
            final ByteList bl = new ByteList();
            for (int i; (i = input.read()) != -1; ) bl.add(i);

            return new HTTPResponse(code, bl.toString());
          } catch (final SocketTimeoutException e) {
          }
        }
        return null;
      } finally {
        hc.disconnect();
      }
    }
  @FixFor("MODE-1308")
  @Test
  public void shouldAllowAnyBinaryImplementation() throws Exception {
    Node node = rootNode.addNode("nodeWithBinaryProperty", "nt:unstructured");
    final String stringValue = "This is the string stringValue";
    Binary binaryValue =
        new Binary() {
          public InputStream getStream() throws RepositoryException {
            return new ByteArrayInputStream(stringValue.getBytes());
          }

          public int read(byte[] b, long position) throws IOException, RepositoryException {
            byte[] content = stringValue.getBytes();
            int length =
                b.length + position < content.length ? b.length : (int) (content.length - position);
            System.arraycopy(content, (int) position, b, 0, length);
            return length;
          }

          public long getSize() throws RepositoryException {
            return stringValue.getBytes().length;
          }

          public void dispose() {}
        };
    node.setProperty("binProp", binaryValue);
    Binary nodeValue = node.getProperty("binProp").getBinary();
    assertNotNull(nodeValue);
    assertEquals(stringValue.getBytes().length, nodeValue.getSize());
    byte[] buffer = new byte[100];
    int available;
    InputStream inputStream = nodeValue.getStream();
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    while ((available = inputStream.read(buffer)) != -1) {
      byteOut.write(buffer, 0, available);
    }
    assertArrayEquals(stringValue.getBytes(), byteOut.toByteArray());
  }
  @Test
  public void testRead1SPP64BPS() throws IOException {
    // 1 sample per pixel, 64 bits per sample (gray)
    FastByteArrayOutputStream out = new FastByteArrayOutputStream(32);
    DataOutput dataOut = new DataOutputStream(out);
    dataOut.writeLong(0x00000000);
    dataOut.writeLong(81985529216486895L);
    dataOut.writeLong(81985529216486895L);
    dataOut.writeLong(-163971058432973790L);

    InputStream in =
        new HorizontalDeDifferencingStream(out.createInputStream(), 4, 1, 64, ByteOrder.BIG_ENDIAN);
    DataInput dataIn = new DataInputStream(in);

    // Row 1
    assertEquals(0, dataIn.readLong());
    assertEquals(81985529216486895L, dataIn.readLong());
    assertEquals(163971058432973790L, dataIn.readLong());
    assertEquals(0, dataIn.readLong());

    // EOF
    assertEquals(-1, in.read());
  }
  @Test
  public void testRead1SPP32BPS() throws IOException {
    // 1 sample per pixel, 32 bits per sample (gray)
    FastByteArrayOutputStream out = new FastByteArrayOutputStream(16);
    DataOutput dataOut = new DataOutputStream(out);
    dataOut.writeInt(0x00000000);
    dataOut.writeInt(305419896);
    dataOut.writeInt(305419896);
    dataOut.writeInt(-610839792);

    InputStream in =
        new HorizontalDeDifferencingStream(out.createInputStream(), 4, 1, 32, ByteOrder.BIG_ENDIAN);
    DataInput dataIn = new DataInputStream(in);

    // Row 1
    assertEquals(0, dataIn.readInt());
    assertEquals(305419896, dataIn.readInt());
    assertEquals(610839792, dataIn.readInt());
    assertEquals(0, dataIn.readInt());

    // EOF
    assertEquals(-1, in.read());
  }
  public static long copy(InputStream is, OutputStream os) {
    byte[] buf = new byte[BUFFER_SIZE];
    long total = 0;
    int len = 0;
    try {
      while (-1 != (len = is.read(buf))) {
        // System.out.print(buf);
        os.write(buf, 0, len);
        total += len;
      }
    } catch (IOException ioe) {
      throw new RuntimeException("error reading stream", ioe);
    }

    return total;
  }
  @Test
  public void testRead1SPP1BPS() throws IOException {
    // 1 sample per pixel, 1 bits per sample (mono/indexed)
    byte[] data = {
      (byte) 0x80, 0x00, 0x00, 0x71, 0x11, 0x44,
    };

    InputStream stream =
        new HorizontalDeDifferencingStream(
            new ByteArrayInputStream(data), 24, 1, 1, ByteOrder.BIG_ENDIAN);

    // Row 1
    assertEquals(0xff, stream.read());
    assertEquals(0xff, stream.read());
    assertEquals(0xff, stream.read());

    // Row 2
    assertEquals(0x5e, stream.read());
    assertEquals(0x1e, stream.read());
    assertEquals(0x78, stream.read());

    // EOF
    assertEquals(-1, stream.read());
  }
  @Test
  public void testRead4SPP8BPS() throws IOException {
    // 4 samples per pixel, 8 bits per sample (RGBA)
    byte[] data = {
      (byte) 0xff,
      (byte) 0x00,
      (byte) 0x7f,
      0x00,
      -1,
      -1,
      -1,
      -1,
      -4,
      -4,
      -4,
      -4,
      4,
      4,
      4,
      4,
      0x7f,
      0x7f,
      0x7f,
      0x7f,
      1,
      1,
      1,
      1,
      4,
      4,
      4,
      4,
      -4,
      -4,
      -4,
      -4,
    };

    InputStream stream =
        new HorizontalDeDifferencingStream(
            new ByteArrayInputStream(data), 4, 4, 8, ByteOrder.BIG_ENDIAN);

    // Row 1
    assertEquals(0xff, stream.read());
    assertEquals(0x00, stream.read());
    assertEquals(0x7f, stream.read());
    assertEquals(0x00, stream.read());

    assertEquals(0xfe, stream.read());
    assertEquals(0xff, stream.read());
    assertEquals(0x7e, stream.read());
    assertEquals(0xff, stream.read());

    assertEquals(0xfa, stream.read());
    assertEquals(0xfb, stream.read());
    assertEquals(0x7a, stream.read());
    assertEquals(0xfb, stream.read());

    assertEquals(0xfe, stream.read());
    assertEquals(0xff, stream.read());
    assertEquals(0x7e, stream.read());
    assertEquals(0xff, stream.read());

    // Row 2
    assertEquals(0x7f, stream.read());
    assertEquals(0x7f, stream.read());
    assertEquals(0x7f, stream.read());
    assertEquals(0x7f, stream.read());

    assertEquals(0x80, stream.read());
    assertEquals(0x80, stream.read());
    assertEquals(0x80, stream.read());
    assertEquals(0x80, stream.read());

    assertEquals(0x84, stream.read());
    assertEquals(0x84, stream.read());
    assertEquals(0x84, stream.read());
    assertEquals(0x84, stream.read());

    assertEquals(0x80, stream.read());
    assertEquals(0x80, stream.read());
    assertEquals(0x80, stream.read());
    assertEquals(0x80, stream.read());

    // EOF
    assertEquals(-1, stream.read());
  }
  @Test
  public void testRead3SPP16BPSLittleEndian() throws IOException {
    FastByteArrayOutputStream out = new FastByteArrayOutputStream(24);
    DataOutput dataOut = new LittleEndianDataOutputStream(out);
    dataOut.writeShort(0x0000);
    dataOut.writeShort(0x0000);
    dataOut.writeShort(0x0000);
    dataOut.writeShort(4660);
    dataOut.writeShort(30292);
    dataOut.writeShort(4660);
    dataOut.writeShort(4660);
    dataOut.writeShort(30292);
    dataOut.writeShort(4660);
    dataOut.writeShort(-9320);
    dataOut.writeShort(-60584);
    dataOut.writeShort(-9320);

    dataOut.writeShort(0x0000);
    dataOut.writeShort(0x0000);
    dataOut.writeShort(0x0000);
    dataOut.writeShort(30292);
    dataOut.writeShort(30292);
    dataOut.writeShort(30292);
    dataOut.writeShort(30292);
    dataOut.writeShort(30292);
    dataOut.writeShort(30292);
    dataOut.writeShort(-60584);
    dataOut.writeShort(-60584);
    dataOut.writeShort(-60584);

    InputStream in =
        new HorizontalDeDifferencingStream(
            out.createInputStream(), 4, 3, 16, ByteOrder.LITTLE_ENDIAN);
    DataInput dataIn = new LittleEndianDataInputStream(in);

    // Row 1
    assertEquals(0, dataIn.readUnsignedShort());
    assertEquals(0, dataIn.readUnsignedShort());
    assertEquals(0, dataIn.readUnsignedShort());
    assertEquals(4660, dataIn.readUnsignedShort());
    assertEquals(30292, dataIn.readUnsignedShort());
    assertEquals(4660, dataIn.readUnsignedShort());
    assertEquals(9320, dataIn.readUnsignedShort());
    assertEquals(60584, dataIn.readUnsignedShort());
    assertEquals(9320, dataIn.readUnsignedShort());
    assertEquals(0, dataIn.readUnsignedShort());
    assertEquals(0, dataIn.readUnsignedShort());
    assertEquals(0, dataIn.readUnsignedShort());

    // Row 2
    assertEquals(0, dataIn.readUnsignedShort());
    assertEquals(0, dataIn.readUnsignedShort());
    assertEquals(0, dataIn.readUnsignedShort());
    assertEquals(30292, dataIn.readUnsignedShort());
    assertEquals(30292, dataIn.readUnsignedShort());
    assertEquals(30292, dataIn.readUnsignedShort());
    assertEquals(60584, dataIn.readUnsignedShort());
    assertEquals(60584, dataIn.readUnsignedShort());
    assertEquals(60584, dataIn.readUnsignedShort());
    assertEquals(0, dataIn.readUnsignedShort());
    assertEquals(0, dataIn.readUnsignedShort());
    assertEquals(0, dataIn.readUnsignedShort());

    // EOF
    assertEquals(-1, in.read());
  }