Skip to content

Commit 0e0b9ce

Browse files
committed
Fix Issues With Large Resource Files ..
1 parent d4042c3 commit 0e0b9ce

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

server/src/main/java/com/androthink/server/handler/ResponseHandler.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import com.androthink.server.helper.ServerHelper;
88

9+
import java.io.BufferedInputStream;
910
import java.io.DataOutputStream;
1011
import java.io.IOException;
1112
import java.util.Date;
@@ -169,6 +170,25 @@ public void sendHtmlFileResponse(int code, String filename, @NonNull Map<String,
169170
this.responseStream.close();
170171
}
171172

173+
/**
174+
* @param filename filename for the html file to be sent .
175+
* @throws IOException throws exception if response channel closed .
176+
*/
177+
public void sendResourceFileResponse(String filename) throws IOException {
178+
179+
BufferedInputStream file = ServerHelper.getResourceFromAsset(context, filename);
180+
sendResponseHeader(ServerHelper.RESPONSE_CODE.OK, ServerHelper.CONTENT_TYPE.HTML, file.available());
181+
182+
int u;
183+
byte[] buffer = new byte[1024];
184+
while ((u = file.read(buffer, 0, buffer.length)) != -1) {
185+
this.responseStream.write(buffer, 0, u);
186+
}
187+
file.close();
188+
this.responseStream.flush();
189+
this.responseStream.close();
190+
}
191+
172192
public Context getContext() {
173193
return this.context;
174194
}

server/src/main/java/com/androthink/server/helper/ServerHelper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import androidx.annotation.NonNull;
99
import androidx.annotation.RawRes;
1010

11+
import java.io.BufferedInputStream;
1112
import java.io.BufferedReader;
1213
import java.io.ByteArrayOutputStream;
1314
import java.io.IOException;
@@ -92,6 +93,10 @@ public static String getHtmlFromAsset(@NonNull Context context,String filename)
9293
return builder.toString();
9394
}
9495

96+
public static BufferedInputStream getResourceFromAsset(@NonNull Context context,String filename)throws IOException {
97+
return new BufferedInputStream(context.getAssets().open(filename));
98+
}
99+
95100
@NonNull
96101
public static byte[] convertBitmapToByte(@NonNull Bitmap bitmap,int quality) {
97102
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

0 commit comments

Comments
 (0)