Skip to content

Commit

Permalink
Fix Issues With Large Resource Files ..
Browse files Browse the repository at this point in the history
  • Loading branch information
AndroThink committed Nov 22, 2019
1 parent d4042c3 commit 0e0b9ce
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.androthink.server.helper.ServerHelper;

import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Date;
Expand Down Expand Up @@ -169,6 +170,25 @@ public void sendHtmlFileResponse(int code, String filename, @NonNull Map<String,
this.responseStream.close();
}

/**
* @param filename filename for the html file to be sent .
* @throws IOException throws exception if response channel closed .
*/
public void sendResourceFileResponse(String filename) throws IOException {

BufferedInputStream file = ServerHelper.getResourceFromAsset(context, filename);
sendResponseHeader(ServerHelper.RESPONSE_CODE.OK, ServerHelper.CONTENT_TYPE.HTML, file.available());

int u;
byte[] buffer = new byte[1024];
while ((u = file.read(buffer, 0, buffer.length)) != -1) {
this.responseStream.write(buffer, 0, u);
}
file.close();
this.responseStream.flush();
this.responseStream.close();
}

public Context getContext() {
return this.context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.RawRes;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -92,6 +93,10 @@ public static String getHtmlFromAsset(@NonNull Context context,String filename)
return builder.toString();
}

public static BufferedInputStream getResourceFromAsset(@NonNull Context context,String filename)throws IOException {
return new BufferedInputStream(context.getAssets().open(filename));
}

@NonNull
public static byte[] convertBitmapToByte(@NonNull Bitmap bitmap,int quality) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Expand Down

0 comments on commit 0e0b9ce

Please sign in to comment.