--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.iapi.services.io.AccessibleByteArrayOutputStream\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ 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, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.iapi.services.io;\r
+\r
+import java.io.ByteArrayOutputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+\r
+/**\r
+ * This allows us to get to the byte array to go back and\r
+ * edit contents or get the array without having a copy made.\r
+ <P>\r
+ Since a copy is not made, users must be careful that no more\r
+ writes are made to the stream if the array reference is handed off.\r
+ * <p>\r
+ * Users of this must make the modifications *before* the\r
+ * next write is done, and then release their hold on the\r
+ * array.\r
+ \r
+ */\r
+public class AccessibleByteArrayOutputStream extends ByteArrayOutputStream {\r
+\r
+ public AccessibleByteArrayOutputStream() {\r
+ super();\r
+ }\r
+\r
+ public AccessibleByteArrayOutputStream(int size) {\r
+ super(size);\r
+ }\r
+\r
+ /**\r
+ * The caller promises to set their variable to null\r
+ * before any other calls to write to this stream are made.\r
+ Or promises to throw away references to the stream before\r
+ passing the array reference out of its control.\r
+ */\r
+ public byte[] getInternalByteArray() {\r
+ return buf;\r
+ }\r
+ \r
+ /**\r
+ * Read the complete contents of the passed input stream\r
+ * into this byte array.\r
+ * @throws IOException \r
+ */\r
+ public void readFrom(InputStream in) throws IOException\r
+ {\r
+ byte[] buffer = new byte[8192];\r
+ \r
+ for(;;)\r
+ {\r
+ int read = in.read(buffer, 0, buf.length);\r
+ if (read == -1)\r
+ break;\r
+ write(buffer, 0, read);\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * Return an InputStream that wraps the valid byte array.\r
+ * Note that no copy is made of the byte array from the\r
+ * input stream, it is up to the caller to ensure the correct\r
+ * co-ordination.\r
+ */\r
+ public InputStream getInputStream()\r
+ {\r
+ return new NewByteArrayInputStream(buf, 0, count);\r
+ }\r
+ \r
+ /**\r
+ * Copy an InputStream into an array of bytes and return\r
+ * an InputStream against those bytes. The input stream\r
+ * is copied until EOF is returned. This is useful to provide\r
+ * streams to applications in order to isolate them from\r
+ * Derby's internals.\r
+ * \r
+ * @param in InputStream to be copied\r
+ * @param bufferSize Initial size of the byte array\r
+ * \r
+ * @return InputStream against the raw data.\r
+ * \r
+ * @throws IOException Error reading the stream\r
+ */\r
+ public static InputStream copyStream(InputStream in, int bufferSize)\r
+ throws IOException\r
+ {\r
+ AccessibleByteArrayOutputStream raw =\r
+ new AccessibleByteArrayOutputStream(bufferSize);\r
+ raw.readFrom(in);\r
+ return raw.getInputStream();\r
+ }\r
+}\r