예제 #1
1
  /**
   * Joins array elements to string.
   *
   * @param arr Array.
   * @return String.
   */
  @Nullable
  public static String compactArray(Object[] arr) {
    if (arr == null || arr.length == 0) return null;

    String sep = ", ";

    StringBuilder sb = new StringBuilder();

    for (Object s : arr) sb.append(s).append(sep);

    if (sb.length() > 0) sb.setLength(sb.length() - sep.length());

    return U.compact(sb.toString());
  }
예제 #2
0
  public static void main(String[] args) throws MessagingException, IOException {
    Properties props = new Properties();
    try (InputStream in = Files.newInputStream(Paths.get("mail", "mail.properties"))) {
      props.load(in);
    }
    List<String> lines = Files.readAllLines(Paths.get(args[0]), Charset.forName("UTF-8"));

    String from = lines.get(0);
    String to = lines.get(1);
    String subject = lines.get(2);

    StringBuilder builder = new StringBuilder();
    for (int i = 3; i < lines.size(); i++) {
      builder.append(lines.get(i));
      builder.append("\n");
    }

    Console console = System.console();
    String password = new String(console.readPassword("Password: "));

    Session mailSession = Session.getDefaultInstance(props);
    // mailSession.setDebug(true);
    MimeMessage message = new MimeMessage(mailSession);
    message.setFrom(new InternetAddress(from));
    message.addRecipient(RecipientType.TO, new InternetAddress(to));
    message.setSubject(subject);
    message.setText(builder.toString());
    Transport tr = mailSession.getTransport();
    try {
      tr.connect(null, password);
      tr.sendMessage(message, message.getAllRecipients());
    } finally {
      tr.close();
    }
  }
예제 #3
0
  /**
   * Concatenates some {@link NlsString} objects. The result has the charset and collation of the
   * first element. The other elements must have matching (or null) charset and collation.
   * Concatenates all at once, not pairwise, to avoid string copies.
   *
   * @param args array of {@link NlsString} to be concatenated
   */
  public static NlsString concat(List<NlsString> args) {
    if (args.size() < 2) {
      return args.get(0);
    }
    String charSetName = args.get(0).charsetName;
    SqlCollation collation = args.get(0).collation;
    int length = args.get(0).value.length();

    // sum string lengths and validate
    for (int i = 1; i < args.size(); i++) {
      final NlsString arg = args.get(i);
      length += arg.value.length();
      if (!((arg.charsetName == null) || arg.charsetName.equals(charSetName))) {
        throw new IllegalArgumentException("mismatched charsets");
      }
      if (!((arg.collation == null) || arg.collation.equals(collation))) {
        throw new IllegalArgumentException("mismatched collations");
      }
    }

    StringBuilder sb = new StringBuilder(length);
    for (NlsString arg : args) {
      sb.append(arg.value);
    }
    return new NlsString(sb.toString(), charSetName, collation);
  }
예제 #4
0
 /**
  * Computes the digest field. This should be called in every non-abstract subclass constructor
  * once the type is fully defined.
  */
 protected void computeDigest() {
   StringBuilder sb = new StringBuilder();
   generateTypeString(sb, true);
   if (!isNullable()) {
     sb.append(" NOT NULL");
   }
   digest = sb.toString();
 }
예제 #5
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);
     }
 }
예제 #6
0
  /**
   * Returns compact class host.
   *
   * @param obj Object to compact.
   * @return String.
   */
  @Nullable
  public static Object compactObject(Object obj) {
    if (obj == null) return null;

    if (obj instanceof Enum) return obj.toString();

    if (obj instanceof String || obj instanceof Boolean || obj instanceof Number) return obj;

    if (obj instanceof Collection) {
      Collection col = (Collection) obj;

      Object[] res = new Object[col.size()];

      int i = 0;

      for (Object elm : col) res[i++] = compactObject(elm);

      return res;
    }

    if (obj.getClass().isArray()) {
      Class<?> arrType = obj.getClass().getComponentType();

      if (arrType.isPrimitive()) {
        if (obj instanceof boolean[]) return Arrays.toString((boolean[]) obj);
        if (obj instanceof byte[]) return Arrays.toString((byte[]) obj);
        if (obj instanceof short[]) return Arrays.toString((short[]) obj);
        if (obj instanceof int[]) return Arrays.toString((int[]) obj);
        if (obj instanceof long[]) return Arrays.toString((long[]) obj);
        if (obj instanceof float[]) return Arrays.toString((float[]) obj);
        if (obj instanceof double[]) return Arrays.toString((double[]) obj);
      }

      Object[] arr = (Object[]) obj;

      int iMax = arr.length - 1;

      StringBuilder sb = new StringBuilder("[");

      for (int i = 0; i <= iMax; i++) {
        sb.append(compactObject(arr[i]));

        if (i != iMax) sb.append(", ");
      }

      sb.append("]");

      return sb.toString();
    }

    return U.compact(obj.getClass().getName());
  }
예제 #7
0
파일: Token.java 프로젝트: nikhi/basex
 /**
  * Converts the given string to camel case.
  *
  * @param string string to convert
  * @return resulting string
  */
 public static String camelCase(final String string) {
   final StringBuilder sb = new StringBuilder(string.length());
   boolean dash = false;
   for (int p = 0; p < string.length(); p++) {
     final char ch = string.charAt(p);
     if (dash) {
       sb.append(Character.toUpperCase(ch));
       dash = false;
     } else {
       dash = ch == '-';
       if (!dash) sb.append(ch);
     }
   }
   return sb.toString();
 }
예제 #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);
     }
 }
예제 #9
0
 /**
  * Returns an MD5 hash.
  *
  * @param pw String
  * @return String
  */
 private static String md5(final String pw) {
   final StringBuilder sb = new StringBuilder();
   try {
     final MessageDigest md = MessageDigest.getInstance("MD5");
     md.update(pw.getBytes());
     for (final byte b : md.digest()) {
       final String s = Integer.toHexString(b & 0xFF);
       if (s.length() == 1) sb.append('0');
       sb.append(s);
     }
   } catch (final NoSuchAlgorithmException ex) {
     // should not occur
     ex.printStackTrace();
   }
   return sb.toString();
 }
예제 #10
0
파일: Token.java 프로젝트: nikhi/basex
  /**
   * Returns a string of the specified UTF8 token.
   *
   * @param token token
   * @param start start position
   * @param length length
   * @return string
   */
  private static String utf8(final byte[] token, final int start, final int length) {
    // input is assumed to be correct UTF8. if input contains codepoints
    // larger than Character.MAX_CODE_POINT, results might be unexpected.

    final StringBuilder sb = new StringBuilder(length << 1);
    final int il = Math.min(start + length, token.length);
    for (int i = start; i < il; i += cl(token, i)) {
      final int cp = cp(token, i);
      if (cp < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
        sb.append((char) cp);
      } else {
        final int o = cp - Character.MIN_SUPPLEMENTARY_CODE_POINT;
        sb.append((char) ((o >>> 10) + Character.MIN_HIGH_SURROGATE));
        sb.append((char) ((o & 0x3ff) + Character.MIN_LOW_SURROGATE));
      }
    }
    return sb.toString();
  }
예제 #11
0
  /**
   * Returns the string quoted for SQL, for example <code>_ISO-8859-1'is it a
   * plane? no it''s superman!'</code>.
   *
   * @param prefix if true, prefix the character set name
   * @param suffix if true, suffix the collation clause
   * @return the quoted string
   */
  public String asSql(boolean prefix, boolean suffix) {
    StringBuilder ret = new StringBuilder();
    if (prefix && (null != charsetName)) {
      ret.append("_");
      ret.append(charsetName);
    }
    ret.append("'");
    ret.append(Util.replace(value, "'", "''"));
    ret.append("'");

    // NOTE jvs 3-Feb-2005:  see FRG-78 for why this should go away
    if (false) {
      if (suffix && (null != collation)) {
        ret.append(" ");
        ret.append(collation.toString());
      }
    }
    return ret.toString();
  }
예제 #12
0
 // implement RelDataType
 public String toString() {
   StringBuilder sb = new StringBuilder();
   generateTypeString(sb, false);
   return sb.toString();
 }
예제 #13
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());
 }