List of usage examples for java.util Enumeration nextElement
E nextElement();
From source file:com.isomorphic.maven.util.ArchiveUtils.java
/** * Unzips the <code>source</code> file to the <code>target</code> directory. * /* w ww.ja v a 2 s . c o m*/ * @param source The file to be unzipped * @param target The directory to which the source file should be unzipped * @throws IOException */ public static void unzip(File source, File target) throws IOException { ZipFile zip = new ZipFile(source); Enumeration<? extends ZipEntry> entries = zip.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); File file = new File(target, entry.getName()); FileUtils.copyInputStreamToFile(zip.getInputStream(entry), file); } }
From source file:io.apiman.test.common.mock.EchoServlet.java
/** * Create an echo response from the inbound information in the http server * request.//from w w w. j a v a2 s. c o m * @param request the request * @param withBody if request is with body * @return a new echo response */ public static EchoResponse response(HttpServletRequest request, boolean withBody) { EchoResponse response = new EchoResponse(); response.setMethod(request.getMethod()); if (request.getQueryString() != null) { String[] normalisedQueryString = request.getQueryString().split("&"); Arrays.sort(normalisedQueryString); response.setResource( request.getRequestURI() + "?" + SimpleStringUtils.join("&", normalisedQueryString)); } else { response.setResource(request.getRequestURI()); } response.setUri(request.getRequestURI()); Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String name = headerNames.nextElement(); String value = request.getHeader(name); response.getHeaders().put(name, value); } if (withBody) { long totalBytes = 0; InputStream is = null; try { is = request.getInputStream(); MessageDigest sha1 = MessageDigest.getInstance("SHA1"); byte[] data = new byte[1024]; int read; while ((read = is.read(data)) != -1) { sha1.update(data, 0, read); totalBytes += read; } ; byte[] hashBytes = sha1.digest(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < hashBytes.length; i++) { sb.append(Integer.toString((hashBytes[i] & 0xff) + 0x100, 16).substring(1)); } String fileHash = sb.toString(); response.setBodyLength(totalBytes); response.setBodySha1(fileHash); } catch (Exception e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(is); } } return response; }
From source file:com.feedzai.fos.server.remote.impl.RemoteInterfacesTest.java
/** * Scans all classes accessible from the context class loader which belong to the given package and subpackages. * * @param packageName The base package//w ww .j av a2 s.c o m * @return The classes * @throws ClassNotFoundException * @throws java.io.IOException */ private static Class[] getClasses(String packageName) throws ClassNotFoundException, IOException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); assert classLoader != null; String path = packageName.replace('.', '/'); Enumeration<URL> resources = classLoader.getResources(path); List<File> dirs = new ArrayList<>(); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); dirs.add(new File(resource.getFile())); } ArrayList<Class> classes = new ArrayList<>(); for (File directory : dirs) { classes.addAll(findClasses(directory, packageName)); } return classes.toArray(new Class[classes.size()]); }
From source file:io.card.development.recording.ManifestEntry.java
public static ManifestEntry[] getManifest(Hashtable<String, byte[]> recordingData) { Enumeration<String> keys = recordingData.keys(); String manifestKey = null;/*w ww . j a v a2 s .c o m*/ while (keys.hasMoreElements()) { String key = keys.nextElement(); if (key.endsWith("manifest.json")) { manifestKey = key; break; } } if (manifestKey == null) { return null; } String manifestDirName = manifestKey.substring(0, manifestKey.lastIndexOf("/")); String manifestString = new String(recordingData.get(manifestKey)); JSONArray manifestData; try { manifestData = new JSONArray(manifestString); } catch (JSONException e1) { Log.e(TAG, "Exception parsing JSON array: " + e1.getMessage()); return null; } ManifestEntry[] manifest = new ManifestEntry[manifestData.length()]; for (int i = 0; i < manifestData.length(); i++) { try { JSONObject jo = manifestData.getJSONObject(i); manifest[i] = new ManifestEntry(manifestDirName, jo, recordingData); } catch (JSONException e) { Log.e(TAG, "Couldn't parse JSON: " + e.getMessage()); } } return manifest; }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T extends Collection<K>, K> T toCollection(Class<?> clazz, Enumeration<K> enumeration) { if (clazz == null) { throw new RuntimeException("Class cannot be null."); }//from w w w . ja v a 2s . c o m try { T collection = (T) clazz.newInstance(); if (enumeration != null) { while (enumeration.hasMoreElements()) { collection.add(enumeration.nextElement()); } } return collection; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.glaf.core.config.MessageProperties.java
public static void reload() { if (!loading.get()) { InputStream inputStream = null; try {//from w w w . j a va 2s. c om loading.set(true); String config = SystemProperties.getConfigRootPath() + "/conf/props/messages"; File directory = new File(config); if (directory.exists() && directory.isDirectory()) { File[] filelist = directory.listFiles(); if (filelist != null) { for (int i = 0, len = filelist.length; i < len; i++) { String filename = config + "/" + filelist[i]; File file = new File(filename); if (file.isFile() && file.getName().endsWith(".properties")) { inputStream = new FileInputStream(file); Properties p = PropertiesUtils.loadProperties(inputStream); if (p != null) { Enumeration<?> e = p.keys(); while (e.hasMoreElements()) { String key = (String) e.nextElement(); String value = p.getProperty(key); properties.setProperty(key, value); properties.setProperty(key.toLowerCase(), value); properties.setProperty(key.toUpperCase(), value); } } IOUtils.closeStream(inputStream); } } } } ISystemPropertyService systemPropertyService = ContextFactory.getBean("systemPropertyService"); List<SystemProperty> list = systemPropertyService.getAllSystemProperties(); if (list != null && !list.isEmpty()) { for (SystemProperty p : list) { properties.put(p.getName(), p); } } } catch (Exception ex) { throw new RuntimeException(ex); } finally { loading.set(false); IOUtils.closeStream(inputStream); } } }
From source file:com.littcore.io.util.ZipUtils.java
public static void unzip(File srcZipFile, File targetFilePath, boolean isDelSrcFile) throws IOException { if (!targetFilePath.exists()) //? targetFilePath.mkdirs();/*w ww. j a v a 2s.c o m*/ ZipFile zipFile = new ZipFile(srcZipFile); Enumeration fileList = zipFile.getEntries(); ZipEntry zipEntry = null; while (fileList.hasMoreElements()) { zipEntry = (ZipEntry) fileList.nextElement(); if (zipEntry.isDirectory()) //? { File subFile = new File(targetFilePath, zipEntry.getName()); if (!subFile.exists()) //?? subFile.mkdir(); continue; } else //? { File targetFile = new File(targetFilePath, zipEntry.getName()); if (logger.isDebugEnabled()) { logger.debug("" + targetFile.getName()); } if (!targetFile.exists()) { File parentPath = new File(targetFile.getParent()); if (!parentPath.exists()) //???????? parentPath.mkdirs(); //targetFile.createNewFile(); } InputStream in = zipFile.getInputStream(zipEntry); FileOutputStream out = new FileOutputStream(targetFile); int len; byte[] buf = new byte[BUFFERED_SIZE]; while ((len = in.read(buf)) != -1) { out.write(buf, 0, len); } out.close(); in.close(); } } zipFile.close(); if (isDelSrcFile) { boolean isDeleted = srcZipFile.delete(); if (isDeleted) { if (logger.isDebugEnabled()) { logger.debug(""); } } } }
From source file:com.tesora.dve.common.PELogUtils.java
private static Attributes readManifestFile() throws PEException { try {// w w w . j a va 2 s . co m Enumeration<URL> resources = PELogUtils.class.getClassLoader().getResources(MANIFEST_FILE_NAME); Attributes attrs = new Attributes(); while (resources.hasMoreElements()) { URL url = resources.nextElement(); if (url.toString().contains(CORE_PROJECT_NAME)) { Manifest manifest = new Manifest(url.openStream()); attrs = manifest.getMainAttributes(); break; } } return attrs; } catch (Exception e) { throw new PEException("Error retrieving build manifest", e); } }
From source file:Main.java
public static String GetWantedIPAdress_JAVA() { String wantedIP = ""; Enumeration<NetworkInterface> e = null; try {/*www. j a v a 2 s . c o m*/ e = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e1) { e1.printStackTrace(); } if (e != null) { while (e.hasMoreElements()) { NetworkInterface n = (NetworkInterface) e.nextElement(); Enumeration<InetAddress> ee = n.getInetAddresses(); while (ee.hasMoreElements()) { InetAddress i = (InetAddress) ee.nextElement(); if (i.getHostAddress().toString().contains("192.168.") || i.getHostAddress().toString().startsWith("10.")) { wantedIP = i.getHostAddress(); break; } } } } return wantedIP; }
From source file:net.ontopia.topicmaps.webed.impl.utils.ReqParamUtils.java
/** * INTERNAL: Builds the Parameters object from an HttpServletRequest * object.// w w w . j av a 2 s. c o m * @since 2.0 */ public static Parameters decodeParameters(HttpServletRequest request, String charenc) throws ServletException, IOException { String ctype = request.getHeader("content-type"); log.debug("Content-type: " + ctype); Parameters params = new Parameters(); if (ctype != null && ctype.startsWith("multipart/form-data")) { // special file upload request, so use FileUpload to decode log.debug("Decoding with FileUpload; charenc=" + charenc); try { FileUpload upload = new FileUpload(new DefaultFileItemFactory()); Iterator iterator = upload.parseRequest(request).iterator(); while (iterator.hasNext()) { FileItem item = (FileItem) iterator.next(); log.debug("Reading: " + item); if (item.isFormField()) { if (charenc != null) params.addParameter(item.getFieldName(), item.getString(charenc)); else params.addParameter(item.getFieldName(), item.getString()); } else params.addParameter(item.getFieldName(), new FileParameter(item)); } } catch (FileUploadException e) { throw new ServletException(e); } } else { // ordinary web request, so retrieve info and stuff into Parameters object log.debug("Normal parameter decode, charenc=" + charenc); if (charenc != null) request.setCharacterEncoding(charenc); Enumeration enumeration = request.getParameterNames(); while (enumeration.hasMoreElements()) { String param = (String) enumeration.nextElement(); params.addParameter(param, request.getParameterValues(param)); } } return params; }