Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / derby-10.3.2.1 / java / engine / org / apache / derby / impl / jdbc / ClobAsciiStream.java
diff --git a/JMCR-Stable/real-world application/derby-10.3.2.1/java/engine/org/apache/derby/impl/jdbc/ClobAsciiStream.java b/JMCR-Stable/real-world application/derby-10.3.2.1/java/engine/org/apache/derby/impl/jdbc/ClobAsciiStream.java
new file mode 100644 (file)
index 0000000..9c0ee9f
--- /dev/null
@@ -0,0 +1,104 @@
+/* \r
+\r
+   Derby - Class org.apache.derby.impl.jdbc.ClobAsciiStream\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.OutputStream;\r
+import java.io.Writer;\r
+\r
+/**\r
+ * Wrap a Writer as an OutputStream to support Clob.setAsciiStream().\r
+ * Any value written to the OutputStream is a valid ASCII value\r
+ * (0-255 from JDBC 4 spec appendix C2) thus this class simply\r
+ * passes the written values onto the Writer.\r
+ *\r
+ */\r
+final class ClobAsciiStream extends OutputStream {\r
+\r
+    private final Writer writer;\r
+    private final char[] buffer = new char[1024];\r
+    \r
+    ClobAsciiStream (Writer writer){\r
+        this.writer = writer;\r
+    }\r
+\r
+    /**\r
+     * Writes the specified byte to this output stream.\r
+     * <p>\r
+     * The general contract for <code>write</code> is that one byte is written\r
+     * to the output stream. The byte to be written is the eight low-order bits\r
+     * of the argument <code>b</code>. The 24 high-order bits of <code>b</code>\r
+     * are ignored.\r
+     * \r
+     * @param b   the <code>byte</code>.\r
+     * @exception IOException  if an I/O error occurs. In particular, \r
+     *             an <code>IOException</code> may be thrown if the \r
+     *             output stream has been closed.\r
+     */\r
+    public void write(int b) throws IOException {\r
+        writer.write(b & 0xff);\r
+    }\r
+\r
+    /**\r
+     * Writes <code>len</code> bytes from the specified byte array \r
+     * starting at offset <code>off</code> to this output stream. \r
+     * <p>\r
+     * The general contract for <code>write(b, off, len)</code> is that \r
+     * some of the bytes in the array <code>b</code> are written to the \r
+     * output stream in order; element <code>b[off]</code> is the first \r
+     * byte written and <code>b[off+len-1]</code> is the last byte written \r
+     * by this operation.\r
+     * <p>\r
+     * The <code>write</code> method of <code>OutputStream</code> calls \r
+     * the write method of one argument on each of the bytes to be \r
+     * written out. Subclasses are encouraged to override this method and \r
+     * provide a more efficient implementation. \r
+     * <p>\r
+     * If <code>b</code> is <code>null</code>, a \r
+     * <code>NullPointerException</code> is thrown.\r
+     * <p>\r
+     * If <code>off</code> is negative, or <code>len</code> is negative, or \r
+     * <code>off+len</code> is greater than the length of the array \r
+     * <code>b</code>, then an <tt>IndexOutOfBoundsException</tt> is thrown.\r
+     * \r
+     * @param b     the data.\r
+     * @param off   the start offset in the data.\r
+     * @param len   the number of bytes to write.\r
+     * @exception IOException  if an I/O error occurs. In particular, \r
+     *             an <code>IOException</code> is thrown if the output \r
+     *             stream is closed.\r
+     */\r
+    public void write(byte[] b, int off, int len) throws IOException {\r
+        \r
+        while (len > 0)\r
+        {\r
+            int clen = Math.min(len, buffer.length);\r
+            for (int i = 0; i < clen; i++) {\r
+                buffer[i] = (char)(b[off + i] & 0xff);\r
+            }\r
+            writer.write(buffer, 0, clen);\r
+            off += clen;\r
+            len -= clen;\r
+        }\r
+    }    \r
+}\r