コード例 #1
0
 protected long readLong(byte[] content) throws IOException {
   byte[] bytes = inflateFrom(content, index);
   index += 1 + bytes.length;
   bytes = ByteUtils.rightAlignBytes(bytes, 8);
   long value = ByteUtils.convertLongFromBytes(bytes);
   return value;
 }
コード例 #2
0
 protected int readInt(byte[] content) throws IOException {
   byte[] bytes = inflateFrom(content, index);
   index += 1 + bytes.length;
   bytes = ByteUtils.rightAlignBytes(bytes, 4);
   int value = ByteUtils.convertIntFromBytes(bytes);
   if (value == BinaryOutputCapsule.NULL_OBJECT || value == BinaryOutputCapsule.DEFAULT_OBJECT)
     index -= 4;
   return value;
 }
コード例 #3
0
  @Test
  public void testSubArray() {
    byte[] hamburger = ByteUtils.toAsciiBytes("hamburger");
    byte[] urge = ByteUtils.toAsciiBytes("urge");
    ByteArrayBuffer buf = new ByteArrayBuffer(hamburger);
    assertArraysEqual(urge, buf.subArray(4, 8));

    byte[] smiles = ByteUtils.toAsciiBytes("smiles");
    byte[] mile = ByteUtils.toAsciiBytes("mile");
    buf = new ByteArrayBuffer(smiles);
    assertArraysEqual(mile, buf.subArray(1, 5));
    assertArraysEqual(smiles, buf.subArray(0, buf.length()));
  }
コード例 #4
0
 protected static byte[] inflateFrom(byte[] contents, int index) {
   byte firstByte = contents[index];
   if (firstByte == BinaryOutputCapsule.NULL_OBJECT)
     return ByteUtils.convertToBytes(BinaryOutputCapsule.NULL_OBJECT);
   else if (firstByte == BinaryOutputCapsule.DEFAULT_OBJECT)
     return ByteUtils.convertToBytes(BinaryOutputCapsule.DEFAULT_OBJECT);
   else if (firstByte == 0) return new byte[0];
   else {
     byte[] rVal = new byte[firstByte];
     for (int x = 0; x < rVal.length; x++) rVal[x] = contents[x + 1 + index];
     return rVal;
   }
 }
コード例 #5
0
ファイル: HashAlgorithm.java プロジェクト: gepp/studyDemo
 /** Get the md5 of the given key. */
 public static byte[] computeMd5(String k) {
   MessageDigest md5;
   try {
     md5 = MessageDigest.getInstance("MD5");
   } catch (NoSuchAlgorithmException e) {
     throw new RuntimeException("MD5 not supported", e);
   }
   md5.reset();
   md5.update(ByteUtils.getBytes(k));
   return md5.digest();
 }
コード例 #6
0
ファイル: JavaBinCodec.java プロジェクト: Gukie/lucene-solr
 public String readStr(DataInputInputStream dis, StringCache stringCache) throws IOException {
   int sz = readSize(dis);
   if (bytes == null || bytes.length < sz) bytes = new byte[sz];
   dis.readFully(bytes, 0, sz);
   if (stringCache != null) {
     return stringCache.get(bytesRef.reset(bytes, 0, sz));
   } else {
     arr.reset();
     ByteUtils.UTF8toUTF16(bytes, 0, sz, arr);
     return arr.toString();
   }
 }
コード例 #7
0
ファイル: JavaBinCodec.java プロジェクト: Gukie/lucene-solr
  /** write the string as tag+length, with length being the number of UTF-8 bytes */
  public void writeStr(String s) throws IOException {
    if (s == null) {
      writeTag(NULL);
      return;
    }
    int end = s.length();
    int maxSize = end * ByteUtils.MAX_UTF8_BYTES_PER_CHAR;

    if (maxSize <= MAX_UTF8_SIZE_FOR_ARRAY_GROW_STRATEGY) {
      if (bytes == null || bytes.length < maxSize) bytes = new byte[maxSize];
      int sz = ByteUtils.UTF16toUTF8(s, 0, end, bytes, 0);
      writeTag(STR, sz);
      daos.write(bytes, 0, sz);
    } else {
      // double pass logic for large strings, see SOLR-7971
      int sz = ByteUtils.calcUTF16toUTF8Length(s, 0, end);
      writeTag(STR, sz);
      if (bytes == null || bytes.length < 8192) bytes = new byte[8192];
      ByteUtils.writeUTF16toUTF8(s, 0, end, daos, bytes);
    }
  }
コード例 #8
0
ファイル: JavaBinCodec.java プロジェクト: Gukie/lucene-solr
 public String get(StringBytes b) {
   String result = cache.get(b);
   if (result == null) {
     // make a copy because the buffer received may be changed later by the caller
     StringBytes copy =
         new StringBytes(
             Arrays.copyOfRange(b.bytes, b.offset, b.offset + b.length), 0, b.length);
     CharArr arr = new CharArr();
     ByteUtils.UTF8toUTF16(b.bytes, b.offset, b.length, arr);
     result = arr.toString();
     cache.put(copy, result);
   }
   return result;
 }
コード例 #9
0
ファイル: Close.java プロジェクト: maxmmurphy/jruby-pg
  public Close(String name, StatementType type) {
    this.name = name;
    this.type = type;

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
      out.write(getFirstByte());
      ByteUtils.writeInt4(out, 0);
      switch (type) {
        case Portal:
          out.write('P');
          break;
        case Prepared:
          out.write('S');
          break;
      }
      ByteUtils.writeString(out, name);
    } catch (Exception e) {
      // we cannot be here
    }
    this.bytes = out.toByteArray();
    ByteUtils.fixLength(bytes);
  }
コード例 #10
0
ファイル: Tag_v2.java プロジェクト: gdusbabek/id3
 /** {@inheritDoc} */
 public void write(OutputStream out) throws IOException {
   // convert all frames to a byte array.
   ByteArrayOutputStream bout = new ByteArrayOutputStream();
   for (Iterator i = frames.values().iterator(); i.hasNext(); ) {
     Frame fr = (Frame) i.next();
     fr.write(bout);
   }
   bout.flush();
   byte[] frameBytes = bout.toByteArray();
   bout.close();
   if (header.usesUnsynchronization()) frameBytes = ByteUtils.unsync(frameBytes);
   header.setDataSize(frameBytes.length);
   header.write(out);
   out.write(frameBytes);
 }
コード例 #11
0
  public void decodeFile(String encodedFileName, String decodedFileName) {
    try {
      byte[] encodedFileAsBytes = getFileAsByteArray(encodedFileName);

      // Generating the Symbol & Code ArrayList
      ArrayList<Symbol> symbols = getSymbolTable(encodedFileAsBytes);

      //            // Generating CodeTree
      //            Node codeTree = CodeTree.makeCodeTree(symbols);
      //            // Logging the CodeTree whether it have been generated correctly or not.
      //            String code = "";
      //            CodeTree.getCodeOfSymbol(CodeTree.Node.ROOT, symbols.get(2).getIdentifier(),
      // code);
      //            System.out.println("Symbol: " + symbols.get(2) + " Code_Retrived: " + code);

      // Decoding the content
      int dividerIndex = getDividerIndexInArray(encodedFileAsBytes);

      byte[] encodedMessage = new byte[encodedFileAsBytes.length - dividerIndex];
      for (int j = 0, i = dividerIndex + 1; i < encodedFileAsBytes.length; i++) {
        encodedMessage[j++] = encodedFileAsBytes[i];
      }
      String message = ByteUtils.toBinary(encodedMessage);

      System.out.println("Encoded Message In File: " + message);
      Symbol.symbolListToString(symbols);

      StringBuilder decodedMessage = new StringBuilder("");
      while (message.length() != 0) {
        //                Loggin the Messages
        //                System.out.println("");
        //                logger.info("Meesage: " + message);
        //                logger.info("Message Decoded: " + decodedMessage);

        boolean flag = false;
        for (Symbol symbol : symbols) {
          if (message.startsWith(symbol.getCode())) {
            decodedMessage.append(symbol.getIdentifier());
            message = message.substring(symbol.getCode().length(), message.length());
            flag = true;
            break;
          }
        }
        /*
         * We are checking that if no symbol is there in the symbol list
         * then it is the EOF as some padding is done by the String to
         * Byte & Byte to String conversion
         */
        if (!flag) {
          break;
        }
      }
      System.out.println("Decoded Message: " + decodedMessage);

      // Writing the decoded Message in the Output File
      File file = new File(decodedFileName);
      FileOutputStream fos = new FileOutputStream(file);
      fos.write(decodedMessage.toString().getBytes());
    } catch (Exception ex) {
      logger.log(Level.WARNING, ex.toString());
      ex.printStackTrace();
    }
  }
コード例 #12
0
 protected boolean readBoolean(byte[] content) throws IOException {
   boolean value = ByteUtils.convertBooleanFromBytes(content, index);
   index += 1;
   return value;
 }
コード例 #13
0
 protected short readShort(byte[] content) throws IOException {
   short value = ByteUtils.convertShortFromBytes(content, index);
   index += 2;
   return value;
 }
コード例 #14
0
 protected double readDouble(byte[] content) throws IOException {
   double value = ByteUtils.convertDoubleFromBytes(content, index);
   index += 8;
   return value;
 }
コード例 #15
0
 protected float readFloat(byte[] content) throws IOException {
   float value = ByteUtils.convertFloatFromBytes(content, index);
   index += 4;
   return value;
 }
コード例 #16
0
ファイル: HashAlgorithm.java プロジェクト: gepp/studyDemo
  /**
   * Compute the hash for the given key.
   *
   * @return a positive integer hash
   */
  public long hash(final String k) {
    long rv = 0;
    switch (this) {
      case NATIVE_HASH:
        rv = k.hashCode();
        break;
      case CRC32_HASH:
        // return (crc32(shift) >> 16) & 0x7fff;
        CRC32 crc32 = new CRC32();
        crc32.update(ByteUtils.getBytes(k));
        rv = crc32.getValue() >> 16 & 0x7fff;
        break;
      case FNV1_64_HASH:
        {
          // Thanks to [email protected] for the pointer
          rv = FNV_64_INIT;
          int len = k.length();
          for (int i = 0; i < len; i++) {
            rv *= FNV_64_PRIME;
            rv ^= k.charAt(i);
          }
        }
        break;
      case MurMurHash:
        ByteBuffer buf = ByteBuffer.wrap(k.getBytes());
        int seed = 0x1234ABCD;

        ByteOrder byteOrder = buf.order();
        buf.order(ByteOrder.LITTLE_ENDIAN);

        long m = 0xc6a4a7935bd1e995L;
        int r = 47;

        rv = seed ^ (buf.remaining() * m);

        long ky;
        while (buf.remaining() >= 8) {
          ky = buf.getLong();

          ky *= m;
          ky ^= ky >>> r;
          ky *= m;

          rv ^= ky;
          rv *= m;
        }

        if (buf.remaining() > 0) {
          ByteBuffer finish = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN);
          // for big-endian version, do this first:
          // finish.position(8-buf.remaining());
          finish.put(buf).rewind();
          rv ^= finish.getLong();
          rv *= m;
        }

        rv ^= rv >>> r;
        rv *= m;
        rv ^= rv >>> r;
        buf.order(byteOrder);
        break;
      case FNV1A_64_HASH:
        {
          rv = FNV_64_INIT;
          int len = k.length();
          for (int i = 0; i < len; i++) {
            rv ^= k.charAt(i);
            rv *= FNV_64_PRIME;
          }
        }
        break;
      case FNV1_32_HASH:
        {
          rv = FNV_32_INIT;
          int len = k.length();
          for (int i = 0; i < len; i++) {
            rv *= FNV_32_PRIME;
            rv ^= k.charAt(i);
          }
        }
        break;
      case FNV1A_32_HASH:
        {
          rv = FNV_32_INIT;
          int len = k.length();
          for (int i = 0; i < len; i++) {
            rv ^= k.charAt(i);
            rv *= FNV_32_PRIME;
          }
        }
        break;
      case KETAMA_HASH:
        byte[] bKey = computeMd5(k);
        rv =
            (long) (bKey[3] & 0xFF) << 24
                | (long) (bKey[2] & 0xFF) << 16
                | (long) (bKey[1] & 0xFF) << 8
                | bKey[0] & 0xFF;
        break;

      case MYSQL_HASH:
        int nr2 = 4;
        for (int i = 0; i < k.length(); i++) {
          rv ^= ((rv & 63) + nr2) * k.charAt(i) + (rv << 8);
          nr2 += 3;
        }
        break;
      case ELF_HASH:
        long x = 0;
        for (int i = 0; i < k.length(); i++) {
          rv = (rv << 4) + k.charAt(i);
          if ((x = rv & 0xF0000000L) != 0) {
            rv ^= x >> 24;
            rv &= ~x;
          }
        }
        rv = rv & 0x7FFFFFFF;
        break;
      case RS_HASH:
        long b = 378551;
        long a = 63689;
        for (int i = 0; i < k.length(); i++) {
          rv = rv * a + k.charAt(i);
          a *= b;
        }
        rv = rv & 0x7FFFFFFF;
        break;
      case LUA_HASH:
        int step = (k.length() >> 5) + 1;
        rv = k.length();
        for (int len = k.length(); len >= step; len -= step) {
          rv = rv ^ (rv << 5) + (rv >> 2) + k.charAt(len - 1);
        }
      case ONE_AT_A_TIME:
        try {
          int hash = 0;
          for (byte bt : k.getBytes("utf-8")) {
            hash += (bt & 0xFF);
            hash += (hash << 10);
            hash ^= (hash >>> 6);
          }
          hash += (hash << 3);
          hash ^= (hash >>> 11);
          hash += (hash << 15);
          return hash;
        } catch (UnsupportedEncodingException e) {
          throw new IllegalStateException("Hash function error", e);
        }
      default:
        assert false;
    }

    return rv & 0xffffffffL; /* Truncate to 32-bits */
  }