Search in sources :

Example 86 with ProtocolException

use of java.net.ProtocolException in project platform_frameworks_base by android.

the class NetworkStatsCollection method read.

public void read(DataInputStream in) throws IOException {
    // verify file magic header intact
    final int magic = in.readInt();
    if (magic != FILE_MAGIC) {
        throw new ProtocolException("unexpected magic: " + magic);
    }
    final int version = in.readInt();
    switch(version) {
        case VERSION_UNIFIED_INIT:
            {
                // uid := size *(NetworkIdentitySet size *(uid set tag NetworkStatsHistory))
                final int identSize = in.readInt();
                for (int i = 0; i < identSize; i++) {
                    final NetworkIdentitySet ident = new NetworkIdentitySet(in);
                    final int size = in.readInt();
                    for (int j = 0; j < size; j++) {
                        final int uid = in.readInt();
                        final int set = in.readInt();
                        final int tag = in.readInt();
                        final Key key = new Key(ident, uid, set, tag);
                        final NetworkStatsHistory history = new NetworkStatsHistory(in);
                        recordHistory(key, history);
                    }
                }
                break;
            }
        default:
            {
                throw new ProtocolException("unexpected version: " + version);
            }
    }
}
Also used : ProtocolException(java.net.ProtocolException) NetworkStatsHistory(android.net.NetworkStatsHistory)

Example 87 with ProtocolException

use of java.net.ProtocolException in project camel by apache.

the class HttpHelper method parserHttpVersion.

public static int[] parserHttpVersion(String s) throws ProtocolException {
    int major;
    int minor;
    if (s == null) {
        throw new IllegalArgumentException("String may not be null");
    }
    if (!s.startsWith("HTTP/")) {
        throw new ProtocolException("Invalid HTTP version string: " + s);
    }
    int i1 = "HTTP/".length();
    int i2 = s.indexOf(".", i1);
    if (i2 == -1) {
        throw new ProtocolException("Invalid HTTP version number: " + s);
    }
    try {
        major = Integer.parseInt(s.substring(i1, i2));
    } catch (NumberFormatException e) {
        throw new ProtocolException("Invalid HTTP major version number: " + s);
    }
    i1 = i2 + 1;
    i2 = s.length();
    try {
        minor = Integer.parseInt(s.substring(i1, i2));
    } catch (NumberFormatException e) {
        throw new ProtocolException("Invalid HTTP minor version number: " + s);
    }
    return new int[] { major, minor };
}
Also used : ProtocolException(java.net.ProtocolException)

Example 88 with ProtocolException

use of java.net.ProtocolException in project android_frameworks_base by ParanoidAndroid.

the class NetworkStatsFactory method readNetworkStatsSummaryXt.

/**
     * Parse and return interface-level summary {@link NetworkStats}. Designed
     * to return only IP layer traffic. Values monotonically increase since
     * device boot, and may include details about inactive interfaces.
     *
     * @throws IllegalStateException when problem parsing stats.
     */
public NetworkStats readNetworkStatsSummaryXt() throws IOException {
    final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
    // return null when kernel doesn't support
    if (!mStatsXtIfaceFmt.exists())
        return null;
    final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 6);
    final NetworkStats.Entry entry = new NetworkStats.Entry();
    ProcFileReader reader = null;
    try {
        // open and consume header line
        reader = new ProcFileReader(new FileInputStream(mStatsXtIfaceFmt));
        reader.finishLine();
        while (reader.hasMoreData()) {
            entry.iface = reader.nextString();
            entry.uid = UID_ALL;
            entry.set = SET_ALL;
            entry.tag = TAG_NONE;
            entry.rxBytes = reader.nextLong();
            entry.rxPackets = reader.nextLong();
            entry.txBytes = reader.nextLong();
            entry.txPackets = reader.nextLong();
            stats.addValues(entry);
            reader.finishLine();
        }
    } catch (NullPointerException e) {
        throw new ProtocolException("problem parsing stats", e);
    } catch (NumberFormatException e) {
        throw new ProtocolException("problem parsing stats", e);
    } finally {
        IoUtils.closeQuietly(reader);
        StrictMode.setThreadPolicy(savedPolicy);
    }
    return stats;
}
Also used : StrictMode(android.os.StrictMode) ProcFileReader(com.android.internal.util.ProcFileReader) ProtocolException(java.net.ProtocolException) NetworkStats(android.net.NetworkStats) FileInputStream(java.io.FileInputStream)

Example 89 with ProtocolException

use of java.net.ProtocolException in project android_frameworks_base by ParanoidAndroid.

the class NetworkStatsCollection method readLegacyUid.

@Deprecated
public void readLegacyUid(File file, boolean onlyTags) throws IOException {
    final AtomicFile inputFile = new AtomicFile(file);
    DataInputStream in = null;
    try {
        in = new DataInputStream(new BufferedInputStream(inputFile.openRead()));
        // verify file magic header intact
        final int magic = in.readInt();
        if (magic != FILE_MAGIC) {
            throw new ProtocolException("unexpected magic: " + magic);
        }
        final int version = in.readInt();
        switch(version) {
            case VERSION_UID_INIT:
                {
                    // mapping into NetworkIdentitySet.
                    break;
                }
            case VERSION_UID_WITH_IDENT:
                {
                    // for a short time.
                    break;
                }
            case VERSION_UID_WITH_TAG:
            case VERSION_UID_WITH_SET:
                {
                    // uid := size *(NetworkIdentitySet size *(uid set tag NetworkStatsHistory))
                    final int identSize = in.readInt();
                    for (int i = 0; i < identSize; i++) {
                        final NetworkIdentitySet ident = new NetworkIdentitySet(in);
                        final int size = in.readInt();
                        for (int j = 0; j < size; j++) {
                            final int uid = in.readInt();
                            final int set = (version >= VERSION_UID_WITH_SET) ? in.readInt() : SET_DEFAULT;
                            final int tag = in.readInt();
                            final Key key = new Key(ident, uid, set, tag);
                            final NetworkStatsHistory history = new NetworkStatsHistory(in);
                            if ((tag == TAG_NONE) != onlyTags) {
                                recordHistory(key, history);
                            }
                        }
                    }
                    break;
                }
            default:
                {
                    throw new ProtocolException("unexpected version: " + version);
                }
        }
    } catch (FileNotFoundException e) {
    // missing stats is okay, probably first boot
    } finally {
        IoUtils.closeQuietly(in);
    }
}
Also used : ProtocolException(java.net.ProtocolException) AtomicFile(android.util.AtomicFile) BufferedInputStream(java.io.BufferedInputStream) FileNotFoundException(java.io.FileNotFoundException) NetworkStatsHistory(android.net.NetworkStatsHistory) DataInputStream(java.io.DataInputStream)

Example 90 with ProtocolException

use of java.net.ProtocolException in project robovm by robovm.

the class RawHeaders method fromNameValueBlock.

/** Returns headers for a name value block containing a SPDY response. */
public static RawHeaders fromNameValueBlock(List<String> nameValueBlock) throws IOException {
    if (nameValueBlock.size() % 2 != 0) {
        throw new IllegalArgumentException("Unexpected name value block: " + nameValueBlock);
    }
    String status = null;
    String version = null;
    RawHeaders result = new RawHeaders();
    for (int i = 0; i < nameValueBlock.size(); i += 2) {
        String name = nameValueBlock.get(i);
        String values = nameValueBlock.get(i + 1);
        for (int start = 0; start < values.length(); ) {
            int end = values.indexOf('\0', start);
            if (end == -1) {
                end = values.length();
            }
            String value = values.substring(start, end);
            if (":status".equals(name)) {
                status = value;
            } else if (":version".equals(name)) {
                version = value;
            } else {
                result.namesAndValues.add(name);
                result.namesAndValues.add(value);
            }
            start = end + 1;
        }
    }
    if (status == null)
        throw new ProtocolException("Expected ':status' header not present");
    if (version == null)
        throw new ProtocolException("Expected ':version' header not present");
    result.setStatusLine(version + " " + status);
    return result;
}
Also used : ProtocolException(java.net.ProtocolException)

Aggregations

ProtocolException (java.net.ProtocolException)143 IOException (java.io.IOException)34 HttpURLConnection (java.net.HttpURLConnection)26 URL (java.net.URL)24 FileInputStream (java.io.FileInputStream)19 NetworkStats (android.net.NetworkStats)18 NetworkStatsHistory (android.net.NetworkStatsHistory)18 StrictMode (android.os.StrictMode)18 ProcFileReader (com.android.internal.util.ProcFileReader)18 BufferedInputStream (java.io.BufferedInputStream)17 FileNotFoundException (java.io.FileNotFoundException)17 AtomicFile (android.util.AtomicFile)12 DataInputStream (java.io.DataInputStream)12 Test (org.junit.Test)12 InputStream (java.io.InputStream)11 OutputStream (java.io.OutputStream)11 MalformedURLException (java.net.MalformedURLException)11 Map (java.util.Map)11 BufferedReader (java.io.BufferedReader)10 InputStreamReader (java.io.InputStreamReader)10