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:com.zb.app.common.file.FileUtils.java

public static void unJar(File jarFile, File toDir) throws IOException {
    JarFile jar = new JarFile(jarFile);
    try {/*from  www.j a  v  a2  s. c o  m*/
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = (JarEntry) entries.nextElement();
            if (!entry.isDirectory()) {
                InputStream in = jar.getInputStream(entry);
                try {
                    File file = new File(toDir, entry.getName());
                    if (!file.getParentFile().mkdirs()) {
                        if (!file.getParentFile().isDirectory()) {
                            throw new IOException("Mkdirs failed to create " + file.getParentFile().toString());
                        }
                    }
                    OutputStream out = new FileOutputStream(file);
                    try {
                        byte[] buffer = new byte[8192];
                        int i;
                        while ((i = in.read(buffer)) != -1) {
                            out.write(buffer, 0, i);
                        }
                    } finally {
                        out.close();
                    }
                } finally {
                    in.close();
                }
            }
        }
    } finally {
        jar.close();
    }
}

From source file:Main.java

/** 
 * Adjust all fonts for displaying to the given size.
 * <br>Use with care!// w  w  w.j  a  v a 2  s .  c om
 */
public static void setUIFontSize(final int size) {

    final java.util.Enumeration<Object> keys = UIManager.getLookAndFeelDefaults().keys();
    while (keys.hasMoreElements()) {
        final Object key = keys.nextElement();
        final Object value = UIManager.get(key);
        if (value instanceof Font) {
            final Font ifont = (Font) value;
            final Font ofont = ifont.deriveFont((float) size);
            UIManager.put(key, ofont);
        }
    }
}

From source file:Main.java

public static Iterable<String> iterateAsStrings(Enumeration<?> enumeration) {
    List<String> list = new ArrayList<String>();
    while (enumeration.hasMoreElements()) {
        list.add(valueOf(enumeration.nextElement()));
    }/*  w  w w  .ja v  a  2  s . com*/
    return list;
}

From source file:Main.java

/**
 * Marshal the elements from the given enumeration into an array of the given type. Enumeration elements must be assignable to the type
 * of the given array. The array returned will be a different instance than the array given.
 * /* ww  w .  ja v  a  2s .c om*/
 * @param enumeration
 *            the enumeration
 * @param array
 *            the array
 * @return the array representation of the enumeration
 * @param <A>
 *            the type of the array
 * @param <E>
 *            the type of th enumeration
 */
public static <A, E extends A> A[] toArray(Enumeration<E> enumeration, A[] array) {
    final ArrayList<A> elements = new ArrayList<A>();
    while (enumeration.hasMoreElements()) {
        elements.add(enumeration.nextElement());
    }
    return elements.toArray(array);
}

From source file:com.modeln.build.ctrl.charts.CMnBuildChart.java

/**
 * Validate the data submitted by the user and return any error codes.
 *
 * @param   req     HTTP request//ww  w .j  a v a 2 s . c  o  m
 * @param   res     HTTP response
 * 
 * @return  Error code if any errors were found.
 */
public static final JFreeChart getMetricChart(CMnDbBuildData build) {
    JFreeChart chart = null;

    DefaultPieDataset pieData = new DefaultPieDataset();
    if ((build.getMetrics() != null) && (build.getMetrics().size() > 0)) {
        Enumeration metrics = build.getMetrics().elements();
        while (metrics.hasMoreElements()) {
            CMnDbMetricData currentMetric = (CMnDbMetricData) metrics.nextElement();
            String name = currentMetric.getMetricType(currentMetric.getType());

            // Get elapsed time in "minutes"
            Long value = new Long(currentMetric.getElapsedTime() / (1000 * 60));

            pieData.setValue(name, value);
        }
    }

    // API: ChartFactory.createPieChart(title, data, legend, tooltips, urls)
    chart = ChartFactory.createPieChart("Build Metrics", pieData, true, true, false);

    // get a reference to the plot for further customization...
    PiePlot plot = (PiePlot) chart.getPlot();
    chartFormatter.formatMetricChart(plot, "min");

    return chart;
}

From source file:com.blackducksoftware.integration.hub.detect.util.DetectZipUtil.java

public static void unzip(File zip, File dest, Charset charset) throws IOException {
    Path destPath = dest.toPath();
    try (ZipFile zipFile = new ZipFile(zip, ZipFile.OPEN_READ, charset)) {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            Path entryPath = destPath.resolve(entry.getName());
            if (!entryPath.normalize().startsWith(dest.toPath()))
                throw new IOException("Zip entry contained path traversal");
            if (entry.isDirectory()) {
                Files.createDirectories(entryPath);
            } else {
                Files.createDirectories(entryPath.getParent());
                try (InputStream in = zipFile.getInputStream(entry)) {
                    try (OutputStream out = new FileOutputStream(entryPath.toFile())) {
                        IOUtils.copy(in, out);
                    }//from  w ww. j  a  v a  2s  . co m
                }
            }
        }
    }
}

From source file:Main.java

public static String getLocalIpAddress(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo != null && wifiInfo.getIpAddress() != 0) {
        return android.text.format.Formatter.formatIpAddress(wifiInfo.getIpAddress());
    } else {/*from  ww  w .jav a 2 s.  com*/
        try {
            Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
            while (en.hasMoreElements()) {
                NetworkInterface intf = en.nextElement();
                Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
                while (enumIpAddr.hasMoreElements()) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && inetAddress.getHostAddress().indexOf(":") == -1) {
                        String ipAddress = inetAddress.getHostAddress();
                        if (!TextUtils.isEmpty(ipAddress) && !ipAddress.contains(":")) {
                            return ipAddress;
                        }
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:org.tsm.concharto.auth.AuthHelper.java

@SuppressWarnings("unchecked")
public static void clearCredentials(HttpServletRequest request, HttpServletResponse response) {
    //clear out the session
    Enumeration attrNames = request.getSession().getAttributeNames();
    while (attrNames.hasMoreElements()) {
        String name = (String) attrNames.nextElement();
        WebUtils.setSessionAttribute(request, name, null);
        //free all session data
        request.getSession().invalidate();
    }// w  w  w  .ja  v a  2  s  .  co  m
    //clear the remember me cookies
    AuthHelper.setCookie(response, AuthHelper.COOKIE_REMEMBER_ME, 0, "");
    AuthHelper.setCookie(response, AuthHelper.COOKIE_REMEMBER_ME_USERNAME, 0, "");

}

From source file:org.nebula.service.core.HostAddress.java

public static String getLocalHost() throws Exception {

    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface current = interfaces.nextElement();

        if (!current.isUp() || current.isLoopback() || current.isVirtual()) {
            continue;
        }// w  ww .j a  v  a 2  s  .co m
        Enumeration<InetAddress> addresses = current.getInetAddresses();
        while (addresses.hasMoreElements()) {
            InetAddress current_addr = addresses.nextElement();
            if (current_addr.isLoopbackAddress()
                    || !InetAddressUtils.isIPv4Address(current_addr.getHostAddress())) {
                continue;
            }
            return current_addr.getHostName();
        }
    }

    throw new Exception("Failed to get local hostname");
}

From source file:com.ironiacorp.persistence.SqlUtil.java

/**
 * Check if the database driver is loaded.
 * //w w w .  j  a  v a 2s.  c  o m
 * @return True if the driver is loaded, False otherwise.
 */
public static boolean isDriverLoaded(String driver) {
    // Check if the driver has been already loaded
    Enumeration<Driver> drivers = DriverManager.getDrivers();
    while (drivers.hasMoreElements()) {
        Driver d = drivers.nextElement();
        if (d.getClass().getName().equals(driver)) {
            return true;
        }
    }
    return false;
}