private String readLine(int ci) {
    int last_ch = -1;

    fTmpBuffer.setLength(0);
    while (ci != -1 && ci != '\n' || last_ch == '\\') {
      if (last_ch == '\\' && ci == '\n') {
        if (fTmpBuffer.charAt(fTmpBuffer.length() - 1) == '\r') {
          fTmpBuffer.setLength(fTmpBuffer.length() - 1);
        }
        if (fTmpBuffer.charAt(fTmpBuffer.length() - 1) == '\\') {
          fTmpBuffer.setCharAt(fTmpBuffer.length() - 1, '\n');
        }
      } else {
        fTmpBuffer.append((char) ci);
      }

      if (ci != '\r') {
        last_ch = ci;
      }

      ci = get_ch();
    }

    unget_ch(ci);

    if (fTmpBuffer.length() == 0) {
      return null;
    } else {
      return fTmpBuffer.toString();
    }
  }
 @TCB
 private static void stripBannedCodeunits(StringBuilder sb, int start) {
   int k = start;
   for (int i = start, n = sb.length(); i < n; ++i) {
     char ch = sb.charAt(i);
     if (ch < 0x20) {
       if (IS_BANNED_ASCII[ch]) {
         continue;
       }
     } else if (0xd800 <= ch) {
       if (ch <= 0xdfff) {
         if (i + 1 < n) {
           char next = sb.charAt(i + 1);
           if (Character.isSurrogatePair(ch, next)) {
             sb.setCharAt(k++, ch);
             sb.setCharAt(k++, next);
             ++i;
           }
         }
         continue;
       } else if ((ch & 0xfffe) == 0xfffe) {
         continue;
       }
     }
     sb.setCharAt(k++, ch);
   }
   sb.setLength(k);
 }
  private String readLine(ServletInputStream in) throws IOException {
    StringBuilder sbuf = new StringBuilder();
    int result;
    do {
      result = in.readLine(buf, 0, buf.length); // does +=
      if (result != -1) {
        sbuf.append(new String(buf, 0, result, encoding));
      }
    } while (result == buf.length); // loop only if the buffer was filled

    if (sbuf.length() == 0) {
      return null; // nothing read, must be at the end of stream
    }

    // Cut off the trailing \n or \r\n
    // It should always be \r\n but IE5 sometimes does just \n
    // Thanks to Luke Blaikie for helping make this work with \n
    int len = sbuf.length();
    if (len >= 2 && sbuf.charAt(len - 2) == '\r') {
      sbuf.setLength(len - 2); // cut \r\n
    } else if (len >= 1 && sbuf.charAt(len - 1) == '\n') {
      sbuf.setLength(len - 1); // cut \n
    }
    return sbuf.toString();
  }
Exemple #4
1
 public static void main(String[] args) {
   Scanner scanner = new Scanner(System.in);
   int m = scanner.nextInt();
   assert 1 <= m && m <= 100 : "out of range, m: " + m;
   int s = scanner.nextInt();
   assert 0 <= s && s <= 900 : "out of range, s: " + s;
   if (s > 9 * m || (s == 0 && m > 1)) {
     System.out.println("-1 -1");
     return;
   }
   if (m == 1 && s == 0) {
     System.out.println("0 0");
     return;
   }
   StringBuilder sb = new StringBuilder();
   int l = 0;
   for (int i = 0; i < m; i++) {
     int d = (s >= 9) ? 9 : s;
     sb.append(d);
     s -= d;
     if (d != 0) {
       l = i;
     }
   }
   String large = sb.toString();
   if (sb.charAt(m - 1) == '0') {
     sb.setCharAt(l, (char) (sb.charAt(l) - 1));
     sb.setCharAt(m - 1, '1');
   }
   String small = sb.reverse().toString();
   System.out.printf("%s %s", small, large);
 }
  /** remove leading spaces */
  public static final String stripSpaces(final String data) {
    final StringBuilder text = new StringBuilder((data));

    // strip leading chars
    while (text.length() > 0) {
      if (text.charAt(0) != ' ') {
        break;
      }
      text.deleteCharAt(0);

      // strip ending chars
      int pointer2 = text.length() - 1;
      while (pointer2 > 0) {
        if (text.charAt(pointer2) != ' ') {
          break;
        }
        text.deleteCharAt(pointer2);
        pointer2--;
        if (pointer2 < 0) {
          break;
        }
      }
    }
    return text.toString();
  }
  /** @param args */
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System.in);
    int times = Integer.parseInt(input.next());
    for (int x = 0; x < times; x++) {
      String next = input.next();
      int pos = (next.length() - 1) / 2;
      String res = "";
      StringBuilder build = new StringBuilder(next);
      String middle = "";
      if (next.length() % 2 == 0) {
        boolean carry = false;

        for (; pos >= 0; pos--) {
          int left = Character.getNumericValue(build.charAt(pos));
          int right = Character.getNumericValue(build.charAt(build.length() - 1 - pos));
          if (left < right) {
            left++;
            build.setCharAt(pos, Character.forDigit(left, 10));
            break;
          } else {

          }
        }
      } else {

      }

      System.out.println(res);
    }
  }
Exemple #7
1
  private String getSegment() {

    String segment = null;

    int pos = m_sb.indexOf(SEPARATOR);
    int sl = SEPARATOR.length();

    if (pos > 0 && pos + sl < m_sb.length() && m_sb.charAt(pos + sl) == '\r') {
      sl++;
    }

    if (pos > 0 && pos + sl < m_sb.length() && m_sb.charAt(pos + sl) == '\n') {
      sl++;
    }

    if (pos >= 0) {
      segment = m_sb.substring(0, pos);

      m_sb.delete(0, pos + sl);
    } else {
      pos = m_sb.indexOf(TERMINATOR);

      if (pos >= 0) {
        m_last = true;
        segment = m_sb.substring(0, pos);
      }
    }

    return segment;
  }
      /** {@inheritDoc} */
      @Override
      public ByteString normalizeValue(ByteSequence value) throws DirectoryException {
        StringBuilder buffer = new StringBuilder();
        prepareUnicode(buffer, value, TRIM, CASE_FOLD);

        int bufferLength = buffer.length();
        if (bufferLength == 0) {
          if (value.length() > 0) {
            // This should only happen if the value is composed entirely
            // of spaces. In that case, the normalized value is a single space.
            return SINGLE_SPACE_VALUE;
          } else {
            // The value is empty, so it is already normalized.
            return ByteString.empty();
          }
        }

        // Replace any consecutive spaces with a single space.
        for (int pos = bufferLength - 1; pos > 0; pos--) {
          if (buffer.charAt(pos) == ' ') {
            if (buffer.charAt(pos - 1) == ' ') {
              buffer.delete(pos, pos + 1);
            }
          }
        }

        return ByteString.valueOf(buffer.toString());
      }
Exemple #9
1
  // build query
  @Override
  protected String buildQueryUrl(RequestParams params) {
    String uri = params.getUri();
    StringBuilder queryBuilder = new StringBuilder(uri);
    if (!uri.contains("?")) {
      queryBuilder.append("?");
    } else if (!uri.endsWith("?")) {
      queryBuilder.append("&");
    }
    HashMap<String, String> queryParams = params.getQueryStringParams();
    if (queryParams != null) {
      for (Map.Entry<String, String> entry : queryParams.entrySet()) {
        String name = entry.getKey();
        String value = entry.getValue();
        if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(value)) {
          queryBuilder.append(Uri.encode(name)).append("=").append(Uri.encode(value)).append("&");
        }
      }
      if (queryBuilder.charAt(queryBuilder.length() - 1) == '&') {
        queryBuilder.deleteCharAt(queryBuilder.length() - 1);
      }
    }

    if (queryBuilder.charAt(queryBuilder.length() - 1) == '?') {
      queryBuilder.deleteCharAt(queryBuilder.length() - 1);
    }
    return queryBuilder.toString();
  }
Exemple #10
1
  private void addUsernameCookie(
      String username,
      String firstname,
      String lastname,
      HttpServletRequest request,
      HttpServletResponse response) {
    StringBuilder un = new StringBuilder(new String(Base64.encodeBase64(username.getBytes())));
    while (un.charAt(un.length() - 1) == '=') {
      un.deleteCharAt(un.length() - 1);
    }
    response.addCookie(createCookie(USERNAME, un.toString(), request));

    StringBuilder fn = new StringBuilder();
    if (!StringUtils.isBlank(firstname)) {
      fn.append(firstname);
    }
    fn.append(":");
    if (!StringUtils.isBlank(lastname)) {
      fn.append(lastname);
    }
    String value = fn.toString();
    fn = new StringBuilder(new String(Base64.encodeBase64(value.getBytes())));
    while (fn.charAt(fn.length() - 1) == '=') {
      fn.deleteCharAt(fn.length() - 1);
    }
    Cookie c = createCookie(FULLNAME, fn.toString(), request);
    c.setMaxAge(Integer.MAX_VALUE);
    response.addCookie(c);
  }
 private StringBuilder appendNewLine(StringBuilder buffer) {
   if (buffer.length() != 0 && buffer.charAt(buffer.length() - 1) != '\n') {
     while (buffer.charAt(buffer.length() - 1) == ' ') {
       buffer.setLength(buffer.length() - 1);
     }
     buffer.append("\n");
   }
   return buffer;
 }
 private static void trim(StringBuilder s) {
   // remove leading line breaks
   while (s.length() > 0 && (s.charAt(0) == '\n' || s.charAt(0) == '\r')) s.deleteCharAt(0);
   // remove trailing line breaks
   while (s.length() > 0 && (s.charAt(s.length() - 1) == '\n' || s.charAt(s.length() - 1) == '\r'))
     s.deleteCharAt(s.length() - 1);
 }
Exemple #13
0
 private static void a(
     BitSource paramBitSource,
     StringBuilder paramStringBuilder,
     int paramInt,
     boolean paramBoolean) {
   int i = paramStringBuilder.length();
   while (paramInt > 1) {
     int j = paramBitSource.readBits(11);
     paramStringBuilder.append(a(j / 45));
     paramStringBuilder.append(a(j % 45));
     paramInt -= 2;
   }
   if (paramInt == 1) paramStringBuilder.append(a(paramBitSource.readBits(6)));
   if (paramBoolean)
     if (i < paramStringBuilder.length()) {
       if (paramStringBuilder.charAt(i) == '%') {
         if ((i >= -1 + paramStringBuilder.length()) || (paramStringBuilder.charAt(i + 1) != '%'))
           break label133;
         paramStringBuilder.deleteCharAt(i + 1);
       }
       while (true) {
         i++;
         break;
         label133:
         paramStringBuilder.setCharAt(i, '\035');
       }
     }
 }
 /**
  * Scan one or more spaces or tab characters, combining them to form an "indent".
  *
  * @return
  */
 public Token scanIndent() {
   int start = pos;
   while (pos < input.length() && (input.charAt(pos) == ' ' || input.charAt(pos) == '\t')) {
     pos++;
   }
   return new Token(Token.Kind.Indent, input.substring(start, pos), start);
 }
 public String Reduce(String w) {
   int done = 0;
   StringBuilder sb = new StringBuilder(w);
   // System.out.println("Checking "+w);
   while (done == 0) {
     done = 1;
     int i = 0;
     while (i < sb.length()) {
       //	System.out.println("*"+sb.length());
       //	System.out.println(i);
       if (sb.charAt(i) == this.barL.get(sb.charAt((i + 1) % (sb.length())))) {
         if (i == 0) {
           sb.delete(0, 2);
           done = 0;
           i = 0;
         } else if (i < sb.length() - 1) {
           sb.delete(i, i + 2);
           done = 0;
           i = 0;
         } else if (i == sb.length() - 1) {
           sb.deleteCharAt(i);
           sb.deleteCharAt(0);
           done = 0;
           i = 0;
         }
       } else i++;
     }
   }
   return sb.toString();
 }
 private static void reverse(StringBuilder sb, int start, int end) {
   for (int i = start, j = end; --j > i; ++i) {
     char c = sb.charAt(i), d = sb.charAt(j);
     sb.setCharAt(i, d);
     sb.setCharAt(j, c);
   }
 }
  /**
   * Formats a URI by adding a forward slash and removing the last forward slash from the URI.
   *
   * <p>e.g. some/random/uri/ -> /some/random/uri e.g. some/random/uri -> /some/random/uri e.g.
   * /some/random/uri/ -> /some/random/uri e.g. / -> / e.g. ////// -> /
   *
   * @param uri
   * @return
   */
  public static String formatUri(String uri) {
    if (StringUtilities.nullSafeStartsWith(uri, "\\")) {
      // windows file system
      return uri;
    }

    if (StringUtilities.isBlank(uri) || StringUtilities.nullSafeEqualsIgnoreCase("/", uri)) {
      return "/";
    }

    final StringBuilder externalName = new StringBuilder(uri);

    if (externalName.charAt(0) != '/') {
      externalName.insert(0, "/");
    }

    int doubleSlash = externalName.indexOf("//");

    while (doubleSlash > -1) {
      // removes leading '/'
      externalName.replace(doubleSlash, doubleSlash + 2, "/");
      doubleSlash = externalName.indexOf("//");
    }

    if (externalName.charAt(externalName.length() - 1) == '/' && externalName.length() != 1) {
      externalName.deleteCharAt(externalName.length() - 1);
    }

    return externalName.toString();
  }
Exemple #18
0
 public static String makePretty(String str) {
   char lastChar = ' ';
   StringBuilder sb = new StringBuilder(str);
   int whitespaceDist = 0;
   for (int i = 0; i < sb.length(); i++) {
     char nowChar = sb.charAt(i);
     if (nowChar != ' ' && nowChar != '.') {
       whitespaceDist += 1;
     } else {
       if (whitespaceDist == 2) {
         char b = sb.charAt(i - 1);
         char a = sb.charAt(i - 2);
         sb.setCharAt(i - 1, Character.toUpperCase(b));
         sb.setCharAt(i - 2, Character.toUpperCase(a));
       }
     }
     if (lastChar == ' ' || lastChar == '/') {
       sb.setCharAt(i, Character.toUpperCase(nowChar));
     } else {
       sb.setCharAt(i, Character.toLowerCase(nowChar));
     }
     lastChar = nowChar;
   }
   return sb.toString();
 }
  @Nullable
  private static String trimToValue(@Nullable StringBuilder output) {
    if (output == null) {
      return null;
    }
    int pos = output.lastIndexOf("  ");
    int pos2 = output.lastIndexOf("\t");
    pos = Math.max(pos, pos2);
    if (pos == -1) {
      return null;
    }

    output.delete(0, pos + 1);
    String blackList = "\r\n \"";
    int startPos = 0;
    int endPos = output.length() - 1;
    while (true) {
      if (startPos >= endPos) {
        return null;
      }
      if (blackList.indexOf(output.charAt(startPos)) != -1) {
        startPos++;
      } else {
        break;
      }
    }
    while (true) {
      if (blackList.indexOf(output.charAt(endPos)) != -1) {
        endPos--;
      } else {
        break;
      }
    }
    return output.subSequence(startPos, endPos + 1).toString();
  }
  public int lengthOfLongestSubstring(String s) {
    if (s == null || s.length() == 0) return 0;

    StringBuilder cur = new StringBuilder(), opt = new StringBuilder();
    Map<Character, Integer> cur2idx = new HashMap<>(), opt2idx = new HashMap<>();

    for (int i = 0; i < s.length(); ++i) {
      char c = s.charAt(i);

      if (cur2idx.containsKey(c)) {
        // if current path is rejected on c, update the competitor with it
        // iff it's the best path seen so far
        opt = cur.length() >= opt.length() ? cur : opt;
        opt2idx = cur2idx.size() >= opt2idx.size() ? cur2idx : opt2idx;

        // start building the next candidate to compete with opt the competitor
        int idx = cur2idx.get(c);
        for (int j = 0; j < cur.length(); ++j) {
          if (j <= idx) cur2idx.remove(cur.charAt(j));
          else cur2idx.put(cur.charAt(j), j - idx - 1);
        }
        cur = new StringBuilder(cur.substring(idx + 1, cur.length()));
      }
      cur.append(c);
      cur2idx.put(c, cur2idx.size());
    }

    return cur.length() > opt.length() ? cur.length() : opt.length();
  }
  @Override
  public T getPath(String first, String... more) {
    StringBuilder sb = new StringBuilder();
    if (!GenericUtils.isEmpty(first)) {
      appendDedupSep(sb, first.replace('\\', '/')); // in case we are running on Windows
    }

    if (GenericUtils.length(more) > 0) {
      for (String segment : more) {
        if ((sb.length() > 0) && (sb.charAt(sb.length() - 1) != '/')) {
          sb.append('/');
        }
        // in case we are running on Windows
        appendDedupSep(sb, segment.replace('\\', '/'));
      }
    }

    if ((sb.length() > 1) && (sb.charAt(sb.length() - 1) == '/')) {
      sb.setLength(sb.length() - 1);
    }

    String path = sb.toString();
    String root = null;
    if (path.startsWith("/")) {
      root = "/";
      path = path.substring(1);
    }

    String[] names = GenericUtils.split(path, '/');
    return create(root, names);
  }
Exemple #22
0
  public static float toNumber(String str) {
    StringBuilder sb = new StringBuilder();
    int dotCount = 0;

    if (str.length() == 0) return 0;

    for (int i = 0; i < str.length(); i++)
      try {
        int n = Integer.valueOf(str.substring(i, i + 1));
        sb.append(n);
      } catch (NumberFormatException e) {
        if (str.charAt(i) == '.' && dotCount == 0) {
          sb.append('.');
          dotCount++;
        } else if (str.charAt(i) == '.' && dotCount > 0) return 0;
      }

    if (sb.length() == 0) return 0;

    if (sb.charAt(0) == '.') sb.insert(0, '0');

    if (sb.charAt(sb.length() - 1) == '.') sb.append(0);

    return Float.valueOf(sb.toString());
  }
 public static void reverse(int i, int j, StringBuilder input) {
   char temp;
   for (; i < j; i++, j--) {
     temp = input.charAt(i);
     input.setCharAt(i, input.charAt(j));
     input.setCharAt(j, temp);
   }
 }
  public static void main(String args[]) {
    try {
      // ReceiveMessageInterface rmiclient;
      RmiServer server = new RmiServer();
      // rmiclient=(ReceiveMessageInterface)(registry.lookup("rmiclient"));
      // rmiclient.generateKeys(publicKey);
      KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
      keyGen.initialize(1024);
      KeyPair keypair = keyGen.genKeyPair();
      publicKey = keypair.getPublic();
      privateKey = keypair.getPrivate();

      BufferedImage image = ImageIO.read(new File("/home/subba/Desktop/test/iris1.bmp"));

      // write it to byte array in-memory (jpg format)
      ByteArrayOutputStream b = new ByteArrayOutputStream();
      ImageIO.write(image, "bmp", b);

      // do whatever with the array...
      byte[] jpgByteArray = b.toByteArray();

      // convert it to a String with 0s and 1s
      StringBuilder sb = new StringBuilder();
      int i = 0;
      for (byte by : jpgByteArray) {
        i++;
        if (i > 366) break;
        sb.append(Integer.toBinaryString(by & 0xFF));
      }
      sb.append("0000000000000000000000000000000000000000000");
      System.out.println(sb.toString().length());
      System.out.println(sb.toString());
      int token = 170;
      StringBuilder sb1 = new StringBuilder();
      sb1.append(Integer.toBinaryString(token));
      for (int j = 0; j < 102; j++) {
        sb1.append("00000000000000000000");
      }
      System.out.println("Binary is " + sb1.length());
      for (i = 0; i < sb.length(); i++) {
        bioTemplate.append(sb.charAt(i) ^ sb1.charAt(i));
      }

      /*MessageDigest digest = MessageDigest.getInstance("SHA-256");
      byte[] hashvalue=serviceProviderKey.getBytes();
      digest.update(hashvalue);
      Phashdigest=digest.digest();
      */

      securePassword = getSecurePassword(serviceProviderKey, "200");
      System.out.println(securePassword);
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
  }
 public Token scanBlockComment() {
   int start = pos;
   while ((pos + 1) < input.length()
       && (input.charAt(pos) != '*' || input.charAt(pos + 1) != '/')) {
     pos++;
   }
   pos++;
   pos++;
   return new Token(Token.Kind.BlockComment, input.substring(start, pos), start);
 }
  private void setDateTimeFormat(final String format) {
    final StringBuilder fb = new StringBuilder(format);
    for (int i = 0; i < fb.length(); i++) {
      if (DATE_TIME_FORMAT_MAP.containsKey(fb.charAt(i))) {
        fb.setCharAt(i, DATE_TIME_FORMAT_MAP.get(fb.charAt(i)));
      }
    }

    this.dateTimeFormat = DateTimeFormat.getFormat(fb.toString());
  }
 private void removeTrailingWhitespace(StringBuilder sb) {
   while (sb.length() > 0 && (sb.charAt(0) == ' ' || sb.charAt(0) == '\t')) // trim left
   sb.deleteCharAt(0);
   while (sb.length() > 0
       && (sb.charAt(sb.length() - 1) == ' ' || sb.charAt(sb.length() - 1) == '\t')) // trim right
   sb.deleteCharAt(sb.length() - 1);
   if (sb.length() != 0
       && sb.lastIndexOf(K5Symbol.DEFAULT_STATEMENT_SEPARATOR_STRING)
           == (sb.length() - K5Symbol.DEFAULT_STATEMENT_SEPARATOR_STRING.length()))
     sb.delete(sb.length() - K5Symbol.DEFAULT_STATEMENT_SEPARATOR_STRING.length(), sb.length());
 }
Exemple #28
0
 static void extract(StringBuilder target, char[] buffer, int origin, int length) {
   int end = origin + length;
   int start = findStart(buffer, origin, end);
   if (start >= end) // all whitespace
   {
     return;
   }
   // trim whitespace from the end
   for (int cp;
       end > start
           && (isLowSurrogate(buffer[end - 1])
               ? isWhitespace(cp = codePointAt(buffer, end - 2))
               : isWhitespace(cp = buffer[end - 1])); ) {
     end -= charCount(cp);
   }
   // If the first line starts at the origin, and is not preceded by whitespace,
   // it is excluded from the indentation detection by emitting it immediately.
   if (start == origin && !isWhitespace(codePointAt(buffer, start))) {
     if (target.length() > 0 && target.charAt(target.length() - 1) != '\n') {
       target.append('\n');
     }
     start = emitLine(target, buffer, start, end, 0);
     if (start >= end) {
       return;
     }
     target.append('\n');
     // emit blank lines
     for (int pos = start, cp; pos < end; pos += charCount(cp)) {
       cp = codePointAt(buffer, pos);
       if (cp == '\n') {
         target.append('\n');
         start = pos + 1;
       } else if (!isWhitespace(cp)) {
         break;
       }
     }
   }
   // find the shortest indentation (sequence of same WS char as the first) of any of the
   // (remaining) lines
   int indentation = shortestIndentation(buffer, start, end);
   // emit all lines
   if (target.length() > 0 && target.charAt(target.length() - 1) != '\n') {
     target.append('\n');
   }
   for (int ln = target.length() > 0 ? 2 : 0; start < end; ln++) {
     if (ln == 1) {
       target.append('\n');
     }
     start = emitLine(target, buffer, start, end, indentation);
     if (ln > 0) {
       target.append('\n');
     }
   }
 }
 protected static String addUnderscores(String name) {
   StringBuilder buf = new StringBuilder(name.replace('.', '_'));
   for (int i = 1; i < buf.length() - 1; i++) {
     if (Character.isLowerCase(buf.charAt(i - 1))
         && Character.isUpperCase(buf.charAt(i))
         && Character.isLowerCase(buf.charAt(i + 1))) {
       buf.insert(i++, '_');
     }
   }
   return buf.toString().toLowerCase(Locale.ROOT);
 }
Exemple #30
0
 /**
  * Lame - StringBuilder.equals is retarded.
  *
  * @param b1
  * @param b2
  * @return whether or not the two builders consist the same sequence of characters
  */
 public static boolean equals(StringBuilder b1, StringBuilder b2) {
   if (b1.length() != b2.length()) {
     return false;
   }
   for (int i = 0; i < b1.length(); i++) {
     if (b1.charAt(i) != b2.charAt(i)) {
       return false;
     }
   }
   return true;
 }