private void testFormat() throws IOException {
    Map<Long, byte[]> map = New.hashMap();
    StreamStore store = new StreamStore(map);
    store.setMinBlockSize(10);
    store.setMaxBlockSize(20);
    store.setNextKey(123);

    byte[] id;

    id = store.put(new ByteArrayInputStream(new byte[200]));
    assertEquals(200, store.length(id));
    assertEquals("02c8018801", StringUtils.convertBytesToHex(id));

    id = store.put(new ByteArrayInputStream(new byte[0]));
    assertEquals("", StringUtils.convertBytesToHex(id));

    id = store.put(new ByteArrayInputStream(new byte[1]));
    assertEquals("000100", StringUtils.convertBytesToHex(id));

    id = store.put(new ByteArrayInputStream(new byte[3]));
    assertEquals("0003000000", StringUtils.convertBytesToHex(id));

    id = store.put(new ByteArrayInputStream(new byte[10]));
    assertEquals("010a8901", StringUtils.convertBytesToHex(id));

    byte[] combined = StringUtils.convertHexToBytes("0001aa0002bbcc");
    assertEquals(3, store.length(combined));
    InputStream in = store.get(combined);
    assertEquals(1, in.skip(1));
    assertEquals(0xbb, in.read());
    assertEquals(1, in.skip(1));
  }
 private ValueLobDb createLob(InputStream in, int type) throws IOException {
   byte[] streamStoreId;
   try {
     streamStoreId = streamStore.put(in);
   } catch (Exception e) {
     throw DbException.convertToIOException(e);
   }
   long lobId = generateLobId();
   long length = streamStore.length(streamStoreId);
   int tableId = LobStorageFrontend.TABLE_TEMP;
   Object[] value = new Object[] {streamStoreId, tableId, length, 0};
   lobMap.put(lobId, value);
   Object[] key = new Object[] {streamStoreId, lobId};
   refMap.put(key, Boolean.TRUE);
   ValueLobDb lob = ValueLobDb.create(type, database, tableId, lobId, null, length);
   if (TRACE) {
     trace("create " + tableId + "/" + lobId);
   }
   return lob;
 }
 private void testDetectIllegalId() throws IOException {
   Map<Long, byte[]> map = New.hashMap();
   StreamStore store = new StreamStore(map);
   try {
     store.length(new byte[] {3, 0, 0});
     fail();
   } catch (IllegalArgumentException e) {
     // expected
   }
   try {
     store.remove(new byte[] {3, 0, 0});
     fail();
   } catch (IllegalArgumentException e) {
     // expected
   }
   map.put(0L, new byte[] {3, 0, 0});
   InputStream in = store.get(new byte[] {2, 1, 0});
   try {
     in.read();
     fail();
   } catch (IllegalArgumentException e) {
     // expected
   }
 }
 Stream(StreamStore store, byte[] id) {
   this.store = store;
   this.length = store.length(id);
   this.idBuffer = ByteBuffer.wrap(id);
 }
  private void test(StreamStore store, int minBlockSize, int maxBlockSize, int length)
      throws IOException {
    store.setMinBlockSize(minBlockSize);
    assertEquals(minBlockSize, store.getMinBlockSize());
    store.setMaxBlockSize(maxBlockSize);
    assertEquals(maxBlockSize, store.getMaxBlockSize());
    long next = store.getNextKey();
    Random r = new Random(length);
    byte[] data = new byte[length];
    r.nextBytes(data);
    byte[] id = store.put(new ByteArrayInputStream(data));
    if (length > 0 && length >= minBlockSize) {
      assertFalse(store.isInPlace(id));
    } else {
      assertTrue(store.isInPlace(id));
    }
    long next2 = store.getNextKey();
    if (length > 0 && length >= minBlockSize) {
      assertTrue(next2 > next);
    } else {
      assertEquals(next, next2);
    }
    if (length == 0) {
      assertEquals(0, id.length);
    }

    assertEquals(length, store.length(id));

    InputStream in = store.get(id);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    IOUtils.copy(in, out);
    assertTrue(Arrays.equals(data, out.toByteArray()));

    in = store.get(id);
    in.close();
    assertEquals(-1, in.read());

    in = store.get(id);
    assertEquals(0, in.skip(0));
    if (length > 0) {
      assertEquals(1, in.skip(1));
      if (length > 1) {
        assertEquals(data[1] & 255, in.read());
        if (length > 2) {
          assertEquals(1, in.skip(1));
          if (length > 3) {
            assertEquals(data[3] & 255, in.read());
          }
        } else {
          assertEquals(0, in.skip(1));
        }
      } else {
        assertEquals(-1, in.read());
      }
    } else {
      assertEquals(0, in.skip(1));
    }

    if (length > 12) {
      in = store.get(id);
      assertEquals(12, in.skip(12));
      assertEquals(data[12] & 255, in.read());
      long skipped = 0;
      while (true) {
        long s = in.skip(Integer.MAX_VALUE);
        if (s == 0) {
          break;
        }
        skipped += s;
      }
      assertEquals(length - 13, skipped);
      assertEquals(-1, in.read());
    }

    store.remove(id);
    assertEquals(0, store.getMap().size());
  }