Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / derby-10.3.2.1 / java / engine / org / apache / derby / impl / jdbc / ClobUtf8Writer.java
diff --git a/JMCR-Stable/real-world application/derby-10.3.2.1/java/engine/org/apache/derby/impl/jdbc/ClobUtf8Writer.java b/JMCR-Stable/real-world application/derby-10.3.2.1/java/engine/org/apache/derby/impl/jdbc/ClobUtf8Writer.java
new file mode 100644 (file)
index 0000000..a88ef54
--- /dev/null
@@ -0,0 +1,112 @@
+/* \r
+\r
+   Derby - Class org.apache.derby.impl.jdbc.ClobUtf8Writer\r
+\r
+   Licensed to the Apache Software Foundation (ASF) under one\r
+   or more contributor license agreements.  See the NOTICE file\r
+   distributed with this work for additional information\r
+   regarding copyright ownership.  The ASF licenses this file\r
+   to you under the Apache License, Version 2.0 (the\r
+   "License"); you may not use this file except in compliance\r
+   with the License.  You may obtain a copy of the License at\r
+\r
+     http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing,\r
+   software distributed under the License is distributed on an\r
+   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\r
+   KIND, either express or implied.  See the License for the\r
+   specific language governing permissions and limitations\r
+   under the License.\r
+\r
+ */\r
+package org.apache.derby.impl.jdbc;\r
+\r
+import java.io.IOException;\r
+import java.io.InputStreamReader;\r
+import java.io.OutputStream;\r
+import java.io.Writer;\r
+import java.sql.SQLException;\r
+import org.apache.derby.iapi.error.StandardException;\r
+import org.apache.derby.iapi.reference.SQLState;\r
+import org.apache.derby.iapi.services.i18n.MessageService;\r
+\r
+/**\r
+ * Writer implementation for <code>Clob</code>.\r
+ */\r
+final class ClobUtf8Writer extends Writer {\r
+    private TemporaryClob control;    \r
+    private long pos; // Position in characters.\r
+    private boolean closed;\r
+    \r
+    /**\r
+     * Constructor.\r
+     *\r
+     * @param control worker object for the CLOB value\r
+     * @param pos initial <b>byte</b> position in the CLOB value\r
+     */\r
+    ClobUtf8Writer(TemporaryClob control, long pos) {\r
+        this.control = control;\r
+        this.pos = pos;\r
+        closed = false;\r
+    }    \r
+\r
+    /**\r
+     * Flushes the stream.\r
+     * <p>\r
+     * Flushing the stream after {@link #close} has been called will cause an\r
+     * exception to be thrown.\r
+     * <p>\r
+     * <i>Implementation note:</i> In the current implementation, this is a\r
+     * no-op. Flushing is left to the underlying stream(s). Note that when\r
+     * programming against/with this class, always follow good practice and call\r
+     * <code>flush</code>.\r
+     *\r
+     * @throws IOException if the stream has been closed\r
+     */\r
+    public void flush() throws IOException {\r
+        if (closed)\r
+            throw new IOException (\r
+                MessageService.getTextMessage (SQLState.LANG_STREAM_CLOSED));\r
+        // A no-op.\r
+        // Flushing is currently the responsibility of the underlying stream(s).\r
+    }\r
+\r
+    /**\r
+     * Closes the stream.\r
+     * <p>\r
+     * Once the stream has been closed, further <code>write</code> or \r
+     * {@link #flush} invocations will cause an <code>IOException</code> to be\r
+     * thrown. Closing a previously closed stream has no effect.\r
+     */\r
+    public void close() {\r
+        closed = true;\r
+    }\r
+\r
+    /**\r
+     * Writes a portion of an array of characters to the CLOB value.\r
+     * \r
+     * @param cbuf array of characters\r
+     * @param off offset into <code>cbuf</code> from which to start writing\r
+     *      characters\r
+     * @param len number of characters to write\r
+     * @throws IOException if an I/O error occurs\r
+     */\r
+    public void write(char[] cbuf, int off, int len) throws IOException {\r
+        if (closed)\r
+            throw new IOException (\r
+                MessageService.getTextMessage (SQLState.LANG_STREAM_CLOSED));\r
+        try {\r
+            long ret = control.insertString (String.copyValueOf (\r
+                                                    cbuf, off, len), \r
+                                              pos);\r
+            if (ret > 0)\r
+                pos += ret;\r
+        }\r
+        catch (SQLException e) {\r
+            IOException ioe = new IOException (e.getMessage());\r
+            ioe.initCause (e);\r
+            throw ioe;\r
+        }\r
+    }\r
+}\r