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: *
023: * Free SoftwareFoundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.vfs.i18n;
031:
032: import com.caucho.vfs.OutputStreamWithBuffer;
033:
034: import java.io.IOException;
035:
036: /**
037: * Implements the ISO-8859-1 EncodingWriter factory.
038: */
039: public final class ISO8859_1Writer extends EncodingWriter {
040: private final static ISO8859_1Writer _writer = new ISO8859_1Writer();
041:
042: /**
043: * Null-arg constructor for instantiation by com.caucho.vfs.Encoding only.
044: */
045: public ISO8859_1Writer() {
046: }
047:
048: /**
049: * Returns the Java encoding for the writer.
050: */
051: public String getJavaEncoding() {
052: return "ISO8859_1";
053: }
054:
055: /**
056: * Returns null, since WriteStream handles ISO-8859-1 directly.
057: *
058: * @return null for ISO-8859-1
059: */
060: public EncodingWriter create(String javaEncoding) {
061: return _writer;
062: }
063:
064: /**
065: * Returns null, since WriteStream handles ISO-8859-1 directly.
066: *
067: * @return null for ISO-8859-1
068: */
069: public EncodingWriter create() {
070: return _writer;
071: }
072:
073: /**
074: * Returns the writer.
075: */
076: public static EncodingWriter getStaticWriter() {
077: return _writer;
078: }
079:
080: /**
081: * Writes a character to the output stream with the correct encoding.
082: *
083: * @param ch the character to write.
084: */
085: public void write(OutputStreamWithBuffer os, char ch)
086: throws IOException {
087: os.write(ch);
088: }
089:
090: /**
091: * Writes a character buffer using the correct encoding.
092: *
093: * @param cbuf character buffer receiving the data.
094: * @param off starting offset into the buffer.
095: * @param len number of characters to write
096: */
097: @Override
098: public void write(OutputStreamWithBuffer os, char[] cBuf,
099: int cOffset, int cLength) throws IOException {
100: byte[] bBuf = os.getBuffer();
101: int bOffset = os.getBufferOffset();
102: int bEnd = bBuf.length;
103:
104: // int cEnd = cOffset + cLength;
105:
106: while (cLength > 0) {
107: int sublen = bEnd - bOffset;
108: if (cLength < sublen)
109: sublen = cLength;
110:
111: for (int i = 0; i < sublen; i++) {
112: bBuf[bOffset + i] = (byte) cBuf[cOffset + i];
113: }
114:
115: bOffset += sublen;
116: cOffset += sublen;
117: cLength -= sublen;
118:
119: if (bOffset == bEnd) {
120: bBuf = os.nextBuffer(bOffset);
121: bOffset = os.getBufferOffset();
122: bEnd = bBuf.length;
123: }
124: }
125:
126: os.setBufferOffset(bOffset);
127: }
128: }
|