Ejemplo n.º 1
1
  /**
   * Maps blah file with a random offset and checks to see if read from the ByteBuffer gets the
   * right line number
   */
  private static void testRead() throws Exception {
    StringBuilder sb = new StringBuilder();
    sb.setLength(4);

    for (int x = 0; x < 1000; x++) {
      try (FileInputStream fis = new FileInputStream(blah)) {
        FileChannel fc = fis.getChannel();

        long offset = generator.nextInt(10000);
        long expectedResult = offset / CHARS_PER_LINE;
        offset = expectedResult * CHARS_PER_LINE;

        MappedByteBuffer b = fc.map(MapMode.READ_ONLY, offset, 100);

        for (int i = 0; i < 4; i++) {
          byte aByte = b.get(i);
          sb.setCharAt(i, (char) aByte);
        }

        int result = Integer.parseInt(sb.toString());
        if (result != expectedResult) {
          err.println("I expected " + expectedResult);
          err.println("I got " + result);
          throw new Exception("Read test failed");
        }
      }
    }
  }
Ejemplo n.º 2
1
  /**
   * Maps blah file with a random offset and checks to see if data written out to the file can be
   * read back in
   */
  private static void testWrite() throws Exception {
    StringBuilder sb = new StringBuilder();
    sb.setLength(4);

    for (int x = 0; x < 1000; x++) {
      try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) {
        FileChannel fc = raf.getChannel();

        long offset = generator.nextInt(1000);
        MappedByteBuffer b = fc.map(MapMode.READ_WRITE, offset, 100);

        for (int i = 0; i < 4; i++) {
          b.put(i, (byte) ('0' + i));
        }

        for (int i = 0; i < 4; i++) {
          byte aByte = b.get(i);
          sb.setCharAt(i, (char) aByte);
        }
        if (!sb.toString().equals("0123")) throw new Exception("Write test failed");
      }
    }
  }
Ejemplo n.º 3
0
 private static String toHexString(byte[] data) {
   StringBuilder buffer = StringUtils.stringBuilder();
   for (int i = 0; i < data.length; i++) {
     buffer.append(Integer.toHexString((data[i] & 0xf0) >>> 4));
     buffer.append(Integer.toHexString(data[i] & 0x0f));
   }
   return buffer.toString();
 }
Ejemplo n.º 4
0
  private static void testHighOffset() throws Exception {
    StringBuilder sb = new StringBuilder();
    sb.setLength(4);

    for (int x = 0; x < 1000; x++) {
      try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) {
        FileChannel fc = raf.getChannel();
        long offset = 66000;
        MappedByteBuffer b = fc.map(MapMode.READ_WRITE, offset, 100);
      }
    }
  }
Ejemplo n.º 5
0
 private static void appendBase16(StringBuilder buf, byte[] bytes) {
   int base = 16;
   for (byte b : bytes) {
     int bi = 0xff & b;
     int c = '0' + (bi / base) % base;
     if (c > '9') c = 'a' + (c - '0' - 10);
     buf.append((char) c);
     c = '0' + bi % base;
     if (c > '9') c = 'a' + (c - '0' - 10);
     buf.append((char) c);
   }
 }
Ejemplo n.º 6
0
    private byte[] dataDigest(StringBuilder sb, String digestUri, MessageDigest md) {

      sb.append(methodName).append(':').append(digestUri);
      if ("auth-int".equals(qop)) {
        sb.append(':').append(EMPTY_ENTITY_MD5);

      } else if (qop != null && !qop.equals("auth")) {
        throw new UnsupportedOperationException("Digest qop not supported: " + qop);
      }

      return md5FromRecycledStringBuilder(sb, md);
    }
Ejemplo n.º 7
0
    private byte[] secretDigest(StringBuilder sb, MessageDigest md) {

      sb.append(principal).append(':').append(realmName).append(':').append(password);
      byte[] ha1 = md5FromRecycledStringBuilder(sb, md);

      if (algorithm == null || algorithm.equals("MD5")) {
        return ha1;
      } else if ("MD5-sess".equals(algorithm)) {
        appendBase16(sb, ha1);
        sb.append(':').append(nonce).append(':').append(cnonce);
        return md5FromRecycledStringBuilder(sb, md);
      }

      throw new UnsupportedOperationException("Digest algorithm not supported: " + algorithm);
    }
Ejemplo n.º 8
0
 private void appendDataBase(StringBuilder sb) {
   sb.append(':').append(nonce).append(':');
   if ("auth".equals(qop) || "auth-int".equals(qop)) {
     sb.append(nc).append(':').append(cnonce).append(':').append(qop).append(':');
   }
 }
Ejemplo n.º 9
0
 private byte[] md5FromRecycledStringBuilder(StringBuilder sb, MessageDigest md) {
   md.update(StringUtils.charSequence2ByteBuffer(sb, ISO_8859_1));
   sb.setLength(0);
   return md.digest();
 }