Example usage for android.content.res AssetManager ACCESS_STREAMING

List of usage examples for android.content.res AssetManager ACCESS_STREAMING

Introduction

In this page you can find the example usage for android.content.res AssetManager ACCESS_STREAMING.

Prototype

int ACCESS_STREAMING

To view the source code for android.content.res AssetManager ACCESS_STREAMING.

Click Source Link

Document

Mode for #open(String,int) : Read sequentially, with an occasional forward seek.

Usage

From source file:net.facework.core.http.ModAssetServer.java

@Override
public void handle(final HttpRequest request, final HttpResponse response, final HttpContext context)
        throws HttpException, IOException {
    AbstractHttpEntity body = null;//ww w.  j  a v a 2 s.co  m

    final String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
    if (!method.equals("GET") && !method.equals("HEAD") && !method.equals("POST")) {
        throw new MethodNotSupportedException(method + " method not supported");
    }

    final String url = URLDecoder.decode(request.getRequestLine().getUri());
    if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
        byte[] entityContent = EntityUtils.toByteArray(entity);
        Log.d(TAG, "Incoming entity content (bytes): " + entityContent.length);
    }

    final String location = "www" + (url.equals("/") ? "/index.htm" : url);
    response.setStatusCode(HttpStatus.SC_OK);

    try {
        Log.i(TAG, "Requested: \"" + url + "\"");

        // Compares the Last-Modified date header (if present) with the If-Modified-Since date
        if (request.containsHeader("If-Modified-Since")) {
            try {
                Date date = DateUtils.parseDate(request.getHeaders("If-Modified-Since")[0].getValue());
                if (date.compareTo(mServer.mLastModified) <= 0) {
                    // The file has not been modified
                    response.setStatusCode(HttpStatus.SC_NOT_MODIFIED);
                    return;
                }
            } catch (DateParseException e) {
                e.printStackTrace();
            }
        }

        // We determine if the asset is compressed
        try {
            AssetFileDescriptor afd = mAssetManager.openFd(location);

            // The asset is not compressed
            FileInputStream fis = new FileInputStream(afd.getFileDescriptor());
            fis.skip(afd.getStartOffset());
            body = new InputStreamEntity(fis, afd.getDeclaredLength());

            Log.d(TAG, "Serving uncompressed file " + "www" + url);

        } catch (FileNotFoundException e) {

            // The asset may be compressed
            // AAPT compresses assets so first we need to uncompress them to determine their length
            InputStream stream = mAssetManager.open(location, AssetManager.ACCESS_STREAMING);
            ByteArrayOutputStream buffer = new ByteArrayOutputStream(64000);
            byte[] tmp = new byte[4096];
            int length = 0;
            while ((length = stream.read(tmp)) != -1)
                buffer.write(tmp, 0, length);
            body = new InputStreamEntity(new ByteArrayInputStream(buffer.toByteArray()), buffer.size());
            stream.close();

            Log.d(TAG, "Serving compressed file " + "www" + url);

        }

        body.setContentType(getMimeMediaType(url) + "; charset=UTF-8");
        response.addHeader("Last-Modified", DateUtils.formatDate(mServer.mLastModified));

    } catch (IOException e) {
        // File does not exist
        response.setStatusCode(HttpStatus.SC_NOT_FOUND);
        body = new EntityTemplate(new ContentProducer() {
            @Override
            public void writeTo(final OutputStream outstream) throws IOException {
                OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
                writer.write("<html><body><h1>");
                writer.write("File ");
                writer.write("www" + url);
                writer.write(" not found");
                writer.write("</h1></body></html>");
                writer.flush();
            }
        });
        Log.d(TAG, "File " + "www" + url + " not found");
        body.setContentType("text/html; charset=UTF-8");
    }

    response.setEntity(body);

}

From source file:com.wifi.brainbreaker.mydemo.http.ModAssetServer.java

public void handle(final HttpRequest request, final HttpResponse response, final HttpContext context)
        throws HttpException, IOException {
    AbstractHttpEntity body = null;// w ww .ja v  a2s.c  o  m

    final String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
    if (!method.equals("GET") && !method.equals("HEAD") && !method.equals("POST")) {
        throw new MethodNotSupportedException(method + " method not supported");
    }

    final String url = URLDecoder.decode(request.getRequestLine().getUri());
    if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
        byte[] entityContent = EntityUtils.toByteArray(entity);
        Log.d(TAG, "Incoming entity content (bytes): " + entityContent.length);
    }

    final String location = "www" + (url.equals("/") ? "/index.htm" : url);
    response.setStatusCode(HttpStatus.SC_OK);

    try {
        Log.i(TAG, "Requested: \"" + url + "\"");

        // Compares the Last-Modified date header (if present) with the If-Modified-Since date
        if (request.containsHeader("If-Modified-Since")) {
            try {
                Date date = DateUtils.parseDate(request.getHeaders("If-Modified-Since")[0].getValue());
                if (date.compareTo(mServer.mLastModified) <= 0) {
                    // The file has not been modified
                    response.setStatusCode(HttpStatus.SC_NOT_MODIFIED);
                    return;
                }
            } catch (DateParseException e) {
                e.printStackTrace();
            }
        }

        // We determine if the asset is compressed
        try {
            AssetFileDescriptor afd = mAssetManager.openFd(location);

            // The asset is not compressed
            FileInputStream fis = new FileInputStream(afd.getFileDescriptor());
            fis.skip(afd.getStartOffset());
            body = new InputStreamEntity(fis, afd.getDeclaredLength());

            Log.d(TAG, "Serving uncompressed file " + "www" + url);

        } catch (FileNotFoundException e) {

            // The asset may be compressed
            // AAPT compresses assets so first we need to uncompress them to determine their length
            InputStream stream = mAssetManager.open(location, AssetManager.ACCESS_STREAMING);
            ByteArrayOutputStream buffer = new ByteArrayOutputStream(64000);
            byte[] tmp = new byte[4096];
            int length = 0;
            while ((length = stream.read(tmp)) != -1)
                buffer.write(tmp, 0, length);
            body = new InputStreamEntity(new ByteArrayInputStream(buffer.toByteArray()), buffer.size());
            stream.close();

            Log.d(TAG, "Serving compressed file " + "www" + url);

        }

        body.setContentType(getMimeMediaType(url) + "; charset=UTF-8");
        response.addHeader("Last-Modified", DateUtils.formatDate(mServer.mLastModified));

    } catch (IOException e) {
        // File does not exist
        response.setStatusCode(HttpStatus.SC_NOT_FOUND);
        body = new EntityTemplate(new ContentProducer() {
            public void writeTo(final OutputStream outstream) throws IOException {
                OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
                writer.write("<html><body><h1>");
                writer.write("File ");
                writer.write("www" + url);
                writer.write(" not found");
                writer.write("</h1></body></html>");
                writer.flush();
            }
        });
        Log.d(TAG, "File " + "www" + url + " not found");
        body.setContentType("text/html; charset=UTF-8");
    }

    response.setEntity(body);

}

From source file:at.bitfire.ical4android.TaskTest.java

private Task parseCalendar(String fname, Charset charset) throws IOException, InvalidCalendarException {
    fname = "tasks/" + fname;
    Log.d(TAG, "Loading task file " + fname);
    @Cleanup/*from  ww w . j a  va  2 s .  c  om*/
    InputStream is = assetMgr.open(fname, AssetManager.ACCESS_STREAMING);
    return Task.fromStream(is, charset)[0];
}

From source file:com.limitfan.gojuuon.utils.ScreenSlidePageFragment.java

public String getFromAssets(String fileName) {
    String result = "???";
    try {//from  ww w.  j  ava 2 s  .  c  o  m
        InputStream in = getResources().getAssets().open(fileName, AssetManager.ACCESS_STREAMING);
        //?
        int length = in.available();
        //byte
        byte[] buffer = new byte[length];
        //?byte
        in.read(buffer);
        //result = EncodingUtils.getString(buffer, ENCODING);
        //result = EncodingUtils.getString(buffer, ENCODING);
        result = new String(buffer, ENCODING);
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:at.bitfire.davdroid.webdav.WebDavResourceTest.java

public void testGet() throws Exception {
    WebDavResource simpleFile = new WebDavResource(davAssets, "test.random");
    simpleFile.get("*/*");
    @Cleanup/*w ww  .  j  av  a 2s .c o m*/
    InputStream is = assetMgr.open("test.random", AssetManager.ACCESS_STREAMING);
    byte[] expected = IOUtils.toByteArray(is);
    assertTrue(Arrays.equals(expected, simpleFile.getContent()));
}

From source file:playn.android.AndroidAssets.java

/**
 * Attempts to open the asset with the given name, throwing an {@link IOException} in case of
 * failure./*from w w w  . ja  v  a 2 s . c  o  m*/
 */
private InputStream openAsset(String path) throws IOException {
    String fullPath = normalizePath(pathPrefix + path);
    InputStream is = assetMgr.open(fullPath, AssetManager.ACCESS_STREAMING);
    if (is == null)
        throw new FileNotFoundException("Missing resource: " + fullPath);
    return is;
}

From source file:at.bitfire.ical4android.EventTest.java

private Event parseCalendar(String fname, Charset charset) throws IOException, InvalidCalendarException {
    fname = "events/" + fname;
    Log.d(TAG, "Loading event file " + fname);
    @Cleanup/*from ww w . j  a  va 2 s  .  com*/
    InputStream is = assetMgr.open(fname, AssetManager.ACCESS_STREAMING);
    return Event.fromStream(is, charset)[0];
}