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.util.io;
018
019import java.io.Writer;
020
021import org.apache.wicket.util.string.AppendingStringBuffer;
022
023
024/**
025 * @author jcompagner
026 */
027public class StringBufferWriter extends Writer
028{
029        /** The buffer which holds the chars */
030        private AppendingStringBuffer buffer;
031
032        /**
033         * Constructor
034         */
035        public StringBufferWriter()
036        {
037                buffer = new AppendingStringBuffer(4096);
038        }
039
040        /**
041         * @return The AppendingStringBuffer with the written data
042         */
043        public AppendingStringBuffer getStringBuffer()
044        {
045                return buffer;
046        }
047
048        /**
049         * @param buffer
050         */
051        public void setStringBuffer(final AppendingStringBuffer buffer)
052        {
053                this.buffer = buffer;
054        }
055
056        /**
057         * Writers the char to the buffer
058         * 
059         * @param ch
060         */
061        public void write(final char ch)
062        {
063                buffer.append(ch);
064        }
065
066        /**
067         * @see java.io.Writer#write(char[])
068         */
069        @Override
070        public void write(final char charArray[])
071        {
072                buffer.append(charArray);
073        }
074
075        /**
076         * @see java.io.Writer#write(char[], int, int)
077         */
078        @Override
079        public void write(final char charArray[], final int offset, final int length)
080        {
081                buffer.append(charArray, offset, length);
082        }
083
084        /**
085         * @see java.io.Writer#write(java.lang.String)
086         */
087        @Override
088        public void write(final String string)
089        {
090                buffer.append(string);
091        }
092
093        /**
094         * @see java.io.Writer#write(java.lang.String, int, int)
095         */
096        @Override
097        public void write(final String string, final int offset, final int length)
098        {
099                buffer.append(string.substring(offset, offset + length));
100        }
101
102        /**
103         * @see java.io.Writer#flush()
104         */
105        @Override
106        public void flush()
107        {
108        }
109
110        /**
111         * resets the buffer.
112         */
113        public void reset()
114        {
115                buffer.setLength(0);
116        }
117
118        /**
119         * @see java.io.Writer#close()
120         */
121        @Override
122        public void close()
123        {
124        }
125
126}