             }
             stream.write("\n")
         }
-        
+    }
+
+    public func serialize<T: TextOutputStream, S: Sequence>(_ triples: S, to stream: inout T) throws where S.Element == Triple {
+        return try serialize(triples, to: &stream, emitHeader: true)
+    }
+    
+    public func serialize<T: TextOutputStream, S: Sequence>(_ triples: S, to stream: inout T, emitHeader: Bool) throws where S.Element == Triple {
+        if emitHeader {
+            serializeHeader(to: &stream)
+        }
         let subjects = Dictionary(grouping: triples) { $0.subject }
         for (i, s) in subjects.keys.sorted().enumerated() {
             let triples = subjects[s]!


open class TurtleSerializer : RDFSerializer {
    public var canonicalMediaType = "application/turtle"
    var prefixes: [String:Term]
    
    public init(prefixes: [String:Term]? = nil) {
        self.prefixes = prefixes ?? [:] // TODO: implement prefix use

        data.append(contentsOf: [0x2e, 0x0a]) // dot newline
    }
    
    public func serializeHeader<T: TextOutputStream>(to stream: inout T) {
        if prefixes.count > 0 {
            for (k, t) in prefixes {
                stream.write("@prefix \(k): ")
