Example usage for java.io FileInputStream markSupported

List of usage examples for java.io FileInputStream markSupported

Introduction

In this page you can find the example usage for java.io FileInputStream markSupported.

Prototype

public boolean markSupported() 

Source Link

Document

Tests if this input stream supports the mark and reset methods.

Usage

From source file:org.bndtools.rt.repository.server.RepositoryResourceComponent.java

@GET
@Path("index")
@Produces(MediaType.APPLICATION_XML)//ww  w  .  j  a  v a 2  s.c om
public Response getIndex(@Context HttpHeaders headers) throws Exception {
    List<URI> locations = repo.getIndexLocations();
    if (locations.isEmpty())
        return Response.serverError().entity("No index available").build();

    Response response;
    URI location = locations.get(0);
    if ("file".equals(location.getScheme())) {
        File file = new File(location);

        @SuppressWarnings("resource")
        FileInputStream indexStream = new FileInputStream(file);
        InputStream bufferedStream = indexStream.markSupported() ? indexStream
                : new BufferedInputStream(indexStream);

        InputStream responseStream;
        if (isGZip(bufferedStream)) {
            responseStream = new GZIPInputStream(indexStream);
        } else {
            responseStream = bufferedStream;
        }
        response = Response.ok(responseStream, MediaType.APPLICATION_XML_TYPE).build();
    } else {
        response = Response.status(Status.SEE_OTHER).location(location).build();
    }
    return response;
}

From source file:jp.aegif.alfresco.online_webdav.WebDAVMethod.java

/**
 * Set the request/response details/*from   w  ww. ja  v  a 2  s  .  co m*/
 * 
 * @param req
 *            HttpServletRequest
 * @param resp
 *            HttpServletResponse
 * @param registry
 *            ServiceRegistry
 * @param rootNode
 *            NodeRef
 */
public void setDetails(final HttpServletRequest req, HttpServletResponse resp, WebDAVHelper davHelper,
        NodeRef rootNode) {
    // Wrap the request so that it is 'retryable'. Calls to getInputStream() and getReader() will result in the
    // request body being read into an intermediate file.
    this.m_request = new HttpServletRequestWrapper(req) {

        @Override
        public ServletInputStream getInputStream() throws IOException {
            if (WebDAVMethod.this.m_reader != null) {
                throw new IllegalStateException("Reader in use");
            }
            if (WebDAVMethod.this.m_inputStream == null) {
                final FileInputStream in = new FileInputStream(getRequestBodyAsFile(req));
                WebDAVMethod.this.m_inputStream = new ServletInputStream() {

                    @Override
                    public int read() throws IOException {
                        return in.read();
                    }

                    @Override
                    public int read(byte b[]) throws IOException {
                        return in.read(b);
                    }

                    @Override
                    public int read(byte b[], int off, int len) throws IOException {
                        return in.read(b, off, len);
                    }

                    @Override
                    public long skip(long n) throws IOException {
                        return in.skip(n);
                    }

                    @Override
                    public int available() throws IOException {
                        return in.available();
                    }

                    @Override
                    public void close() throws IOException {
                        in.close();
                    }

                    @Override
                    public void mark(int readlimit) {
                        in.mark(readlimit);
                    }

                    @Override
                    public void reset() throws IOException {
                        in.reset();
                    }

                    @Override
                    public boolean markSupported() {
                        return in.markSupported();
                    }
                };
            }

            return WebDAVMethod.this.m_inputStream;
        }

        @Override
        public BufferedReader getReader() throws IOException {
            if (WebDAVMethod.this.m_inputStream != null) {
                throw new IllegalStateException("Input Stream in use");
            }
            if (WebDAVMethod.this.m_reader == null) {
                String encoding = req.getCharacterEncoding();
                WebDAVMethod.this.m_reader = new BufferedReader(
                        new InputStreamReader(new FileInputStream(getRequestBodyAsFile(req)),
                                encoding == null ? "ISO-8859-1" : encoding));
            }

            return WebDAVMethod.this.m_reader;
        }

    };
    this.m_response = resp;
    this.m_davHelper = davHelper;
    this.m_rootNodeRef = rootNode;

    this.m_strPath = m_davHelper.getRepositoryPath(m_request);
}