Java UUID Create generateUUID()

Here you can find the source of generateUUID()

Description

generate UUID

License

Apache License

Declaration

public static String generateUUID() 

Method Source Code


//package com.java2s;
/*/*from   ww  w . j  a  v a 2 s  .  c  o m*/
 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import java.util.UUID;

public class Main {
    public static String generateUUID() {
        char[] uuid = new char[32];
        char[] chars = UUID.randomUUID().toString().toCharArray();
        for (int i = 0, j = 0; i < 32; j++)
            if (chars[j] != '-')
                uuid[i++] = chars[j];
        return new String(uuid);
    }

    public static String toString(Reader reader) throws IOException {
        if (reader instanceof BufferedReader) {
            return toString((BufferedReader) reader);
        } else {
            return toString(new BufferedReader(reader));
        }
    }

    public static String toString(InputStream is) throws IOException {
        return toString(new BufferedReader(new InputStreamReader(is, "UTF-8")));
    }

    public static String toString(BufferedReader reader) throws IOException {
        StringBuilder sb = new StringBuilder();
        try {
            String line;
            boolean previousLine = false;
            while ((line = reader.readLine()) != null) {
                if (previousLine) {
                    sb.append("\n");
                }
                sb.append(line);
                previousLine = true;
            }
        } finally {
            reader.close();
        }
        return sb.toString();
    }
}

Related

  1. generateUUID()
  2. generateUUID()
  3. generateUUID()
  4. generateUuid()
  5. generateUUID()
  6. generateUUID()
  7. generateUUID()
  8. generateUUID()
  9. generateUUID()