001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.response;
018
019import org.apache.wicket.request.Response;
020import org.apache.wicket.util.string.AppendingStringBuffer;
021
022
023/**
024 * Response object that writes to an AppendingStringBuffer. This class is functionally equivalent to
025 * {@link StringResponse}, but defers creating the buffer until it is needed.
026 * 
027 * @author Thomas Heigl
028 * @deprecated In Wicket 10 {@link StringResponse} will be made lazy and this class will be removed
029 */
030@Deprecated(since = "9.13.0", forRemoval = true)
031public class LazyStringResponse extends Response
032{
033
034        private static final int DEFAULT_INITIAL_CAPACITY = 128;
035
036        /** Initial capacity of the buffer */
037        private final int initialCapacity;
038
039        /** Buffer to write to */
040        private AppendingStringBuffer out;
041
042        public LazyStringResponse()
043        {
044                this(DEFAULT_INITIAL_CAPACITY);
045        }
046
047        public LazyStringResponse(int initialCapacity)
048        {
049                this.initialCapacity = initialCapacity;
050        }
051
052        /**
053         * @see Response#write(CharSequence)
054         */
055        @Override
056        public void write(final CharSequence string)
057        {
058                if (out == null)
059                {
060                        out = new AppendingStringBuffer(initialCapacity);
061                }
062                out.append(string);
063        }
064
065        /**
066         * @see Response#reset()
067         */
068        @Override
069        public void reset()
070        {
071                if (out != null)
072                {
073                        out.clear();
074                }
075        }
076
077        /**
078         * @see Object#toString()
079         */
080        @Override
081        public String toString()
082        {
083                return getBuffer().toString();
084        }
085
086        /**
087         * @return The internal buffer as a {@link CharSequence} or an empty string if no content has
088         *         been written to the response
089         */
090        public CharSequence getBuffer()
091        {
092                return out != null ? out : "";
093        }
094
095        @Override
096        public void write(byte[] array)
097        {
098                throw new UnsupportedOperationException();
099        }
100
101        @Override
102        public void write(byte[] array, int offset, int length)
103        {
104                throw new UnsupportedOperationException();
105        }
106
107        @Override
108        public String encodeURL(CharSequence url)
109        {
110                return url != null ? url.toString() : null;
111        }
112
113        @Override
114        public Object getContainerResponse()
115        {
116                return null;
117        }
118}