Example usage for java.util Scanner useDelimiter

List of usage examples for java.util Scanner useDelimiter

Introduction

In this page you can find the example usage for java.util Scanner useDelimiter.

Prototype

public Scanner useDelimiter(String pattern) 

Source Link

Document

Sets this scanner's delimiting pattern to a pattern constructed from the specified String .

Usage

From source file:nz.org.winters.appspot.acrareporter.server.MappingUploadHandler.java

public static String convertStreamToString(java.io.InputStream is) {
    java.util.Scanner s = new java.util.Scanner(is);
    s.useDelimiter("\\A");
    try {/*from w  w w  . java 2s  . c o  m*/
        return s.hasNext() ? s.next() : "";
    } finally {
        s.close();
    }
}

From source file:com.networknt.light.server.handler.loader.MenuLoader.java

private static void loadMenuFile(File file) {
    Scanner scan = null;
    try {/* w ww  .j a va  2  s.  com*/
        scan = new Scanner(file, Loader.encoding);
        // the content is only the data portion. convert to map
        String content = scan.useDelimiter("\\Z").next();
        HttpPost httpPost = new HttpPost("http://injector:8080/api/rs");
        StringEntity input = new StringEntity(content);
        input.setContentType("application/json");
        httpPost.setEntity(input);
        CloseableHttpResponse response = httpclient.execute(httpPost);

        try {
            System.out.println(response.getStatusLine());
            HttpEntity entity = response.getEntity();
            BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
            String json = "";
            String line = "";
            while ((line = rd.readLine()) != null) {
                json = json + line;
            }
            System.out.println("json = " + json);
            EntityUtils.consume(entity);
        } finally {
            response.close();
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        if (scan != null)
            scan.close();
    }
}

From source file:no.digipost.api.useragreements.client.response.ResponseUtils.java

public static Stream<String> streamDelimitedStringsOf(InputStream inputStream, Charset charset,
        Pattern delimiter) {/*from   www.  j a  v a 2s . c  om*/
    Scanner contentScanner = new Scanner(inputStream, charset.name());
    return stream(spliteratorUnknownSize(contentScanner.useDelimiter(delimiter), IMMUTABLE), false).onClose(
            () -> close(contentScanner, inputStream).map(RuntimeIOException::from).ifPresent(exception -> {
                throw exception;
            }));
}

From source file:org.t3.metamediamanager.OmdbProvider.java

/**
 * Converts an InputStream object into a String object.
 * @param is/*from w  w w  .j a va2s  .  co m*/
 *       The InputStream you want to convert.
 * @return
 *       A String Object.
 */
private static String convertStreamToString(InputStream is) {
    Scanner s = new Scanner(is);
    Scanner s2 = s.useDelimiter("\\A");
    String res = s.hasNext() ? s.next() : "";
    s2.close();
    s.close();
    return res;
}

From source file:org.eclipse.om2m.comm.http.RestHttpServlet.java

static String convertStreamToString(InputStream is) {
    Scanner scanner = new Scanner(is);
    scanner.useDelimiter("\\A");
    return scanner.hasNext() ? scanner.next() : "";
}

From source file:com.sina.cloudstorage.util.URLEncodedUtils.java

/**
 * Adds all parameters within the Scanner to the list of
 * <code>parameters</code>, as encoded by <code>encoding</code>. For
 * example, a scanner containing the string <code>a=1&b=2&c=3</code> would
 * add the {@link NameValuePair NameValuePairs} a=1, b=2, and c=3 to the
 * list of parameters./*from  w w  w .  j a  v  a  2 s . c  om*/
 *
 * @param parameters
 *            List to add parameters to.
 * @param scanner
 *            Input that contains the parameters to parse.
 * @param charset
 *            Encoding to use when decoding the parameters.
 */
public static void parse(final List<NameValuePair> parameters, final Scanner scanner, final String charset) {
    scanner.useDelimiter(PARAMETER_SEPARATOR);
    while (scanner.hasNext()) {
        String name = null;
        String value = null;
        String token = scanner.next();
        int i = token.indexOf(NAME_VALUE_SEPARATOR);
        if (i != -1) {
            name = decode(token.substring(0, i).trim(), charset);
            value = decode(token.substring(i + 1).trim(), charset);
        } else {
            name = decode(token.trim(), charset);
        }
        parameters.add(new BasicNameValuePair(name, value));
    }
}

From source file:com.networknt.light.server.handler.loader.PageLoader.java

private static void loadPageFile(String host, File file) {
    Scanner scan = null;
    try {//from   w  w  w.j  av  a  2  s . c  o m
        scan = new Scanner(file, Loader.encoding);
        // the content is only the data portion. convert to map
        String content = scan.useDelimiter("\\Z").next();
        String pageId = file.getName();
        pageId = pageId.substring(0, pageId.lastIndexOf('.'));
        if (content != null && !content.equals(pageMap.get(pageId))) {
            System.out.println(content);
            System.out.println(pageMap.get(pageId));
            Map<String, Object> inputMap = new HashMap<String, Object>();
            inputMap.put("category", "page");
            inputMap.put("name", "impPage");
            inputMap.put("readOnly", false);
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("pageId", pageId);
            data.put("content", content);
            inputMap.put("data", data);
            HttpPost httpPost = new HttpPost(host + "/api/rs");
            httpPost.addHeader("Authorization", "Bearer " + jwt);
            StringEntity input = new StringEntity(
                    ServiceLocator.getInstance().getMapper().writeValueAsString(inputMap));
            input.setContentType("application/json");
            httpPost.setEntity(input);
            CloseableHttpResponse response = httpclient.execute(httpPost);

            try {
                System.out.println("Page: " + file.getAbsolutePath() + " is loaded with status "
                        + response.getStatusLine());
                HttpEntity entity = response.getEntity();
                BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
                String json = "";
                String line = "";
                while ((line = rd.readLine()) != null) {
                    json = json + line;
                }
                //System.out.println("json = " + json);
                EntityUtils.consume(entity);
            } finally {
                response.close();
            }
        } else {
            //System.out.println("Skip file " + pageId);
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        if (scan != null)
            scan.close();
    }
}

From source file:org.fragzone.unrealmmo.bukkit.entities.npcs.NPCProfile.java

private static void addProperties(GameProfile profile, UUID id) {
    String uuid = id.toString().replaceAll("-", "");

    try {/*from w w w .j  ava  2 s.c  o m*/
        // Get the name from SwordPVP
        URL url = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid);
        URLConnection uc = url.openConnection();
        uc.setUseCaches(false);
        uc.setDefaultUseCaches(false);
        uc.addRequestProperty("User-Agent", "Mozilla/5.0");
        uc.addRequestProperty("Cache-Control", "no-cache, no-store, must-revalidate");
        uc.addRequestProperty("Pragma", "no-cache");

        // Parse it
        Scanner scanner = new Scanner(uc.getInputStream(), "UTF-8");
        String json = scanner.useDelimiter("\\A").next();
        scanner.close();
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(json);
        JSONArray properties = (JSONArray) ((JSONObject) obj).get("properties");

        for (int i = 0; i < properties.size(); i++) {
            try {
                JSONObject property = (JSONObject) properties.get(i);
                String name = (String) property.get("name");
                String value = (String) property.get("value");
                String signature = property.containsKey("signature") ? (String) property.get("signature")
                        : null;
                if (signature != null) {
                    profile.getProperties().put(name, new Property(name, value, signature));
                } else {
                    profile.getProperties().put(name, new Property(value, name));
                }
            } catch (Exception e) {
                Bukkit.getLogger().log(Level.WARNING, "Failed to apply auth property", e);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.springframework.yarn.test.support.ContainerLogUtils.java

/**
 * Reads a file content and return it as String. Returns <code>NULL</code>
 * if file doesn't exist and empty String if file exists but is empty.
 * @param file the file/* w  w w . j  a va  2s.  c  o  m*/
 * @return the file content
 * @throws Exception the exception if error occurred
 */
public static String getFileContent(File file) throws Exception {
    Scanner scanner = null;
    String content = null;
    Exception reThrow = null;
    if (file != null && file.length() > 0) {
        try {
            scanner = new Scanner(file);
            content = scanner.useDelimiter("\\A").next();
        } catch (Exception e) {
            reThrow = e;
        } finally {
            if (scanner != null) {
                try {
                    scanner.close();
                } catch (Exception e) {
                }
            }
        }
    } else if (file != null && file.length() == 0) {
        content = "";
    }
    if (reThrow != null) {
        throw reThrow;
    } else {
        return content;
    }
}

From source file:IO.java

public static double[] readDoubleVec(File file, int nDim) {
    Scanner sc;
    double[] wavelength = new double[nDim];
    try {/* w w w. j  a v a2  s .  c  o m*/
        sc = new Scanner(file);
        sc.useDelimiter(",|\\n");
        for (int i = 0; i < nDim; i++) {
            wavelength[i] = Double.parseDouble(sc.next());
            //System.out.print(wavelength[i]+"\t");
        }
    } catch (FileNotFoundException e) {
        System.out.println("Wavelength file not found\n" + e);
        System.exit(0);
    }
    return wavelength;
}