package org.marc4j;

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

import org.marc4j.converter.CharConverter;
import org.marc4j.marc.ControlField;
import org.marc4j.marc.DataField;
import org.marc4j.marc.Record;
import org.marc4j.marc.Subfield;
import org.marc4j.marc.VariableField;

public class MarcTxtWriter implements MarcWriter {
    /**
     * Character encoding. Default is UTF-8.
     */
    private String indexkeyprefix = null;

    private PrintWriter out = null;

    private CharConverter conv = null;

    public MarcTxtWriter(OutputStream os) {
        this.out = new PrintWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8), true);
    }

    public MarcTxtWriter(OutputStream os, String indexkeyprefix) {
        this.out = new PrintWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8), true);
        this.indexkeyprefix = indexkeyprefix;
    }

    @Override
    public void write(Record record) {
        String recStr;

        if (conv != null) {
            recStr = applyConverter(record, conv);
        }
        else {
            recStr = record.toString();
        }
        if (indexkeyprefix != null) {
            String lines[] = recStr.split("\r?\n");
            for (String line : lines) {
                if (line.length() >= 3 && indexkeyprefix.contains(line.substring(0,3))) {
                    out.println(line);
                }
            }
            if (indexkeyprefix.contains("err") && record.getErrors() != null) {
                for (MarcError err : record.getErrors()) {
                    out.println(err.toString());
                }
            }
        }
        else {
            out.println(recStr);
        }
    }

    private String applyConverter(Record record, CharConverter conv) {
        final StringBuilder sb = new StringBuilder();
        sb.append("LEADER ");
        sb.append(record.getLeader().toString());
        sb.append('\n');
    
        for (final VariableField field : record.getVariableFields()) {
            if (field instanceof ControlField) {
                sb.append(field.toString());
            }
            else if (field instanceof DataField) {
                DataField df = (DataField)field;
                sb.append(df.getTag());
                sb.append(' ');
                sb.append(df.getIndicator1());
                sb.append(df.getIndicator2());

                for (final Subfield sf : df.getSubfields()) {
                    sb.append("$").append(sf.getCode()).append(conv.convert(sf.getData()));
                }
            }
            sb.append('\n');
        }
        return sb.toString();
    }


    @Override
    public void setConverter(CharConverter converter) { 
        conv = converter;
    }

    @Override
    public CharConverter getConverter() {
        return conv;
    }

    @Override
    public void close() {
        this.out.flush();
        this.out.close();
    }
}
@@ -103,32 +103,78 @@
 o.PrintWriter;%0D%0A
+import java.io.UnsupportedEncodingException;%0D%0A
 import java.nio.
@@ -709,24 +709,43 @@
 ream os) %7B%0D%0A
+        try %7B%0D%0A    
         this
@@ -821,24 +821,330 @@
 F_8), true);
+            %0D%0A        %7D%0D%0A        catch (java.lang.NoClassDefFoundError ncdf) %7B%0D%0A            try%0D%0A            %7B%0D%0A                this.out = new PrintWriter(new OutputStreamWriter(os, %22UTF-8%22), true);%0D%0A            %7D%0D%0A            catch (UnsupportedEncodingException e)%0D%0A            %7B%0D%0A            %7D%0D%0A        %7D
 %0D%0A    %7D%0D%0A%0D%0A 
@@ -1206,24 +1206,43 @@
 yprefix) %7B%0D%0A
+        try %7B%0D%0A    
         this
@@ -1314,32 +1314,338 @@
 s.UTF_8), true);
+            %0D%0A        %7D%0D%0A        catch (java.lang.NoClassDefFoundError ncdf) %7B%0D%0A            try%0D%0A            %7B%0D%0A                this.out = new PrintWriter(new OutputStreamWriter(os, %22UTF-8%22), true);%0D%0A            %7D%0D%0A            catch (UnsupportedEncodingException e)%0D%0A            %7B%0D%0A            %7D%0D%0A        %7D
 %0D%0A        this.i

Make class able to be used under Java 6 by avoiding using StandardCharsets.UTF_8