当前位置: 首页>>代码示例>>Java>>正文


Java URLConnection.getHeaderFieldInt方法代码示例

本文整理汇总了Java中java.net.URLConnection.getHeaderFieldInt方法的典型用法代码示例。如果您正苦于以下问题:Java URLConnection.getHeaderFieldInt方法的具体用法?Java URLConnection.getHeaderFieldInt怎么用?Java URLConnection.getHeaderFieldInt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.net.URLConnection的用法示例。


在下文中一共展示了URLConnection.getHeaderFieldInt方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: shoutcastProxyReaderLoop

import java.net.URLConnection; //导入方法依赖的package包/类
private void shoutcastProxyReaderLoop(Socket proxy, URLConnection connection) throws IOException {

        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);
        connection.setRequestProperty("Icy-MetaData", "1");
        connection.connect();

        InputStream in = connection.getInputStream();

        OutputStream out = proxy.getOutputStream();
        out.write(("HTTP/1.0 200 OK\r\n" +
                "Pragma: no-cache\r\n" +
                "Content-Type: " + connection.getContentType() +
                "\r\n\r\n").getBytes(StandardCharsets.UTF_8));

        byte buf[] = new byte[16384]; // one second of 128kbit stream
        int count = 0;
        int total = 0;
        int metadataSize = 0;
        final int metadataOffset = connection.getHeaderFieldInt("icy-metaint", 0);
        int bitRate = Math.max(connection.getHeaderFieldInt("icy-br", 128), 32);
        LogHelper.v(LOG_TAG, "createProxyConnection: connected, icy-metaint " + metadataOffset + " icy-br " + bitRate);
        while (true) {
            count = Math.min(in.available(), buf.length);
            if (count <= 0) {
                count = Math.min(bitRate * 64, buf.length); // buffer half-second of stream data
            }
            if (metadataOffset > 0) {
                count = Math.min(count, metadataOffset - total);
            }

            count = in.read(buf, 0, count);
            if (count == 0) {
                continue;
            }
            if (count < 0) {
                break;
            }

            out.write(buf, 0, count);

            total += count;
            if (metadataOffset > 0 && total >= metadataOffset) {
                // read metadata
                total = 0;
                count = in.read();
                if (count < 0) {
                    break;
                }
                count *= 16;
                metadataSize = count;
                if (metadataSize == 0) {
                    continue;
                }
                // maximum metadata length is 4080 bytes
                total = 0;
                while (total < metadataSize) {
                    count = in.read(buf, total, count);
                    if (count < 0) {
                        break;
                    }
                    if (count == 0) {
                        continue;
                    }
                    total += count;
                    count = metadataSize - total;
                }
                total = 0;
                String[] metadata = new String(buf, 0, metadataSize, StandardCharsets.UTF_8).split(";");
                for (String s : metadata) {
                    if (s.indexOf(TransistorKeys.SHOUTCAST_STREAM_TITLE_HEADER) == 0 &&
                            s.length() >= TransistorKeys.SHOUTCAST_STREAM_TITLE_HEADER.length() + 1) {
                        handleMetadataString(s.substring(TransistorKeys.SHOUTCAST_STREAM_TITLE_HEADER.length(), s.length() - 1));
                        // break;
                    }
                }
            }
        }
    }
 
开发者ID:malah-code,项目名称:Open-Quran-Radio,代码行数:80,代码来源:MetadataHelper.java


注:本文中的java.net.URLConnection.getHeaderFieldInt方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。