Example usage for java.util Enumeration hasMoreElements

List of usage examples for java.util Enumeration hasMoreElements

Introduction

In this page you can find the example usage for java.util Enumeration hasMoreElements.

Prototype

boolean hasMoreElements();

Source Link

Document

Tests if this enumeration contains more elements.

Usage

From source file:Main.java

@SuppressWarnings("unchecked")
public static <E> E[] toArray(Enumeration<E> enumeration, Class<E> type) {
    ArrayList<E> elements = new ArrayList<E>();
    while (enumeration.hasMoreElements()) {
        elements.add(enumeration.nextElement());
    }/*from  ww  w.  j av a  2  s  . c o m*/
    E[] array = (E[]) Array.newInstance(type, elements.size());
    elements.toArray(array);
    return array;
}

From source file:Main.java

public static void unzip(URL _url, URL _dest, boolean _remove_top) {
    int BUFFER_SZ = 2048;
    File file = new File(_url.getPath());

    String dest = _dest.getPath();
    new File(dest).mkdir();

    try {/*www  .  j a va2  s.co m*/
        ZipFile zip = new ZipFile(file);
        Enumeration<? extends ZipEntry> zipFileEntries = zip.entries();

        // Process each entry
        while (zipFileEntries.hasMoreElements()) {
            // grab a zip file entry
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();

            if (_remove_top)
                currentEntry = currentEntry.substring(currentEntry.indexOf('/'), currentEntry.length());

            File destFile = new File(dest, currentEntry);
            //destFile = new File(newPath, destFile.getName());
            File destinationParent = destFile.getParentFile();

            // create the parent directory structure if needed
            destinationParent.mkdirs();

            if (!entry.isDirectory()) {
                BufferedInputStream is;
                is = new BufferedInputStream(zip.getInputStream(entry));
                int currentByte;
                // establish buffer for writing file
                byte data[] = new byte[BUFFER_SZ];

                // write the current file to disk
                FileOutputStream fos = new FileOutputStream(destFile);
                BufferedOutputStream dst = new BufferedOutputStream(fos, BUFFER_SZ);

                // read and write until last byte is encountered
                while ((currentByte = is.read(data, 0, BUFFER_SZ)) != -1) {
                    dst.write(data, 0, currentByte);
                }
                dst.flush();
                dst.close();
                is.close();
            }
            /*
            if (currentEntry.endsWith(".zip"))
            {
            // found a zip file, try to open
            extractFolder(destFile.getAbsolutePath());
            }*/
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.scf.core.context.app.cfg.module.ModuleConfigHandler.java

/**
 * marges  props/*w  ww. j a  v a2  s . co m*/
 * 
 * @param marges
 * @param moduleName
 * @param props
 * @return
 */
@SuppressWarnings("rawtypes")
private static ConfigParams loadModuleConfig(Properties marges, String moduleName, Properties props) {
    if (marges != null) {
        Enumeration pks = marges.propertyNames();
        while (pks.hasMoreElements()) {
            String pk = (String) pks.nextElement();
            String _pk = "module_" + moduleName + ".";
            if (pk.startsWith(_pk)) {
                _pk = pk.substring(_pk.length());
                _logger.info("Module config override " + pk + " = " + marges.getProperty(pk) + "[default: "
                        + props.getProperty(_pk) + "]");
                props.setProperty(_pk, marges.getProperty(pk));
            }
        }
    }
    ConfigParams cp = new ConfigParams();
    cp.setParams(props);
    return cp;
}

From source file:proxy.ElementalHttpGet.java

static List<InetAddress> getAllIp(String networkInterface) {
    try {//from www  . ja v a  2 s.  c  om
        NetworkInterface interfaces = NetworkInterface.getByName(networkInterface);
        Enumeration<InetAddress> inetAddresses = interfaces.getInetAddresses();
        List<InetAddress> result = new ArrayList<InetAddress>();
        while (inetAddresses.hasMoreElements()) {
            InetAddress inetAddress = inetAddresses.nextElement();
            if (inetAddress instanceof Inet4Address) {
                result.add(inetAddress);
            }
        }
        return result;
    } catch (SocketException e) {
        throw new RuntimeException(e);
    }
}

From source file:cz.zeno.miner.Utils.java

public static String[] getAvailableSerialPorts() {
    java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
    ArrayList<String> serialPorts = new ArrayList<>();
    while (portEnum.hasMoreElements()) {
        CommPortIdentifier portIdentifier = portEnum.nextElement();

        if (CommPortIdentifier.PORT_SERIAL == portIdentifier.getPortType()) {
            serialPorts.add(portIdentifier.getName());
        }//from w ww.j  a v a  2s  .  co  m
    }
    return serialPorts.toArray(new String[0]);
}

From source file:com.mgmtp.jfunk.core.mail.MessageUtils.java

/**
 * Returns the specified message as text.
 * /* w  ww.jav a  2 s  . c o m*/
 * @param message
 *            the message
 * @param includeHeaders
 *            specifies whether message headers are to be included in the returned text
 * @return the message text
 */
public static String messageAsText(final Message message, final boolean includeHeaders) {
    try {
        StrBuilder sb = new StrBuilder(300);

        if (includeHeaders) {
            @SuppressWarnings("unchecked")
            Enumeration<Header> headers = message.getAllHeaders();
            while (headers.hasMoreElements()) {
                Header header = headers.nextElement();
                sb.append(header.getName()).append('=').appendln(header.getValue());
            }

            sb.appendln("");
        }

        Object content = message.getContent();
        if (content instanceof String) {
            String body = (String) content;
            sb.appendln(body);
            sb.appendln("");
        } else if (content instanceof Multipart) {
            parseMultipart(sb, (Multipart) content);
        }
        return sb.toString();
    } catch (MessagingException ex) {
        throw new MailException("Error getting mail content.", ex);
    } catch (IOException ex) {
        throw new MailException("Error getting mail content.", ex);
    }
}

From source file:Main.java

public static boolean isCACertificateInstalled(File fileCA, String type, char[] password)
        throws KeyStoreException {

    KeyStore keyStoreCA = null;/*from   ww  w  .  ja  v a 2  s . c om*/
    try {
        keyStoreCA = KeyStore.getInstance(type/*, "BC"*/);
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (fileCA.exists() && fileCA.canRead()) {
        try {
            FileInputStream fileCert = new FileInputStream(fileCA);
            keyStoreCA.load(fileCert, password);
            fileCert.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (java.security.cert.CertificateException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Enumeration ex = keyStoreCA.aliases();
        Date exportFilename = null;
        String caAliasValue = "";

        while (ex.hasMoreElements()) {
            String is = (String) ex.nextElement();
            Date lastStoredDate = keyStoreCA.getCreationDate(is);
            if (exportFilename == null || lastStoredDate.after(exportFilename)) {
                exportFilename = lastStoredDate;
                caAliasValue = is;
            }
        }

        try {
            return keyStoreCA.getKey(caAliasValue, password) != null;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnrecoverableKeyException e) {
            e.printStackTrace();
        }
    }
    return false;
}

From source file:io.fabric8.maven.core.extenvvar.ExternalEnvVarHandler.java

/**
 * Finds all of the environment json schemas and combines them together
 *//*from  w w w .  j  a  va  2s  . c o  m*/
private static JsonSchema loadEnvironmentSchemas(ClassLoader classLoader, String... folderPaths)
        throws IOException {
    JsonSchema answer = null;
    Enumeration<URL> resources = classLoader.getResources(ENVIRONMENT_SCHEMA_FILE);
    while (resources.hasMoreElements()) {
        URL url = resources.nextElement();
        JsonSchema schema = loadSchema(url);
        answer = combineSchemas(answer, schema);
    }
    for (String folderPath : folderPaths) {
        File file = new File(folderPath, ENVIRONMENT_SCHEMA_FILE);
        if (file.isFile()) {
            JsonSchema schema = loadSchema(file);
            answer = combineSchemas(answer, schema);
        }
    }
    return answer;
}

From source file:com.arrow.acs.ManifestUtils.java

public static Manifest readManifest(Class<?> clazz) {
    String method = "readManifest";
    String jarFile = null;/*from w w  w .jav  a2s  .  co m*/
    String path = clazz.getProtectionDomain().getCodeSource().getLocation().toString();
    for (String token : path.split("/")) {
        token = token.replace("!", "").toLowerCase().trim();
        if (token.endsWith(".jar")) {
            jarFile = token;
            break;
        }
    }
    LOGGER.logInfo(method, "className: %s, path: %s, jarFile: %s", clazz.getName(), path, jarFile);
    InputStream is = null;
    try {
        if (!StringUtils.isEmpty(jarFile)) {
            Enumeration<URL> enumeration = Thread.currentThread().getContextClassLoader()
                    .getResources(JarFile.MANIFEST_NAME);
            while (enumeration.hasMoreElements()) {
                URL url = enumeration.nextElement();
                for (String token : url.toString().split("/")) {
                    token = token.replace("!", "").toLowerCase();
                    if (token.equals(jarFile)) {
                        LOGGER.logInfo(method, "loading manifest from: %s", url.toString());
                        return new Manifest(is = url.openStream());
                    }
                }
            }
        } else {
            URL url = new URL(path + "/META-INF/MANIFEST.MF");
            LOGGER.logInfo(method, "loading manifest from: %s", url.toString());
            return new Manifest(is = url.openStream());
        }
    } catch (IOException e) {
    } finally {
        IOUtils.closeQuietly(is);
    }
    LOGGER.logError(method, "manifest file not found for: %s", clazz.getName());
    return null;
}

From source file:com.teradata.logging.TestFrameworkLoggingAppender.java

/**
 * Returns logs directory for configured TestFrameworkLoggingAppender.
 *//*  w ww  .j  a v a  2 s.  c om*/
public static Optional<String> getSelectedLogsDirectory() {
    Enumeration allAppenders = Logger.getRootLogger().getAllAppenders();
    while (allAppenders.hasMoreElements()) {
        Appender appender = (Appender) allAppenders.nextElement();
        if (appender instanceof TestFrameworkLoggingAppender) {
            return Optional.of(((TestFrameworkLoggingAppender) appender).logsDirectory);
        }
    }
    return Optional.empty();
}