Search in sources :

Example 36 with LineNumberReader

use of java.io.LineNumberReader in project platform_frameworks_base by android.

the class ConfigParser method parsePasspointConfig.

/**
     * Parse the Hotspot 2.0 Release 1 configuration data into a {@link PasspointConfiguration}
     * object.  The configuration data is a base64 encoded MIME multipart data.  Below is
     * the format of the decoded message:
     *
     * Content-Type: multipart/mixed; boundary={boundary}
     * Content-Transfer-Encoding: base64
     *
     * --{boundary}
     * Content-Type: application/x-passpoint-profile
     * Content-Transfer-Encoding: base64
     *
     * [base64 encoded Passpoint profile data]
     * --{boundary}
     * Content-Type: application/x-x509-ca-cert
     * Content-Transfer-Encoding: base64
     *
     * [base64 encoded X509 CA certificate data]
     * --{boundary}
     * Content-Type: application/x-pkcs12
     * Content-Transfer-Encoding: base64
     *
     * [base64 encoded PKCS#12 ASN.1 structure containing client certificate chain]
     * --{boundary}
     *
     * @param mimeType MIME type of the encoded data.
     * @param data A base64 encoded MIME multipart message containing the Passpoint profile
     *             (required), CA (Certificate Authority) certificate (optional), and client
     *             certificate chain (optional).
     * @return {@link PasspointConfiguration}
     */
public static PasspointConfiguration parsePasspointConfig(String mimeType, byte[] data) {
    // Verify MIME type.
    if (!TextUtils.equals(mimeType, TYPE_WIFI_CONFIG)) {
        Log.e(TAG, "Unexpected MIME type: " + mimeType);
        return null;
    }
    try {
        // Decode the data.
        byte[] decodedData = Base64.decode(new String(data, StandardCharsets.ISO_8859_1), Base64.DEFAULT);
        Map<String, byte[]> mimeParts = parseMimeMultipartMessage(new LineNumberReader(new InputStreamReader(new ByteArrayInputStream(decodedData), StandardCharsets.ISO_8859_1)));
        return createPasspointConfig(mimeParts);
    } catch (IOException | IllegalArgumentException e) {
        Log.e(TAG, "Failed to parse installation file: " + e.getMessage());
        return null;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) LineNumberReader(java.io.LineNumberReader)

Example 37 with LineNumberReader

use of java.io.LineNumberReader in project rsense by m2ym.

the class CodeAssist method readAndInjectCode.

private String readAndInjectCode(Reader _reader, Location loc, String injection, String prefixPattern, String defaultPrefix) throws IOException {
    LineNumberReader reader = new LineNumberReader(_reader);
    int line = reader.getLineNumber() + 1;
    int offset = -1;
    char[] buf = new char[4096];
    int read;
    int len = 0;
    StringBuilder buffer = new StringBuilder();
    while ((read = reader.read(buf)) != -1) {
        int index = 0;
        if (loc != null) {
            if (offset == -1) {
                offset = loc.findOffset(len, line, buf, read);
            }
            for (int i = 0; i < read; i++) {
                if (Character.isHighSurrogate(buf[i]) || buf[i] == '\r') {
                } else {
                    len++;
                    if (len == offset) {
                        index = i + 1;
                        int pstart = Math.max(0, index - 128);
                        String pbuf = new String(buf, pstart, index - pstart);
                        Matcher matcher = Pattern.compile(".*" + prefixPattern, Pattern.DOTALL).matcher(pbuf);
                        boolean match = matcher.matches();
                        if (match) {
                            if (matcher.groupCount() > 0) {
                                int end = index - (pbuf.length() - matcher.start(1));
                                buffer.append(buf, 0, end);
                                buffer.append(injection);
                                buffer.append(buf, end, index - end);
                            } else {
                                buffer.append(buf, 0, index);
                                buffer.append(injection);
                            }
                        } else {
                            buffer.append(buf, 0, index);
                            if (defaultPrefix != null)
                                buffer.append(defaultPrefix);
                            buffer.append(injection);
                        }
                        index += loc.getSkip();
                        break;
                    }
                }
            }
        }
        buffer.append(buf, index, read - index);
    }
    return buffer.toString();
}
Also used : Matcher(java.util.regex.Matcher) LineNumberReader(java.io.LineNumberReader)

Example 38 with LineNumberReader

use of java.io.LineNumberReader in project camel by apache.

the class FileHelper method loadFile.

/**
     * Loads the file
     */
public static List<String> loadFile(InputStream fis) throws Exception {
    List<String> lines = new ArrayList<>();
    LineNumberReader reader = new LineNumberReader(new InputStreamReader(fis));
    String line;
    do {
        line = reader.readLine();
        if (line != null) {
            lines.add(line);
        }
    } while (line != null);
    reader.close();
    return lines;
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) LineNumberReader(java.io.LineNumberReader)

Example 39 with LineNumberReader

use of java.io.LineNumberReader in project camel by apache.

the class FileHelper method loadText.

/**
     * Loads the entire stream into memory as a String and returns it.
     * <p/>
     * <b>Notice:</b> This implementation appends a <tt>\n</tt> as line
     * terminator at the of the text.
     * <p/>
     * Warning, don't use for crazy big streams :)
     */
public static String loadText(InputStream in) throws IOException {
    StringBuilder builder = new StringBuilder();
    InputStreamReader isr = new InputStreamReader(in);
    try {
        BufferedReader reader = new LineNumberReader(isr);
        while (true) {
            String line = reader.readLine();
            if (line != null) {
                builder.append(line);
                builder.append("\n");
            } else {
                break;
            }
        }
        return builder.toString();
    } finally {
        isr.close();
        in.close();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) LineNumberReader(java.io.LineNumberReader)

Example 40 with LineNumberReader

use of java.io.LineNumberReader in project camel by apache.

the class FileHelper method loadFile.

/**
     * Loads the file
     */
public static List<String> loadFile(File file) throws Exception {
    List<String> lines = new ArrayList<>();
    LineNumberReader reader = new LineNumberReader(new FileReader(file));
    String line;
    do {
        line = reader.readLine();
        if (line != null) {
            lines.add(line);
        }
    } while (line != null);
    reader.close();
    return lines;
}
Also used : ArrayList(java.util.ArrayList) FileReader(java.io.FileReader) LineNumberReader(java.io.LineNumberReader)

Aggregations

LineNumberReader (java.io.LineNumberReader)401 IOException (java.io.IOException)203 InputStreamReader (java.io.InputStreamReader)167 FileReader (java.io.FileReader)102 File (java.io.File)79 InputStream (java.io.InputStream)64 StringReader (java.io.StringReader)64 ArrayList (java.util.ArrayList)54 FileInputStream (java.io.FileInputStream)33 BufferedReader (java.io.BufferedReader)27 PrintWriter (java.io.PrintWriter)25 HashMap (java.util.HashMap)22 FileNotFoundException (java.io.FileNotFoundException)20 BufferedWriter (java.io.BufferedWriter)16 FileWriter (java.io.FileWriter)16 Pattern (java.util.regex.Pattern)16 Test (org.junit.jupiter.api.Test)16 ByteArrayInputStream (java.io.ByteArrayInputStream)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 Reader (java.io.Reader)14