package com.mulberry.player; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.Map; import org.jaudiotagger.tag.id3.framebody.FrameBodyCOMM; /* JADX INFO: loaded from: classes.dex */ public final class Net { public static final String UA = "Mulberry-Android/1.0 ( https://github.com/BeamStarTech/Mulberry )"; public static String enc(String str) { if (str == null) { str = FrameBodyCOMM.DEFAULT; } try { return URLEncoder.encode(str, "UTF-8"); } catch (Exception e) { return FrameBodyCOMM.DEFAULT; } } public static String query(Map map) { StringBuilder sb = new StringBuilder(); for (Map.Entry entry : map.entrySet()) { if (sb.length() > 0) { sb.append('&'); } sb.append(enc(entry.getKey())).append('=').append(enc(entry.getValue())); } return sb.toString(); } public static byte[] getBytes(String str, Map map) throws IOException { HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(str).openConnection(); httpURLConnection.setConnectTimeout(10000); httpURLConnection.setReadTimeout(15000); httpURLConnection.setInstanceFollowRedirects(true); httpURLConnection.setRequestProperty("User-Agent", UA); if (map != null) { for (Map.Entry entry : map.entrySet()) { httpURLConnection.setRequestProperty(entry.getKey(), entry.getValue()); } } try { int responseCode = httpURLConnection.getResponseCode(); InputStream errorStream = responseCode >= 400 ? httpURLConnection.getErrorStream() : httpURLConnection.getInputStream(); byte[] all = errorStream == null ? new byte[0] : readAll(errorStream); if (responseCode >= 400 && responseCode != 403 && responseCode != 404) { throw new IOException("HTTP " + responseCode); } if (responseCode == 404) { byte[] bArr = new byte[0]; httpURLConnection.disconnect(); return bArr; } httpURLConnection.disconnect(); return all; } catch (Throwable th) { httpURLConnection.disconnect(); throw th; } } public static String get(String str, Map map) throws IOException { return new String(getBytes(str, map), "UTF-8"); } public static String post(String str, String str2) throws IOException { HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(str).openConnection(); httpURLConnection.setConnectTimeout(10000); httpURLConnection.setReadTimeout(15000); httpURLConnection.setDoOutput(true); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setRequestProperty("User-Agent", UA); httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); try { OutputStream outputStream = httpURLConnection.getOutputStream(); outputStream.write(str2.getBytes("UTF-8")); outputStream.close(); InputStream errorStream = httpURLConnection.getResponseCode() >= 400 ? httpURLConnection.getErrorStream() : httpURLConnection.getInputStream(); return errorStream == null ? FrameBodyCOMM.DEFAULT : new String(readAll(errorStream), "UTF-8"); } finally { httpURLConnection.disconnect(); } } public static byte[] readAll(InputStream inputStream) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] bArr = new byte[16384]; while (true) { try { int i = inputStream.read(bArr); if (i <= 0) { inputStream.close(); return byteArrayOutputStream.toByteArray(); } byteArrayOutputStream.write(bArr, 0, i); } catch (Throwable th) { inputStream.close(); throw th; } } } }