001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.vfs.i18n;
030:
031: import com.caucho.vfs.OutputStreamWithBuffer;
032:
033: import java.io.IOException;
034: import java.util.logging.Logger;
035:
036: /**
037: * Abstract class for a character-to-byte encoding writer.
038: *
039: * <p/>Implementations need to implement <code>create</code>
040: * and <code>write()</code> at minimum. Efficient implementations will
041: * also implement the <code>write</code> into a buffer.
042: *
043: * <p/>Implementations should not buffer the bytes.
044: */
045: abstract public class EncodingWriter {
046: protected static final Logger log = Logger
047: .getLogger(EncodingWriter.class.getName());
048:
049: /**
050: * Returns the Java encoding for the writer.
051: */
052: public String getJavaEncoding() {
053: return "unknown";
054: }
055:
056: /**
057: * Sets the Java encoding for the writer.
058: */
059: public void setJavaEncoding(String encoding) {
060: }
061:
062: /**
063: * Returns a new encoding writer for the given stream and javaEncoding.
064: *
065: * @param javaEncoding the JDK name for the encoding.
066: *
067: * @return the encoding writer
068: */
069: public abstract EncodingWriter create(String javaEncoding);
070:
071: /**
072: * Returns a new encoding writer using the saved writer.
073: *
074: * @return the encoding writer
075: */
076: public EncodingWriter create() {
077: return create(getJavaEncoding());
078: }
079:
080: /**
081: * Writes the next character using the correct encoding.
082: *
083: * @param ch the character to write
084: */
085: public abstract void write(OutputStreamWithBuffer os, char ch)
086: throws IOException;
087:
088: /**
089: * Writes a character buffer using the correct encoding.
090: *
091: * @param cbuf character buffer receiving the data.
092: * @param off starting offset into the buffer.
093: * @param len number of characters to write
094: */
095: public void write(OutputStreamWithBuffer os, char[] cbuf, int off,
096: int len) throws IOException {
097: for (int i = 0; i < len; i++)
098: write(os, cbuf[off + i]);
099: }
100: }
|