summaryrefslogtreecommitdiffstats
path: root/src/dom/io/gzipstream.cpp
diff options
context:
space:
mode:
authorBob Jamison <ishmalius@gmail.com>2006-04-12 13:25:21 +0000
committerishmal <ishmal@users.sourceforge.net>2006-04-12 13:25:21 +0000
commit6fd9af13a8614a4b95e8be1518e745915ef8bb56 (patch)
treebf94fa5f7c3a7b2f581f3e7cf2522bf7c2d4b6c9 /src/dom/io/gzipstream.cpp
parentRemoved file/folder for ishmal (diff)
downloadinkscape-6fd9af13a8614a4b95e8be1518e745915ef8bb56.tar.gz
inkscape-6fd9af13a8614a4b95e8be1518e745915ef8bb56.zip
Add new rearranged /dom directory
(bzr r479)
Diffstat (limited to 'src/dom/io/gzipstream.cpp')
-rw-r--r--src/dom/io/gzipstream.cpp245
1 files changed, 245 insertions, 0 deletions
diff --git a/src/dom/io/gzipstream.cpp b/src/dom/io/gzipstream.cpp
new file mode 100644
index 000000000..628adaa7a
--- /dev/null
+++ b/src/dom/io/gzipstream.cpp
@@ -0,0 +1,245 @@
+/**
+ * Zlib-enabled input and output streams
+ *
+ * This is a thin wrapper of libz calls, in order
+ * to provide a simple interface to our developers
+ * for gzip input and output.
+ *
+ * Authors:
+ * Bob Jamison
+ *
+ * Copyright (C) 2006 Bob Jamison
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+
+
+#include "gzipstream.h"
+
+#include "util/ziptool.h"
+
+
+namespace org
+{
+namespace w3c
+{
+namespace dom
+{
+namespace io
+{
+
+
+//#########################################################################
+//# G Z I P I N P U T S T R E A M
+//#########################################################################
+
+/**
+ *
+ */
+GzipInputStream::GzipInputStream(InputStream &sourceStream)
+ : BasicInputStream(sourceStream)
+{
+ loaded = false;
+ bufPos = 0;
+}
+
+/**
+ *
+ */
+GzipInputStream::~GzipInputStream()
+{
+ close();
+}
+
+/**
+ * Returns the number of bytes that can be read (or skipped over) from
+ * this input stream without blocking by the next caller of a method for
+ * this input stream.
+ */
+int GzipInputStream::available()
+{
+ if (closed)
+ return 0;
+ if (!loaded)
+ if (!load())
+ return 0;
+ return (int) buffer.size();
+}
+
+
+/**
+ * Closes this input stream and releases any system resources
+ * associated with the stream.
+ */
+void GzipInputStream::close()
+{
+ if (closed)
+ return;
+
+ closed = true;
+}
+
+/**
+ * Reads the next byte of data from the input stream. -1 if EOF
+ */
+int GzipInputStream::get()
+{
+ if (closed)
+ return -1;
+
+ if (!loaded)
+ if (!load())
+ return -1;
+
+ if (bufPos >= buffer.size())
+ return -1;
+ int ch = (int) buffer[bufPos++];
+
+ return ch;
+}
+
+/**
+ * Processes input. Fills read buffer.
+ */
+bool GzipInputStream::load()
+{
+ if (closed)
+ return false;
+
+ if (loaded)
+ return true;
+
+ std::vector<unsigned char> compBuf;
+ while (true)
+ {
+ int ch = source.get();
+ if (ch < 0)
+ break;
+ compBuf.push_back(ch);
+ }
+ GzipFile gz;
+ if (!gz.readBuffer(compBuf))
+ {
+ return -1;
+ }
+ buffer = gz.getData();
+ bufPos = 0;
+ loaded = true;
+ return true;
+}
+
+
+
+
+
+
+//#########################################################################
+//# G Z I P O U T P U T S T R E A M
+//#########################################################################
+
+/**
+ *
+ */
+GzipOutputStream::GzipOutputStream(OutputStream &destinationStream)
+ : BasicOutputStream(destinationStream)
+{
+
+ closed = false;
+}
+
+/**
+ *
+ */
+GzipOutputStream::~GzipOutputStream()
+{
+ close();
+}
+
+/**
+ * Closes this output stream and releases any system resources
+ * associated with this stream.
+ */
+void GzipOutputStream::close()
+{
+ if (closed)
+ return;
+
+ flush();
+
+ closed = true;
+}
+
+/**
+ * Flushes this output stream and forces any buffered output
+ * bytes to be written out.
+ */
+void GzipOutputStream::flush()
+{
+ if (closed || buffer.size()<1)
+ return;
+
+ std::vector<unsigned char> compBuf;
+ GzipFile gz;
+
+ gz.writeBuffer(buffer);
+
+ std::vector<unsigned char>::iterator iter;
+ for (iter=compBuf.begin() ; iter!=compBuf.end() ; iter++)
+ {
+ int ch = (int) *iter;
+ destination.put(ch);
+ }
+
+ buffer.clear();
+
+ //printf("done\n");
+
+}
+
+
+
+/**
+ * Writes the specified byte to this output stream.
+ */
+void GzipOutputStream::put(int ch)
+{
+ if (closed)
+ {
+ //probably throw an exception here
+ return;
+ }
+
+ //Add char to buffer
+ buffer.push_back(ch);
+
+}
+
+
+
+} // namespace io
+} // namespace dom
+} // namespace w3c
+} // namespace org
+
+
+
+
+//#########################################################################
+//# E N D O F F I L E
+//#########################################################################
+
+
+