[geary/wip/726281-text-attachment-crlf: 4/13] Move MidstreamConverter to same source file as other stream symbols.



commit 6f7cff219c53981b367624c1894b3ac7751f6370
Author: Michael James Gratton <mike vee net>
Date:   Sat May 5 11:49:26 2018 +1000

    Move MidstreamConverter to same source file as other stream symbols.

 po/POTFILES.in                      |    1 -
 src/CMakeLists.txt                  |    1 -
 src/engine/meson.build              |    1 -
 src/engine/util/util-converter.vala |   77 --------------------
 src/engine/util/util-stream.vala    |  134 +++++++++++++++++++++++++++--------
 5 files changed, 103 insertions(+), 111 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index feeb76e..02f5e59 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -375,7 +375,6 @@ src/engine/state/state-machine.vala
 src/engine/state/state-mapping.vala
 src/engine/util/util-ascii.vala
 src/engine/util/util-collection.vala
-src/engine/util/util-converter.vala
 src/engine/util/util-files.vala
 src/engine/util/util-generic-capabilities.vala
 src/engine/util/util-html.vala
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 91c3d7d..4404bde 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -299,7 +299,6 @@ engine/state/state-mapping.vala
 engine/util/util-ascii.vala
 engine/util/util-collection.vala
 engine/util/util-connectivity-manager.vala
-engine/util/util-converter.vala
 engine/util/util-files.vala
 engine/util/util-generic-capabilities.vala
 engine/util/util-html.vala
diff --git a/src/engine/meson.build b/src/engine/meson.build
index fa6cbba..717ea20 100644
--- a/src/engine/meson.build
+++ b/src/engine/meson.build
@@ -296,7 +296,6 @@ geary_engine_vala_sources = files(
   'util/util-ascii.vala',
   'util/util-collection.vala',
   'util/util-connectivity-manager.vala',
-  'util/util-converter.vala',
   'util/util-files.vala',
   'util/util-generic-capabilities.vala',
   'util/util-html.vala',
diff --git a/src/engine/util/util-stream.vala b/src/engine/util/util-stream.vala
index ba90c3f..3ac1839 100644
--- a/src/engine/util/util-stream.vala
+++ b/src/engine/util/util-stream.vala
@@ -6,40 +6,112 @@
 
 namespace Geary.Stream {
 
-/**
- * Provides an asynchronous version of OutputStream.write_all().
- */
-public async void write_all_async(OutputStream outs, Memory.Buffer buffer, Cancellable? cancellable)
+    /**
+     * Provides an asynchronous version of OutputStream.write_all().
+     */
+    public async void write_all_async(OutputStream outs, Memory.Buffer buffer, Cancellable? cancellable)
     throws Error {
-    if (buffer.size == 0)
-        return;
-    
-    // use an unowned bytes buffer whenever possible
-    Bytes? bytes = null;
-    unowned uint8[] data;
-    Memory.UnownedBytesBuffer? unowned_bytes = buffer as Memory.UnownedBytesBuffer;
-    if (unowned_bytes != null) {
-        data = unowned_bytes.to_unowned_uint8_array();
-    } else {
-        // hold the reference to the Bytes object until finished
-        bytes = buffer.get_bytes();
-        data = bytes.get_data();
+        if (buffer.size == 0)
+            return;
+
+        // use an unowned bytes buffer whenever possible
+        Bytes? bytes = null;
+        unowned uint8[] data;
+        Memory.UnownedBytesBuffer? unowned_bytes = buffer as Memory.UnownedBytesBuffer;
+        if (unowned_bytes != null) {
+            data = unowned_bytes.to_unowned_uint8_array();
+        } else {
+            // hold the reference to the Bytes object until finished
+            bytes = buffer.get_bytes();
+            data = bytes.get_data();
+        }
+
+        ssize_t offset = 0;
+        do {
+            offset += yield outs.write_async(data[offset:data.length], Priority.DEFAULT, cancellable);
+        } while (offset < data.length);
     }
-    
-    ssize_t offset = 0;
-    do {
-        offset += yield outs.write_async(data[offset:data.length], Priority.DEFAULT, cancellable);
-    } while (offset < data.length);
-}
 
-/**
- * Asynchronously writes the entire string to the OutputStream.
- */
-public async void write_string_async(OutputStream outs, string? str, Cancellable? cancellable)
+    /**
+     * Asynchronously writes the entire string to the OutputStream.
+     */
+    public async void write_string_async(OutputStream outs, string? str, Cancellable? cancellable)
     throws Error {
-    if (!String.is_empty(str))
-        yield write_all_async(outs, new Memory.StringBuffer(str), cancellable);
-}
+        if (!String.is_empty(str))
+            yield write_all_async(outs, new Memory.StringBuffer(str), cancellable);
+    }
 
-}
 
+    public class MidstreamConverter : BaseObject, Converter {
+        public uint64 total_bytes_read { get; private set; default = 0; }
+        public uint64 total_bytes_written { get; private set; default = 0; }
+        public uint64 converted_bytes_read { get; private set; default = 0; }
+        public uint64 converted_bytes_written { get; private set; default = 0; }
+
+        public bool log_performance { get; set; default = false; }
+
+        private string name;
+        private Converter? converter = null;
+
+        public MidstreamConverter(string name) {
+            this.name = name;
+        }
+
+        public bool install(Converter converter) {
+            if (this.converter != null)
+                return false;
+
+            this.converter = converter;
+
+            return true;
+        }
+
+        public ConverterResult convert(uint8[] inbuf, uint8[] outbuf, ConverterFlags flags,
+                                       out size_t bytes_read, out size_t bytes_written) throws Error {
+            if (converter != null) {
+                ConverterResult result = converter.convert(inbuf, outbuf, flags, out bytes_read, out 
bytes_written);
+
+                total_bytes_read += bytes_read;
+                total_bytes_written += bytes_written;
+
+                converted_bytes_read += bytes_read;
+                converted_bytes_written += bytes_written;
+
+                if (log_performance && (bytes_read > 0 || bytes_written > 0)) {
+                    double pct = (converted_bytes_read > converted_bytes_written)
+                    ? (double) converted_bytes_written / (double) converted_bytes_read
+                    : (double) converted_bytes_read / (double) converted_bytes_written;
+                    debug("%s read/written: %s/%s (%ld%%)", name, converted_bytes_read.to_string(),
+                          converted_bytes_written.to_string(), (long) (pct * 100.0));
+                }
+
+                return result;
+            }
+
+            // passthrough
+            size_t copied = size_t.min(inbuf.length, outbuf.length);
+            if (copied > 0)
+                GLib.Memory.copy(outbuf, inbuf, copied);
+
+            bytes_read = copied;
+            bytes_written = copied;
+
+            total_bytes_read += copied;
+            total_bytes_written += copied;
+
+            if ((flags & ConverterFlags.FLUSH) != 0)
+                return ConverterResult.FLUSHED;
+
+            if ((flags & ConverterFlags.INPUT_AT_END) != 0)
+                return ConverterResult.FINISHED;
+
+            return ConverterResult.CONVERTED;
+        }
+
+        public void reset() {
+            if (converter != null)
+                converter.reset();
+        }
+    }
+
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]