001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.servlet;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
018    import com.liferay.portal.kernel.io.unsync.UnsyncPrintWriter;
019    
020    import java.io.PrintWriter;
021    
022    import java.nio.ByteBuffer;
023    
024    import javax.servlet.ServletOutputStream;
025    import javax.servlet.http.HttpServletResponse;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public class ByteBufferServletResponse extends HeaderCacheServletResponse {
031    
032            public ByteBufferServletResponse(HttpServletResponse response) {
033                    super(response);
034            }
035    
036            public void flushBuffer() {
037            }
038    
039            public int getBufferSize() {
040                    if (_byteBuffer != null) {
041                            return _byteBuffer.remaining();
042                    }
043                    else if (_unsyncByteArrayOutputStream != null) {
044                            return _unsyncByteArrayOutputStream.size();
045                    }
046                    else {
047                            return 0;
048                    }
049            }
050    
051            public ByteBuffer getByteBuffer() {
052                    if (_byteBuffer != null) {
053                            return _byteBuffer;
054                    }
055                    else if (_unsyncByteArrayOutputStream != null) {
056                            return _unsyncByteArrayOutputStream.unsafeGetByteBuffer();
057                    }
058                    else {
059                            return ByteBuffer.wrap(new byte[0]);
060                    }
061            }
062    
063            public ServletOutputStream getOutputStream() {
064                    if (_printWriter != null) {
065                            throw new IllegalStateException(
066                                    "Cannot obtain OutputStream because Writer is already in use");
067                    }
068    
069                    if (_servletOutputStream == null) {
070                            _unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();
071                            _servletOutputStream = new PipingServletOutputStream(
072                                    _unsyncByteArrayOutputStream);
073                    }
074    
075                    return _servletOutputStream;
076            }
077    
078            public PrintWriter getWriter() {
079                    if (_printWriter != null) {
080                            return _printWriter;
081                    }
082    
083                    if (_servletOutputStream != null) {
084                            throw new IllegalStateException(
085                                    "Cannot obtain Writer because OutputStream is already in use");
086                    }
087    
088                    _unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();
089                    _servletOutputStream = new PipingServletOutputStream(
090                            _unsyncByteArrayOutputStream);
091                    _printWriter = new UnsyncPrintWriter(_unsyncByteArrayOutputStream);
092    
093                    return _printWriter;
094            }
095    
096            public void setByteBuffer(ByteBuffer byteBuffer) {
097                    _byteBuffer = byteBuffer;
098            }
099    
100            private ByteBuffer _byteBuffer;
101            private PrintWriter _printWriter;
102            private ServletOutputStream _servletOutputStream;
103            private UnsyncByteArrayOutputStream _unsyncByteArrayOutputStream;
104    
105    }