Search in sources :

Example 1 with NetException

use of com.acgist.snail.context.exception.NetException in project snail by acgist.

the class HlsClient method checkCompleted.

/**
 * <p>校验是否下载完成</p>
 *
 * @param downloadSize 已经下载大小
 *
 * @return 是否下载完成
 */
private boolean checkCompleted(final long downloadSize) {
    // 如果文件已经下载完成直接返回
    if (this.completed) {
        return this.completed;
    }
    final File file = new File(this.path);
    if (!file.exists()) {
        return false;
    }
    try {
        final var header = HttpClient.newInstance(this.link).head().responseHeader();
        this.size = header.fileSize();
        return this.size == downloadSize;
    } catch (NetException e) {
        LOGGER.error("HLS文件校验异常:{}", this.link, e);
    }
    return false;
}
Also used : NetException(com.acgist.snail.context.exception.NetException) File(java.io.File)

Example 2 with NetException

use of com.acgist.snail.context.exception.NetException in project snail by acgist.

the class HttpClient method responseToString.

/**
 * <p>获取响应文本</p>
 *
 * @return 响应文本
 *
 * @throws NetException 网络异常
 */
public String responseToString() throws NetException {
    int length;
    final var input = this.response();
    final var bytes = new byte[SystemConfig.DEFAULT_EXCHANGE_LENGTH];
    final var builder = new StringBuilder();
    try {
        while ((length = input.read(bytes)) >= 0) {
            builder.append(new String(bytes, 0, length));
        }
    } catch (IOException e) {
        throw new NetException(e);
    } finally {
        IoUtils.close(input);
    }
    return builder.toString();
}
Also used : NetException(com.acgist.snail.context.exception.NetException) IOException(java.io.IOException)

Example 3 with NetException

use of com.acgist.snail.context.exception.NetException in project snail by acgist.

the class HttpClient method execute.

/**
 * <p>执行请求</p>
 *
 * @param method 请求方法
 * @param body 请求数据
 *
 * @return {@link HttpClient}
 *
 * @throws NetException 网络异常
 */
public HttpClient execute(Method method, String body) throws NetException {
    OutputStream output = null;
    try {
        // 设置请求方式
        this.httpURLConnection.setRequestMethod(method.name());
        if (method == Method.GET) {
            // 是否写出:GET不要写出
            this.httpURLConnection.setDoOutput(false);
        } else if (method == Method.HEAD) {
            // 是否写出:HEAD不要写出
            this.httpURLConnection.setDoOutput(false);
        } else if (method == Method.POST) {
            // 是否写出:POST需要写出
            this.httpURLConnection.setDoOutput(true);
        } else {
            throw new NetException("不支持的请求方式:" + method);
        }
        // 发起连接
        this.httpURLConnection.connect();
        // 发送请求参数
        if (body != null) {
            output = this.httpURLConnection.getOutputStream();
            output.write(body.getBytes());
        }
        // 设置状态码
        this.code = this.httpURLConnection.getResponseCode();
    } catch (IOException e) {
        throw new NetException(e);
    } finally {
        IoUtils.close(output);
    }
    return this;
}
Also used : NetException(com.acgist.snail.context.exception.NetException) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Example 4 with NetException

use of com.acgist.snail.context.exception.NetException in project snail by acgist.

the class HttpClient method buildHttpURLConnection.

/**
 * <p>新建请求连接</p>
 *
 * @param connectTimeout 连接超时时间(毫秒)
 * @param receiveTimeout 响应超时时间(毫秒)
 *
 * @return 请求连接
 *
 * @throws NetException 网络异常
 */
private HttpURLConnection buildHttpURLConnection(int connectTimeout, int receiveTimeout) throws NetException {
    try {
        final var requestUrl = new URL(this.url);
        final var connection = (HttpURLConnection) requestUrl.openConnection();
        // 是否读取
        connection.setDoInput(true);
        // 是否缓存
        connection.setUseCaches(false);
        // 响应超时时间
        connection.setReadTimeout(receiveTimeout);
        // 连接超时时间
        connection.setConnectTimeout(connectTimeout);
        // 是否自动重定向
        connection.setInstanceFollowRedirects(true);
        return connection;
    } catch (IOException e) {
        throw new NetException(e);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) NetException(com.acgist.snail.context.exception.NetException) IOException(java.io.IOException) URL(java.net.URL)

Example 5 with NetException

use of com.acgist.snail.context.exception.NetException in project snail by acgist.

the class TcpMessageHandler method send.

@Override
public void send(ByteBuffer buffer, int timeout) throws NetException {
    this.check(buffer);
    // 阻塞线程(等待发送完成):防止多线程同时写导致WritePendingException
    synchronized (this.channel) {
        try {
            int size;
            final Future<Integer> future = this.channel.write(buffer);
            // 超时时间:超时异常导致数据没有发送完成但释放了锁从而引起一连串的WritePendingException
            if (timeout <= SystemConfig.NONE_TIMEOUT) {
                // 没有超时:除了连接消息(首条消息)以外所有消息都不使用超时时间
                size = future.get();
            } else {
                // 超时时间:连接消息(首条消息)使用超时时间
                size = future.get(timeout, TimeUnit.SECONDS);
            }
            if (size <= 0) {
                LOGGER.warn("TCP消息发送失败:{}-{}", this.channel, size);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new NetException(e);
        } catch (TimeoutException | ExecutionException e) {
            throw new NetException(e);
        }
    }
}
Also used : NetException(com.acgist.snail.context.exception.NetException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

NetException (com.acgist.snail.context.exception.NetException)18 IOException (java.io.IOException)6 DownloadException (com.acgist.snail.context.exception.DownloadException)3 File (java.io.File)3 InvalidKeyException (java.security.InvalidKeyException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)3 Logger (com.acgist.snail.logger.Logger)2 LoggerFactory (com.acgist.snail.logger.LoggerFactory)2 M3u8 (com.acgist.snail.pojo.bean.M3u8)2 Type (com.acgist.snail.pojo.bean.M3u8.Type)2 StringUtils (com.acgist.snail.utils.StringUtils)2 OutputStream (java.io.OutputStream)2 Key (java.security.Key)2 Cipher (javax.crypto.Cipher)2 Test (org.junit.jupiter.api.Test)2 SymbolConfig (com.acgist.snail.config.SymbolConfig)1 NatContext (com.acgist.snail.context.NatContext)1 UpnpContext (com.acgist.snail.context.UpnpContext)1 JSON (com.acgist.snail.format.JSON)1