001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.pdf.encoding;
031:
032: import java.io.IOException;
033: import java.io.OutputStream;
034: import java.io.Writer;
035:
036: /**
037: * A writer that uses a PDF style encoding to map unicode to byte code.
038: */
039: public class MappedWriter extends Writer {
040: /** the encoding we use to encode the characters */
041: private Encoding encoding;
042:
043: /** the byte stream we write on */
044: private OutputStream outStream;
045:
046: /**
047: * Create a MappedWriter
048: *
049: * @param out
050: * The underlying output byte stream.
051: * @param encoding
052: * The encoding to use.
053: */
054: public MappedWriter(OutputStream out, Encoding encoding) {
055: super (out);
056: setOutStream(out);
057: setEncoding(encoding);
058: }
059:
060: /**
061: * The encoding used by this writer.
062: *
063: * @return The encoding used by this writer.
064: */
065: public Encoding getEncoding() {
066: return encoding;
067: }
068:
069: /**
070: * @see java.io.Writer#close()
071: */
072: public void close() throws IOException {
073: synchronized (lock) {
074: if (outStream == null) {
075: return;
076: }
077: flush();
078: outStream.close();
079: outStream = null;
080: }
081: }
082:
083: /*
084: * (non-Javadoc)
085: *
086: * @see java.io.Writer#flush()
087: */
088: public void flush() throws IOException {
089: synchronized (lock) {
090: outStream.flush();
091: }
092: }
093:
094: /*
095: * (non-Javadoc)
096: *
097: * @see java.io.Writer#write(char[], int, int)
098: */
099: public void write(char[] cbuf, int off, int len) throws IOException {
100: synchronized (lock) {
101: ensureOpen();
102: if ((off < 0) || (off > cbuf.length) || (len < 0)
103: || ((off + len) > cbuf.length) || ((off + len) < 0)) {
104: throw new IndexOutOfBoundsException();
105: } else if (len == 0) {
106: return;
107: }
108:
109: for (int i = off; i < (off + len); i++) {
110: outStream
111: .write(getEncoding().getValidByteCode(cbuf[i]));
112: }
113: }
114: }
115:
116: /**
117: * The underlying output stream.
118: *
119: * @return The underlying output stream.
120: */
121: protected java.io.OutputStream getOutStream() {
122: return outStream;
123: }
124:
125: /**
126: * Set the encoding to be used by this writer.
127: *
128: * @param encoding
129: * THe new encoding to be used.
130: */
131: private void setEncoding(Encoding encoding) {
132: this .encoding = encoding;
133: }
134:
135: private void setOutStream(java.io.OutputStream newOutStream) {
136: outStream = newOutStream;
137: }
138:
139: /**
140: * Check to make sure that the stream has not been closed
141: *
142: * @throws IOException
143: * if the outStream is null.
144: */
145: private void ensureOpen() throws IOException {
146: if (outStream == null) {
147: throw new IOException("Stream closed");
148: }
149: }
150: }
|