40 | * Temp file managers are created 1-to-1 with incoming requests, to create and 41 | * cleanup temporary files created as a result of handling the request. 42 | *
43 | */ 44 | public interface ITempFileManager { 45 | 46 | void clear(); 47 | 48 | public ITempFile createTempFile(String filename_hint) throws Exception; 49 | } 50 | -------------------------------------------------------------------------------- /lib/src/main/java/org/nanohttpd2/protocols/http/tempfiles/ITempFile.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd2.protocols.http.tempfiles; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.OutputStream; 37 | 38 | /** 39 | * A temp file. 40 | * 41 | *42 | * Temp files are responsible for managing the actual temporary storage and 43 | * cleaning themselves up when no longer needed. 44 | *
45 | */ 46 | public interface ITempFile { 47 | 48 | public void delete() throws Exception; 49 | 50 | public String getName(); 51 | 52 | public OutputStream open() throws Exception; 53 | } 54 | -------------------------------------------------------------------------------- /lib/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | apply plugin: 'kotlin-android' 4 | 5 | apply plugin: 'com.novoda.bintray-release' 6 | 7 | android { 8 | compileSdkVersion 28 9 | 10 | compileOptions { 11 | kotlinOptions.freeCompilerArgs += ['-module-name', "com.tlz.ada"] 12 | } 13 | 14 | defaultConfig { 15 | minSdkVersion 16 16 | targetSdkVersion 28 17 | versionCode 1 18 | versionName "1.0" 19 | 20 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 21 | 22 | } 23 | 24 | buildTypes { 25 | release { 26 | minifyEnabled false 27 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 28 | } 29 | } 30 | 31 | sourceSets { 32 | main { 33 | java.srcDirs += 'src/main/kotlin' 34 | } 35 | } 36 | } 37 | 38 | //sourceCompatibility = "1.6" 39 | //targetCompatibility = "1.6" 40 | 41 | dependencies { 42 | implementation fileTree(dir: 'libs', include: ['*.jar']) 43 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 44 | implementation 'androidx.appcompat:appcompat:1.0.0' 45 | implementation 'androidx.room:room-runtime:2.2.1' 46 | // implementation 'android.arch.persistence.room:runtime:1.1.1' 47 | api 'com.google.code.gson:gson:2.8.5' 48 | api 'net.zetetic:android-database-sqlcipher:4.2.0@aar' 49 | testImplementation 'junit:junit:4.12' 50 | androidTestImplementation 'androidx.test:runner:1.1.0' 51 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' 52 | } 53 | 54 | buildscript{ 55 | tasks.withType(Javadoc) { 56 | failOnError false 57 | options{ 58 | encoding "UTF-8" 59 | charSet 'UTF-8' 60 | links "http://docs.oracle.com/javase/7/docs/api" 61 | } 62 | } 63 | } 64 | 65 | publish { 66 | userOrg = parent.ext.userOrg 67 | artifactId = parent.ext.artifactId 68 | autoPublish = parent.ext.autoPublish 69 | desc = parent.ext.desc 70 | groupId = parent.ext.groupId 71 | publishVersion = parent.ext.publishVersion 72 | uploadName = parent.ext.uploadName 73 | website = parent.ext.website 74 | repoName = parent.ext.repoName 75 | } 76 | -------------------------------------------------------------------------------- /lib/src/main/java/org/nanohttpd2/protocols/http/sockets/DefaultServerSocketFactory.java: -------------------------------------------------------------------------------- 1 | package org.nanohttpd2.protocols.http.sockets; 2 | 3 | /* 4 | * #%L 5 | * NanoHttpd-Core 6 | * %% 7 | * Copyright (C) 2012 - 2016 nanohttpd 8 | * %% 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * 1. Redistributions of source code must retain the above copyright notice, this 13 | * list of conditions and the following disclaimer. 14 | * 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 19 | * 3. Neither the name of the nanohttpd nor the names of its contributors 20 | * may be used to endorse or promote products derived from this software without 21 | * specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 | * OF THE POSSIBILITY OF SUCH DAMAGE. 33 | * #L% 34 | */ 35 | 36 | import java.io.IOException; 37 | import java.net.ServerSocket; 38 | 39 | import org.nanohttpd2.util.IFactoryThrowing; 40 | 41 | /** 42 | * Creates a normal ServerSocket for TCP connections 43 | */ 44 | public class DefaultServerSocketFactory implements IFactoryThrowingString back
38 | * to its enum value.
39 | */
40 | public enum Method {
41 | GET,
42 | PUT,
43 | POST,
44 | DELETE,
45 | HEAD,
46 | OPTIONS,
47 | TRACE,
48 | CONNECT,
49 | PATCH,
50 | PROPFIND,
51 | PROPPATCH,
52 | MKCOL,
53 | MOVE,
54 | COPY,
55 | LOCK,
56 | UNLOCK,
57 | NOTIFY,
58 | SUBSCRIBE;
59 |
60 | public static Method lookup(String method) {
61 | if (method == null)
62 | return null;
63 |
64 | try {
65 | return valueOf(method);
66 | } catch (IllegalArgumentException e) {
67 | // TODO: Log it?
68 | return null;
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/lib/src/main/java/org/nanohttpd2/protocols/websockets/CloseCode.java:
--------------------------------------------------------------------------------
1 | package org.nanohttpd2.protocols.websockets;
2 |
3 | /*
4 | * #%L
5 | * NanoHttpd-Websocket
6 | * %%
7 | * Copyright (C) 2012 - 2016 nanohttpd
8 | * %%
9 | * Redistribution and use in source and binary forms, with or without modification,
10 | * are permitted provided that the following conditions are met:
11 | *
12 | * 1. Redistributions of source code must retain the above copyright notice, this
13 | * list of conditions and the following disclaimer.
14 | *
15 | * 2. Redistributions in binary form must reproduce the above copyright notice,
16 | * this list of conditions and the following disclaimer in the documentation
17 | * and/or other materials provided with the distribution.
18 | *
19 | * 3. Neither the name of the nanohttpd nor the names of its contributors
20 | * may be used to endorse or promote products derived from this software without
21 | * specific prior written permission.
22 | *
23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32 | * OF THE POSSIBILITY OF SUCH DAMAGE.
33 | * #L%
34 | */
35 |
36 | public enum CloseCode {
37 | NormalClosure(1000),
38 | GoingAway(1001),
39 | ProtocolError(1002),
40 | UnsupportedData(1003),
41 | NoStatusRcvd(1005),
42 | AbnormalClosure(1006),
43 | InvalidFramePayloadData(1007),
44 | PolicyViolation(1008),
45 | MessageTooBig(1009),
46 | MandatoryExt(1010),
47 | InternalServerError(1011),
48 | TLSHandshake(1015);
49 |
50 | public static CloseCode find(int value) {
51 | for (CloseCode code : values()) {
52 | if (code.getValue() == value) {
53 | return code;
54 | }
55 | }
56 | return null;
57 | }
58 |
59 | private final int code;
60 |
61 | private CloseCode(int code) {
62 | this.code = code;
63 | }
64 |
65 | public int getValue() {
66 | return this.code;
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/lib/src/main/java/org/nanohttpd2/protocols/websockets/WebSocketException.java:
--------------------------------------------------------------------------------
1 | package org.nanohttpd2.protocols.websockets;
2 |
3 | /*
4 | * #%L
5 | * NanoHttpd-Websocket
6 | * %%
7 | * Copyright (C) 2012 - 2016 nanohttpd
8 | * %%
9 | * Redistribution and use in source and binary forms, with or without modification,
10 | * are permitted provided that the following conditions are met:
11 | *
12 | * 1. Redistributions of source code must retain the above copyright notice, this
13 | * list of conditions and the following disclaimer.
14 | *
15 | * 2. Redistributions in binary form must reproduce the above copyright notice,
16 | * this list of conditions and the following disclaimer in the documentation
17 | * and/or other materials provided with the distribution.
18 | *
19 | * 3. Neither the name of the nanohttpd nor the names of its contributors
20 | * may be used to endorse or promote products derived from this software without
21 | * specific prior written permission.
22 | *
23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32 | * OF THE POSSIBILITY OF SUCH DAMAGE.
33 | * #L%
34 | */
35 |
36 | import java.io.IOException;
37 |
38 | public class WebSocketException extends IOException {
39 |
40 | private static final long serialVersionUID = 1L;
41 |
42 | private final CloseCode code;
43 |
44 | private final String reason;
45 |
46 | public WebSocketException(CloseCode code, String reason) {
47 | this(code, reason, null);
48 | }
49 |
50 | public WebSocketException(CloseCode code, String reason, Exception cause) {
51 | super(code + ": " + reason, cause);
52 | this.code = code;
53 | this.reason = reason;
54 | }
55 |
56 | public WebSocketException(Exception cause) {
57 | this(CloseCode.InternalServerError, cause.toString(), cause);
58 | }
59 |
60 | public CloseCode getCode() {
61 | return this.code;
62 | }
63 |
64 | public String getReason() {
65 | return this.reason;
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Android开发辅助依赖库
2 | - 数据库增删查改,excel表导出,数据库文件导出
3 | - SharePreferences增删查改
4 | - 应用实时日志查看和历史日志查看,也支持历史日志下载与删除
5 | - 手机所有应用的信息查看,如:Activity、Service、权限等信息,也可以直接下载手机上的应用到电脑上
6 | - 通过web直接安装应用到手机上
7 | - 手机文件管理,包括新增文件、文件夹,删除文件,批量上传文件都手机指定文件夹中
8 | - 应用截图
9 |
10 | 数据库相关处理思路参考[Android-Debug-Database](https://github.com/amitshekhariitbhu/Android-Debug-Database)
11 | (已知目前数据访问bug:当app应用的数据库对象不关闭时(Room),工具将查询不到数据)
12 |
13 | web端使用Angular6开发,因为个人设备和能力有限,没有在太多手机上测试,不能保证100%兼容每台手机
14 |
15 | ## 如何使用
16 |
17 | ```
18 | 只需要在gradle文件中添加一下代码:
19 | Gradle3.0以上: debugImplementation 'com.tlz.tools:AndDevelopAssistant:0.1.2'
20 | Gradle3.0以下: debugCompile 'com.tlz.tools:AndDevelopAssistant:0.1.2'
21 | 非androidx版本:
22 | Gradle3.0以上: debugImplementation 'com.tlz.tools:AndDevelopAssistant:0.1.2-no-x'
23 | Gradle3.0以下: debugCompile 'com.tlz.tools:AndDevelopAssistant:0.1.2-no-x'
24 | ```
25 |
26 | 启动app,在浏览器中输入手机端ip地址+10000端口号进行访问(10000是默认端口号),如果不知道手机ip地址,可以在logcat窗口中查看名为AdaWebServer的日志,其中会输入完整的访问地址。日志记录功能默认开启
27 |
28 | ## 如何修改web访问端口
29 |
30 | 需要在build.gradle文件下加入以下代码:
31 |
32 | ```
33 | debug {
34 | resValue("string", "ADA_DEBUG_PORT", "你的端口号")
35 | }
36 | ```
37 |
38 | 当手机上有多个应用依赖该库时,就需要自定义端口,避免端口冲突
39 |
40 | ## 如何设置数据库密码
41 |
42 | 需要在Manifest中加入meta-data数据:
43 |
44 | ```
45 |
78 |
79 |
80 |
81 |
82 |
--------------------------------------------------------------------------------
/lib/src/main/java/org/nanohttpd2/protocols/http/response/ChunkedOutputStream.java:
--------------------------------------------------------------------------------
1 | package org.nanohttpd2.protocols.http.response;
2 |
3 | /*
4 | * #%L
5 | * NanoHttpd-Core
6 | * %%
7 | * Copyright (C) 2012 - 2016 nanohttpd
8 | * %%
9 | * Redistribution and use in source and binary forms, with or without modification,
10 | * are permitted provided that the following conditions are met:
11 | *
12 | * 1. Redistributions of source code must retain the above copyright notice, this
13 | * list of conditions and the following disclaimer.
14 | *
15 | * 2. Redistributions in binary form must reproduce the above copyright notice,
16 | * this list of conditions and the following disclaimer in the documentation
17 | * and/or other materials provided with the distribution.
18 | *
19 | * 3. Neither the name of the nanohttpd nor the names of its contributors
20 | * may be used to endorse or promote products derived from this software without
21 | * specific prior written permission.
22 | *
23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32 | * OF THE POSSIBILITY OF SUCH DAMAGE.
33 | * #L%
34 | */
35 |
36 | import java.io.FilterOutputStream;
37 | import java.io.IOException;
38 | import java.io.OutputStream;
39 |
40 | /**
41 | * Output stream that will automatically send every write to the wrapped
42 | * OutputStream according to chunked transfer:
43 | * http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1
44 | */
45 | public class ChunkedOutputStream extends FilterOutputStream {
46 |
47 | public ChunkedOutputStream(OutputStream out) {
48 | super(out);
49 | }
50 |
51 | @Override
52 | public void write(int b) throws IOException {
53 | byte[] data = {
54 | (byte) b
55 | };
56 | write(data, 0, 1);
57 | }
58 |
59 | @Override
60 | public void write(byte[] b) throws IOException {
61 | write(b, 0, b.length);
62 | }
63 |
64 | @Override
65 | public void write(byte[] b, int off, int len) throws IOException {
66 | if (len == 0)
67 | return;
68 | out.write(String.format("%x\r\n", len).getBytes());
69 | out.write(b, off, len);
70 | out.write("\r\n".getBytes());
71 | }
72 |
73 | public void finish() throws IOException {
74 | out.write("0\r\n\r\n".getBytes());
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/lib/src/main/java/org/nanohttpd2/util/ServerRunner.java:
--------------------------------------------------------------------------------
1 | package org.nanohttpd2.util;
2 |
3 | /*
4 | * #%L
5 | * NanoHttpd-Webserver
6 | * %%
7 | * Copyright (C) 2012 - 2015 nanohttpd
8 | * %%
9 | * Redistribution and use in source and binary forms, with or without modification,
10 | * are permitted provided that the following conditions are met:
11 | *
12 | * 1. Redistributions of source code must retain the above copyright notice, this
13 | * list of conditions and the following disclaimer.
14 | *
15 | * 2. Redistributions in binary form must reproduce the above copyright notice,
16 | * this list of conditions and the following disclaimer in the documentation
17 | * and/or other materials provided with the distribution.
18 | *
19 | * 3. Neither the name of the nanohttpd nor the names of its contributors
20 | * may be used to endorse or promote products derived from this software without
21 | * specific prior written permission.
22 | *
23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32 | * OF THE POSSIBILITY OF SUCH DAMAGE.
33 | * #L%
34 | */
35 |
36 | import java.io.IOException;
37 | import java.util.logging.Level;
38 | import java.util.logging.Logger;
39 |
40 | import org.nanohttpd2.protocols.http.NanoHTTPD;
41 |
42 | public class ServerRunner {
43 |
44 | /**
45 | * logger to log to.
46 | */
47 | private static final Logger LOG = Logger.getLogger(ServerRunner.class.getName());
48 |
49 | public static void executeInstance(NanoHTTPD server) {
50 | try {
51 | server.start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
52 | } catch (IOException ioe) {
53 | System.err.println("Couldn't start server:\n" + ioe);
54 | System.exit(-1);
55 | }
56 |
57 | System.out.println("Server started, Hit Enter to stop.\n");
58 |
59 | try {
60 | System.in.read();
61 | } catch (Throwable ignored) {
62 | }
63 |
64 | server.stop();
65 | System.out.println("Server stopped.\n");
66 | }
67 |
68 | public static
47 | * By default, files are created by File.createTempFile() in the
48 | * directory specified.
49 | *
47 | * This class stores its files in the standard location (that is, wherever
48 | * java.io.tmpdir points to). Files are added to an internal list,
49 | * and deleted when no longer needed (that is, when clear() is
50 | * invoked at the end of processing a request).
51 | *
46 | * By default, the server spawns a new Thread for every incoming request. These 47 | * are set to daemon status, and named according to the request number. 48 | * The name is useful when profiling the application. 49 | *
50 | */ 51 | public class DefaultAsyncRunner implements IAsyncRunner { 52 | 53 | protected long requestCount; 54 | 55 | private final List${log.content}
" 117 | "W" -> log.content = "${log.content}
" 118 | else -> log.content = "${log.content}
" 119 | } 120 | return log 121 | } 122 | 123 | companion object { 124 | private val pingPayload = "1337DEADBEEFC001".toByteArray() 125 | } 126 | 127 | } -------------------------------------------------------------------------------- /lib/src/main/kotlin/com/tlz/ada/AdaWebServer.kt: -------------------------------------------------------------------------------- 1 | package com.tlz.ada 2 | 3 | import android.Manifest 4 | import android.content.Context 5 | import android.util.Log 6 | import android.util.Pair 7 | import androidx.sqlite.db.SupportSQLiteDatabase 8 | import com.tlz.ada.db.AdaDataProvider 9 | import com.tlz.ada.db.AdaDataProviderImpl 10 | import com.tlz.ada.handlers.* 11 | import com.tlz.ada.socket.AdaWSD 12 | import org.nanohttpd2.protocols.http.IHTTPSession 13 | import org.nanohttpd2.protocols.http.NanoHTTPD 14 | import org.nanohttpd2.protocols.http.response.Response 15 | import org.nanohttpd2.protocols.http.response.Response.newFixedLengthResponse 16 | import org.nanohttpd2.protocols.http.response.Status 17 | import java.io.File 18 | 19 | 20 | /** 21 | * 服务器. 22 | * Created by tomlezen. 23 | * Data: 2018/1/27. 24 | * Time: 15:00. 25 | */ 26 | class AdaWebServer internal constructor(internal val ctx: Context, port: Int) : NanoHTTPD(port) { 27 | 28 | private val dataProvider: AdaDataProvider by lazy { AdaDataProviderImpl(ctx) } 29 | private val appManager by lazy { AdaApplicationManager(ctx) } 30 | private val activityLifeCycleHooker by lazy { AdaActivityLifeCycleListener(ctx) } 31 | 32 | /** 所有请求处理器. */ 33 | private val handlers = mutableListOf