Beispiel #1
1
  /**
   * If the outbound level is BINARY, convert the string field to binary, then pad to the left with
   * the appropriate number of zero bits to reach a number of bits specified by the bitLength
   * attribute of the TDT definition file.
   */
  private void binaryPadding(Map<String, String> extraparams, Field tdtfield) {
    String fieldname = tdtfield.getName();
    int reqbitlength = tdtfield.getBitLength();
    String value;

    String binaryValue = fieldToBinary(tdtfield, extraparams);
    if (binaryValue.length() < reqbitlength) {
      int extraBitLength = reqbitlength - binaryValue.length();

      StringBuilder zeroPaddedBinaryValue = new StringBuilder("");
      for (int i = 0; i < extraBitLength; i++) {
        zeroPaddedBinaryValue.append("0");
      }
      zeroPaddedBinaryValue.append(binaryValue);
      value = zeroPaddedBinaryValue.toString();
    } else {
      if (binaryValue.length() > reqbitlength)
        throw new TDTException(
            "Binary value ["
                + binaryValue
                + "] for field "
                + fieldname
                + " exceeds maximum allowed "
                + reqbitlength
                + " bits.  Decimal value was "
                + extraparams.get(fieldname));

      value = binaryValue;
    }
    extraparams.put(fieldname, value);
  }
Beispiel #2
1
 private static String convert(List<List<Particle>> lists) {
   StringBuilder sb = new StringBuilder();
   for (List<Particle> list : lists) {
     if (list.isEmpty()) sb.append('.');
     else sb.append('X');
   }
   return sb.toString();
 }
  /**
   * Return a set of the variables in the supplied expression. Note: Substitutions which are in the
   * constant table are not included.
   */
  public Set<String> getVariablesWithin(String exp) {
    Set<String> all = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
    String add = null;

    if (separators == null) {
      StringBuilder sep = new StringBuilder(10);
      for (char chr = 0; chr < operators.length; chr++) {
        if (operators[chr] != null && !operators[chr].internal) {
          sep.append(chr);
        }
      }
      sep.append("()");
      separators = sep.toString();
    }

    for (StringTokenizer tkz = new StringTokenizer(exp, separators, true); tkz.hasMoreTokens(); ) {
      String tkn = tkz.nextToken().trim();

      if (tkn.length() != 0 && Character.isLetter(tkn.charAt(0))) {
        add = tkn;
      } else if (tkn.length() == 1 && tkn.charAt(0) == '(') {
        add = null;
      } else if (add != null && !constants.containsKey(add)) {
        all.add(add);
      }
    }
    if (add != null && !constants.containsKey(add)) {
      all.add(add);
    }
    return all;
  }
Beispiel #4
0
 /**
  * Returns a string representation of all found arguments.
  *
  * @param args array with arguments
  * @return string representation
  */
 static String foundArgs(final Value[] args) {
   // compose found arguments
   final StringBuilder sb = new StringBuilder();
   for (final Value v : args) {
     if (sb.length() != 0) sb.append(", ");
     sb.append(v instanceof Jav ? Util.className(((Jav) v).toJava()) : v.seqType());
   }
   return sb.toString();
 }
  /**
   * Make a string from the contents of this JSONArray. The <code>separator</code> string is
   * inserted between each element. Warning: This method assumes that the data structure is
   * acyclical.
   *
   * @param separator A string that will be inserted between the elements.
   * @return a string.
   * @throws JSONException If the array contains an invalid number.
   */
  public String join(String separator) throws JSONException {
    int len = this.length();
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < len; i += 1) {
      if (i > 0) {
        sb.append(separator);
      }
      sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
    }
    return sb.toString();
  }
Beispiel #6
0
 /**
  * Pads a binary value supplied as a string first parameter to the left with leading zeros in
  * order to reach a required number of bits, as expressed by the second parameter, reqlen. Returns
  * a string value corresponding to the binary value left padded to the required number of bits.
  */
 private String padBinary(String binary, int reqlen) {
   String rv;
   int l = binary.length();
   int pad = (reqlen - (l % reqlen)) % reqlen;
   StringBuilder buffer = new StringBuilder("");
   for (int i = 0; i < pad; i++) {
     buffer.append("0");
   }
   buffer.append(binary);
   rv = buffer.toString();
   return rv;
 }
Beispiel #7
0
  /**
   * Returns a string built using a particular grammar. Single-quotes strings are counted as literal
   * strings, whereas all other strings appearing in the grammar require substitution with the
   * corresponding value from the extraparams hashmap.
   */
  private String buildGrammar(String grammar, Map<String, String> extraparams) {
    StringBuilder outboundstring = new StringBuilder();
    String[] fields = Pattern.compile("\\s+").split(grammar);
    for (int i = 0; i < fields.length; i++) {
      if (fields[i].substring(0, 1).equals("'")) {
        outboundstring.append(fields[i].substring(1, fields[i].length() - 1));
      } else {
        outboundstring.append(extraparams.get(fields[i]));
      }
    }

    return outboundstring.toString();
  }
Beispiel #8
0
 // Find and unescape a string.
 private String readString() {
   StringBuilder sb = new StringBuilder();
   char c = next();
   if (c != '"') throw new RuntimeException("expecting start of string");
   while (true)
     switch (c = next()) {
       case '"':
         return sb.toString();
       case '\\':
         sb.append(readEscaped());
         continue;
       default:
         if (Character.isISOControl(c)) throw new RuntimeException("illegal character in string");
         sb.append(c);
     }
 }
Beispiel #9
0
 void solve() {
   int n = nextInt();
   HashMap<Long, Integer>[] hm = new HashMap[33];
   for (int i = 0; i < hm.length; i++) {
     hm[i] = new HashMap<Long, Integer>();
   }
   boolean[] allows = new boolean[n];
   for (int i = 0; i < n; i++) {
     String s = nextToken();
     nextToken();
     String ip = nextToken();
     if (ip.indexOf("/") == -1) {
       ip = ip + "/32";
     }
     allows[i] = s.equals("allow");
     int p = ip.indexOf("/");
     String s1 = ip.substring(0, p);
     String s2 = ip.substring(p + 1);
     int e = Integer.parseInt(s2);
     long r = ipToInteger(s1);
     r &= ~((1L << (32 - e)) - 1);
     if (!hm[e].containsKey(r)) hm[e].put(r, i);
     // System.err.println("Query " + i + " = " + r);
   }
   int m = nextInt();
   StringBuilder sb = new StringBuilder();
   for (int i = 0; i < m; i++) {
     String ip = nextToken();
     long r = ipToInteger(ip);
     int min = Integer.MAX_VALUE;
     // System.err.println("Cur = " + i);
     for (int j = 0; j <= 32; j++) {
       long e = r;
       e &= ~((1L << (32 - j)) - 1);
       // System.err.println(e + " " + j);
       if (hm[j].containsKey(e)) {
         min = Math.min(min, hm[j].get(e));
       }
     }
     if (min == Integer.MAX_VALUE || allows[min]) {
       sb.append("A");
     } else {
       sb.append("D");
     }
   }
   out.println(sb);
 }
Beispiel #10
0
 public void run() {
   // Scanner sc = new Scanner(System.in);
   Scanner sc = new Scanner();
   StringBuilder sb = new StringBuilder(1000000);
   // System.setOut(new PrintStream(new BufferedOutputStream(System.out)));
   final int N = sc.nextInt();
   final int M = sc.nextInt();
   final int K = (int) floor(sqrt(N));
   final int[] A = new int[N + 1];
   A[0] = 1;
   for (int i = 1; i <= N; i++) A[i] = sc.nextInt();
   final int[] next = new int[N + 1];
   final int[] step = new int[N + 1];
   final int[] last = new int[N + 1];
   for (int i = N; i > 0; i--) {
     int j = i + A[i];
     if (j > N || j / K > i / K) {
       last[i] = i;
       step[i] = 1;
       next[i] = j;
     } else {
       last[i] = last[j];
       step[i] = step[j] + 1;
       next[i] = next[j];
     }
   }
   for (int t = 0; t < M; t++)
     if (sc.nextInt() == 1) {
       int i = sc.nextInt();
       int j = 0;
       int k = 0;
       while (i <= N) {
         j += step[i];
         k = last[i];
         i = next[i];
       }
       sb.append(k).append(' ').append(j).append('\n');
       // System.out.println(k + " " + j);
     } else {
       int k = sc.nextInt();
       int b = k / K * K;
       A[k] = sc.nextInt();
       for (int i = min(b + K - 1, N); i >= b; i--) {
         int j = i + A[i];
         if (j > N || j / K > i / K) {
           last[i] = i;
           step[i] = 1;
           next[i] = j;
         } else {
           last[i] = last[j];
           step[i] = step[j] + 1;
           next[i] = next[j];
         }
       }
     }
   // System.out.flush();
   System.out.print(sb);
 }
Beispiel #11
0
  /** pad a value according the field definition. */
  private void padField(Map<String, String> extraparams, Field field) {
    String name = field.getName();
    String value = extraparams.get(name);
    PadDirectionList padDir = field.getPadDir();
    int requiredLength = field.getLength();

    // assert value != null;
    if (value == null) return;

    String padCharString = field.getPadChar();
    // if no pad char specified, don't attempt padding
    if (padCharString == null) return;
    assert padCharString.length() > 0;
    char padChar = padCharString.charAt(0);

    StringBuilder buf = new StringBuilder(requiredLength);
    if (padDir == PadDirectionList.LEFT) {
      for (int i = 0; i < requiredLength - value.length(); i++) buf.append(padChar);
      buf.append(value);
    } else if (padDir == PadDirectionList.RIGHT) {
      buf.append(value);
      for (int i = 0; i < requiredLength - value.length(); i++) buf.append(padChar);
    }
    assert buf.length() == requiredLength;
    if (requiredLength != value.length()) {
      // System.out.println("    updated " + name + " to '" + buf + "'");
      extraparams.put(name, buf.toString());
    }
    /*
    else {
        StringBuilder mybuf = new StringBuilder();
        for (int i = 0; i < value.length(); i++) {
    	if (i > 0)
    	    mybuf.append(',');
    	mybuf.append('\'');
    	mybuf.append(value.charAt(i));
    	mybuf.append('\'');
        }


        System.out.println("    field " + name + " not padded as " + mybuf.toString() + " is already " + requiredLength + " characters long");
    }
    */
  }
Beispiel #12
0
 /**
  * Converts an upper case character string input into a binary string, using 5-bit compaction of
  * each ASCII byte
  */
 private String uppercasefive2bin(String uppercasefive) {
   String binary;
   StringBuilder buffer = new StringBuilder("");
   int len = uppercasefive.length();
   byte[] bytes = uppercasefive.getBytes();
   for (int i = 0; i < len; i++) {
     buffer.append(padBinary(dec2bin(Integer.toString(bytes[i] % 32)), 8).substring(3, 8));
   }
   binary = buffer.toString();
   return binary;
 }
Beispiel #13
0
 /**
  * Converts a binary string input into an ASCII string output, assuming that 7-bit compaction was
  * used
  */
 private String bin2asciiseven(String binary) {
   String asciiseven;
   StringBuilder buffer = new StringBuilder("");
   int len = binary.length();
   for (int i = 0; i < len; i += 7) {
     int j = Integer.parseInt(bin2dec(padBinary(binary.substring(i, i + 7), 8)));
     buffer.append((char) j);
   }
   asciiseven = buffer.toString();
   return asciiseven;
 }
Beispiel #14
0
 /**
  * Converts an ASCII string input into a binary string, using 7-bit compaction of each ASCII byte
  */
 private String asciiseven2bin(String asciiseven) {
   String binary;
   StringBuilder buffer = new StringBuilder("");
   int len = asciiseven.length();
   byte[] bytes = asciiseven.getBytes();
   for (int i = 0; i < len; i++) {
     buffer.append(padBinary(dec2bin(Integer.toString(bytes[i] % 128)), 8).substring(1, 8));
   }
   binary = buffer.toString();
   return binary;
 }
Beispiel #15
0
 /** Converts a byte string input into a binary string, using 8-bits per character byte */
 private String bin2bytestring(String binary) {
   String bytestring;
   StringBuilder buffer = new StringBuilder("");
   int len = binary.length();
   for (int i = 0; i < len; i += 8) {
     int j = Integer.parseInt(bin2dec(padBinary(binary.substring(i, i + 8), 8)));
     buffer.append((char) j);
   }
   bytestring = buffer.toString();
   return bytestring;
 }
Beispiel #16
0
 /** Converts a binary string input into a byte string, using 8-bits per character byte */
 private String bytestring2bin(String bytestring) {
   String binary;
   StringBuilder buffer = new StringBuilder("");
   int len = bytestring.length();
   byte[] bytes = bytestring.getBytes();
   for (int i = 0; i < len; i++) {
     buffer.append(padBinary(dec2bin(Integer.toString(bytes[i])), 8));
   }
   binary = buffer.toString();
   return binary;
 }
Beispiel #17
0
 /**
  * Converts a binary string input into a character string output, assuming that 5-bit compaction
  * was used
  */
 private String bin2uppercasefive(String binary) {
   String uppercasefive;
   StringBuilder buffer = new StringBuilder("");
   int len = binary.length();
   for (int i = 0; i < len; i += 5) {
     int j = Integer.parseInt(bin2dec(padBinary(binary.substring(i, i + 5), 8)));
     buffer.append((char) (j + 64));
   }
   uppercasefive = buffer.toString();
   return uppercasefive;
 }
Beispiel #18
0
 /**
  * Converts an alphanumeric string input into a binary string, using 6-bit compaction of each
  * ASCII byte
  */
 private String alphanumsix2bin(String alphanumsix) {
   String binary;
   StringBuilder buffer = new StringBuilder("");
   int len = alphanumsix.length();
   byte[] bytes = alphanumsix.getBytes();
   for (int i = 0; i < len; i++) {
     buffer.append(padBinary(dec2bin(Integer.toString(bytes[i] % 64)), 8).substring(2, 8));
   }
   binary = buffer.toString();
   return binary;
 }
Beispiel #19
0
 private Number readNumber() {
   char c;
   boolean d = false;
   StringBuilder sb = new StringBuilder();
   while (true)
     switch (c = peek()) {
       case '.':
       case 'e':
       case 'E':
       case '+':
         d = true;
       case '-':
         sb.append(next());
         continue;
       default:
         if (check(c, '0', '9')) sb.append(next());
         else if (check(c, ",:; \n\r\t>}])")) {
           return d ? new BigDecimal(sb.toString()) : new BigInteger(sb.toString());
         } else throw new RuntimeException("unexpected character: " + c);
     }
 }
Beispiel #20
0
  public static void main(String args[]) {

    int NUM_LETTERS = 27;
    char[] alfabet = {
      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
      'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '?'
    };

    Scanner in = new Scanner(System.in);
    int L = in.nextInt();
    in.nextLine();
    int H = in.nextInt();
    in.nextLine();
    // to store the letter in asci artM
    char[][] art = new char[H][L * NUM_LETTERS];
    String T = in.nextLine(); // text to translate

    System.err.println("create array L:" + L + "/H:" + H + "/to translate:" + T);

    // saving asci art
    for (int i = 0; i < H; i++) {
      String ROW = in.nextLine(); // first line of all letters in asci art
      // the ROW must be saved in L chars groups
      // System.err.println("readed->" + ROW);
      char[] aux = ROW.toCharArray();
      for (int j = 0; j < aux.length; j++) {
        // System.err.println("guardando->["+i+"]["+j+"]" + aux[j]);
        art[i][j] = aux[j];
      }
    }

    StringBuilder builder = new StringBuilder();
    char[] translation = T.toCharArray();
    for (int m = 0; m < H; m++) {
      StringBuilder linea = new StringBuilder();
      for (int j = 0; j < translation.length; j++) {
        // neet to get char -> letter number to get position
        int position = returnPosition(Character.toUpperCase(translation[j]), alfabet);

        int start = (position * L);
        for (int u = start; u < (start + L); u++) {
          linea.append(art[m][u]);
        }
      }
      System.out.println(linea);
    }

    // Write an action using System.out.println()
    // To debug: System.err.println("Debug messages...");

  }
Beispiel #21
0
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String s = in.next();

    int l = s.length();
    Double sqrl = Math.sqrt(l);
    Double low = Math.floor(sqrl);
    Double high = Math.ceil(sqrl);
    int r = 0;
    int c = 0;

    if (low == high) {
      r = low.intValue();
      c = low.intValue();
    } else if (low * high >= l) {
      r = low.intValue();
      c = high.intValue();
    } else {
      r = high.intValue();
      c = high.intValue();
    }

    //        System.out.println("Row: " + r);
    //        System.out.println("Col: " + c);

    StringBuilder sb = new StringBuilder();
    char en[] = s.toCharArray();
    for (int i = 0; i < c; i++) {
      for (int j = 0; j < r; j++) {
        if (j * c + i < l) {
          sb.append(en[j * c + i]);
        }
      }
      sb.append(" ");
    }

    System.out.println(sb.toString());
  }
Beispiel #22
0
 /**
  * Converts a binary string input into a character string output, assuming that 6-bit compaction
  * was used
  */
 private String bin2alphanumsix(String binary) {
   String alphanumsix;
   StringBuilder buffer = new StringBuilder("");
   int len = binary.length();
   for (int i = 0; i < len; i += 6) {
     int j = Integer.parseInt(bin2dec(padBinary(binary.substring(i, i + 6), 8)));
     if (j < 32) {
       j += 64;
     }
     buffer.append((char) j);
   }
   alphanumsix = buffer.toString();
   return alphanumsix;
 }
Beispiel #23
0
 public String readword() throws IOException {
   StringBuilder b = new StringBuilder();
   int c;
   c = in.read();
   while (c >= 0 && c <= ' ') {
     c = in.read();
   }
   if (c < 0) {
     return "";
   }
   while (c > ' ') {
     b.append((char) c);
     c = in.read();
   }
   return b.toString();
 }
Beispiel #24
0
  public static void main(String[] args) throws IOException {

    Scanner scanner = new Scanner(new BufferedInputStream(System.in));
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int num;
    while ((num = scanner.nextInt()) != 0) {
      ArrayList<String> all = new ArrayList<String>();
      for (int i = 0; i < num; i++) {
        all.add(scanner.next());
      }
      Collections.sort(all, comptr);
      StringBuilder sb = new StringBuilder("");
      for (String s : all) {
        sb.append(s);
      }
      System.out.println(sb);
    }
  }
Beispiel #25
0
 // Try to read an atom.
 private Atom readAtom() {
   StringBuilder sb = new StringBuilder();
   for (char c = peek(); validAtomPart(c); c = peek()) sb.append(next());
   return new Atom(sb.toString());
 }
Beispiel #26
0
  /**
   * Adds additional entries to the extraparams hashmap by processing various rules defined in the
   * TDT definition files. Typically used for string processing functions, lookup in tables,
   * calculation of check digits etc.
   */
  private void processRules(Map<String, String> extraparams, Rule tdtrule) {
    String tdtfunction = tdtrule.getFunction();
    int openbracket = tdtfunction.indexOf("(");
    assert openbracket != -1;
    String params = tdtfunction.substring(openbracket + 1, tdtfunction.length() - 1);
    String rulename = tdtfunction.substring(0, openbracket);
    String[] parameter = params.split(",");
    String newfieldname = tdtrule.getNewFieldName();
    // System.out.println(tdtfunction + " " + parameter[0] + " " + extraparams.get(parameter[0]));
    /**
     * Stores in the hashmap extraparams the value obtained from a lookup in a specified XML table.
     *
     * <p>The first parameter is the given value already known. This is denoted as $1 in the
     * corresponding XPath expression
     *
     * <p>The second parameter is the string filename of the table which must be present in the
     * auxiliary subdirectory
     *
     * <p>The third parameter is the column in which the supplied input value should be sought
     *
     * <p>The fourth parameter is the column whose value should be read for the corresponding row,
     * in order to obtain the result of the lookup.
     *
     * <p>The rule in the definition file may contain an XPath expression and a URL where the table
     * may be obtained.
     */
    if (rulename.equals("TABLELOOKUP")) {
      // parameter[0] is given value
      // parameter[1] is table
      // parameter[2] is input column supplied
      // parameter[3] is output column required
      assert parameter.length == 4 : "incorrect number of parameters to tablelookup " + params;
      if (parameter[1].equals("tdt64bitcpi")) {
        String s = extraparams.get(parameter[0]);
        assert s != null : tdtfunction + " when " + parameter[0] + " is null";
        String t = gs1cpi.get(s);
        assert t != null : "gs1cpi[" + s + "] is null";
        assert newfieldname != null;
        extraparams.put(newfieldname, t);
        // extraparams.put(newfieldname, gs1cpi.get(extraparams.get(parameter[0])));
      } else { // JPB! the following is untested
        String tdtxpath = tdtrule.getTableXPath();
        String tdttableurl = tdtrule.getTableURL();
        String tdtxpathsub = tdtxpath.replaceAll("\\$1", extraparams.get(parameter[0]));
        extraparams.put(newfieldname, xpathlookup("ManagerTranslation.xml", tdtxpathsub));
      }
    }

    /**
     * Stores the length of the specified string under the new fieldname specified by the
     * corresponding rule of the definition file.
     */
    if (rulename.equals("LENGTH")) {
      assert extraparams.get(parameter[0]) != null
          : tdtfunction + " when " + parameter[0] + " is null";
      if (extraparams.get(parameter[0]) != null) {
        extraparams.put(newfieldname, Integer.toString(extraparams.get(parameter[0]).length()));
      }
    }

    /**
     * Stores a GS1 check digit in the extraparams hashmap, keyed under the new fieldname specified
     * by the corresponding rule of the definition file.
     */
    if (rulename.equals("GS1CHECKSUM")) {
      assert extraparams.get(parameter[0]) != null
          : tdtfunction + " when " + parameter[0] + " is null";
      if (extraparams.get(parameter[0]) != null) {
        extraparams.put(newfieldname, gs1checksum(extraparams.get(parameter[0])));
      }
    }

    /**
     * Obtains a substring of the string provided as the first parameter. If only a single second
     * parameter is specified, then this is considered as the start index and all characters from
     * the start index onwards are stored in the extraparams hashmap under the key named
     * 'newfieldname' in the corresponding rule of the definition file. If a second and third
     * parameter are specified, then the second parameter is the start index and the third is the
     * length of characters required. A substring consisting characters from the start index up to
     * the required length of characters is stored in the extraparams hashmap, keyed under the new
     * fieldname specified by the corresponding rule of the defintion file.
     */
    if (rulename.equals("SUBSTR")) {
      assert extraparams.get(parameter[0]) != null
          : tdtfunction + " when " + parameter[0] + " is null";
      if (parameter.length == 2) {
        if (extraparams.get(parameter[0]) != null) {
          int start = getIntValue(parameter[1], extraparams);
          if (start >= 0) {
            extraparams.put(newfieldname, extraparams.get(parameter[0]).substring(start));
          }
        }
      }
      if (parameter.length
          == 3) { // need to check that this variation is correct - c.f. Perl substr
        assert extraparams.get(parameter[0]) != null
            : tdtfunction + " when " + parameter[0] + " is null";
        if (extraparams.get(parameter[0]) != null) {
          int start = getIntValue(parameter[1], extraparams);
          int end = getIntValue(parameter[2], extraparams);
          if ((start >= 0) && (end >= 0)) {
            extraparams.put(
                newfieldname, extraparams.get(parameter[0]).substring(start, start + end));
          }
        }
      }
    }

    /**
     * Concatenates specified string parameters together. Literal values must be enclosed within
     * single or double quotes or consist of unquoted digits. Other unquoted strings are considered
     * as fieldnames and the corresponding value from the extraparams hashmap are inserted. The
     * result of the concatenation (and substitution) of the strings is stored as a new entry in the
     * extraparams hashmap, keyed under the new fieldname specified by the rule.
     */
    if (rulename.equals("CONCAT")) {
      StringBuilder buffer = new StringBuilder();
      for (int p1 = 0; p1 < parameter.length; p1++) {
        Matcher matcher = Pattern.compile("\"(.*?)\"|'(.*?)'|[0-9]").matcher(parameter[p1]);
        if (matcher.matches()) {
          buffer.append(parameter[p1]);
        } else {
          assert extraparams.get(parameter[p1]) != null
              : tdtfunction + " when " + parameter[p1] + " is null";
          if (extraparams.get(parameter[p1]) != null) {
            buffer.append(extraparams.get(parameter[p1]));
          }
        }
      }
      extraparams.put(newfieldname, buffer.toString());
    }
  }
Beispiel #27
0
  /* -------------------------------------------------------------------------------------------------------
  This method:

  	This method:
  	--	Takes in three paramters:
  		1. array - this is the byte array that actually holds the document contents
  		2. md5Hases - holds the entire hash values of the document
  		3. Divisor1/Divisor2/divisor3... - main and back up divisors
  		5. The remainder we are looking for
  		6/7. min/max boundaries

  	-- We will start running the karb rabin algorithm
  	-- We will find the boundaries using mod values and once they equal the mod value we have stored
  	-- we also have the divsor2/3 .. which are backup divisors. If we don't find a boundary by the divisor1 once we hit the maxBoundary
  	-- we will see if we have one with divisor2, if not, then we will see if we have one with divisor3 and so on
  	-- We will hash everything in that hash boundary and store it
  -------------------------------------------------------------------------------------------------------- */
  private static void runTddd(
      byte[] array,
      ArrayList<Long> md5Hashes,
      long divisor1,
      long divisor2,
      long divisor3,
      long remainder,
      long minBoundary,
      long maxBoundary) {
    int documentStart = 0; // used to keep track of where the boundaries are
    boolean match = false; // used to ck if we encountered a match
    int backUpBreakPoint = -1; // used to store the backup breakpoint
    int secondBackUpBreakPoint = -1; // used with the divisor3
    StringBuilder builder = new StringBuilder();
    int i = documentStart + (int) minBoundary - 1; // so we start at the minimum
    // loop through all the values in the document
    for (; i < md5Hashes.size() - 1; ++i) {
      // if ((i - documentStart + 1) < minBoundary) //  if the size of this boundary is less than
      // the min, continue looping
      // 	continue;
      /*-----------------------------------------------------------------
      	- If the mod of this equals the modvalue we defined, then
      	- this is a boundary
      ------------------------------------------------------------------*/

      if ((md5Hashes.get(i - 1) + md5Hashes.get(i) + md5Hashes.get(i + 1)) % divisor1
          == remainder) // ck if this equals the mod value
      {
        // Hash all the values in the range (documentStart,current(i))
        // Remember we only want to hash the original VALUES from the array that contains the
        // original
        // content of the file. Not the hash values in the md5Hash Array
        for (int j = documentStart; j <= i; ++j) {
          builder.append(array[j]); // store everything upto the current value
        }
        String original = builder.toString();
        // if the string is a perfect match ( hash and original string)
        if (HashClass.is_string_match(original, table)) // iinsert the hash in the table)
        coverage += i - documentStart + 1; // this is the amount of bytes we saved
        documentStart = i + 1; // set this as the beginning of the new boundary
        backUpBreakPoint = -1; // reset this
        secondBackUpBreakPoint = -1;
        numOfPieces++; // increment the num of pieces
        i = i + (int) minBoundary - 1; // skip all the way here
        builder.setLength(0); // reset the stringBuilder for the next round
      } else if ((md5Hashes.get(i - 1) + md5Hashes.get(i) + md5Hashes.get(i + 1)) % divisor2
          == remainder) { //  check if this is the backup point
        backUpBreakPoint = i; // this is the backup breakpoint
      } else if ((md5Hashes.get(i - 1) + md5Hashes.get(i) + md5Hashes.get(i + 1)) % divisor2
          == remainder) {
        secondBackUpBreakPoint = i; // set the second backup point
      }
      if ((i - documentStart + 1) >= maxBoundary) { // we have reached the maximum
        // ck if we have a backUpbreakpoint
        int point;
        if (backUpBreakPoint != -1) // if we do, set this as the boundary
        point = backUpBreakPoint;
        else if (secondBackUpBreakPoint != -1) {
          point = secondBackUpBreakPoint; // if we don't have a first break point, find the second
        } else point = i; // else this current value of i is the breakpoint

        // Hash all the values in the range (documentStart,current(i))
        // Remember we only want to hash the original VALUES from the array that contains the
        // original
        // content of the file. Not the hash values in the md5Hash Array
        for (int j = documentStart; j <= point; ++j) {
          builder.append(array[j]); // store everything upto the current value
        }
        String original = builder.toString();
        if (HashClass.is_string_match(original, table))
          coverage += point - documentStart + 1; // this is the amount of bytes we saved
        numOfPieces++; // increment the num of pieces
        documentStart = point + 1; // set this as the beginning of the new boundary
        backUpBreakPoint = -1; // reset this
        secondBackUpBreakPoint = -1; // reset the secondBackUp point
        i = point + (int) minBoundary - 1; // skip all the way here ;
        builder.setLength(0); // reset the stringBuilder for the next round
      }
    } // end of the for loop

    // -------------------------------------------------------------------------------------------
    //  we are missing the last boundary, so hash that last value
    //	We will also check against our values of the strings we already have, and if we encountered
    // this
    //	already, then we will simply increment the counter, otherwise we will insert it in the
    // hashtable
    //	and increase our miss counter
    // ----------------------------------------------------------------------------------------------
    for (int j = documentStart; j < array.length; ++j) {
      builder.append(array[j]);
    }
    // only compute hash and insert into our hashtable only if the string buffer isn't empty
    String original = builder.toString();
    if (HashClass.is_string_match(original, table))
      coverage += array.length - documentStart; // this is the amount of bytes we saved
    numOfPieces++; // increment the num of pieces
  } // end of the method