@@ -252,6 +252,7 @@ private static class Encoder extends DefaultStreamPipe<ObjectReceiver<String>> {
252252 private int indentationLevel ;
253253 private boolean formatted = PRETTY_PRINTED ;
254254 private int recordAttributeOffset ;
255+ private int recordLeaderOffset ;
255256
256257 private Encoder () {
257258 }
@@ -294,7 +295,7 @@ public void startRecord(final String identifier) {
294295 writeTag (Tag .record ::open );
295296 recordAttributeOffset = builder .length () - 1 ;
296297 prettyPrintNewLine ();
297-
298+ recordLeaderOffset = builder . length ();
298299 incrementIndentationLevel ();
299300 }
300301
@@ -353,7 +354,7 @@ else if (!appendLeader(name, value)) {
353354 if (value != null ) {
354355 writeEscaped (value .trim ());
355356 }
356- writeTag (Tag .controlfield ::close );
357+ writeTag (Tag .controlfield ::close , false );
357358 prettyPrintNewLine ();
358359 }
359360 }
@@ -408,9 +409,20 @@ private void writeFooter() {
408409 * @param str the unescaped sequence to be written
409410 */
410411 private void writeRaw (final String str ) {
412+
411413 builder .append (str );
412414 }
413415
416+ /**
417+ * Writes the unescaped sequence to the leader position.
418+ *
419+ * @param str the unescaped sequence to be written to the leader position
420+ */
421+ private void writeRawLeader (final String str ) {
422+ builder .insert (recordLeaderOffset , str );
423+ recordLeaderOffset = recordLeaderOffset + str .length ();
424+ }
425+
414426 private boolean appendLeader (final String name , final String value ) {
415427 if (name .equals (Marc21EventNames .LEADER_ENTITY )) {
416428 leaderBuilder .append (value );
@@ -432,11 +444,11 @@ private void writeEscaped(final String str) {
432444
433445 private void writeLeader () {
434446 final String leader = leaderBuilder .toString ();
435- if (! leader . isEmpty () ) {
447+ if (leaderBuilder . length () > 0 ) {
436448 prettyPrintIndentation ();
437- writeTag (Tag .leader ::open );
438- writeRaw (leader );
439- writeTag (Tag .leader ::close );
449+ writeTagLeader (Tag .leader ::open );
450+ writeRawLeader (leader );
451+ writeTagLeader (Tag .leader ::close );
440452 prettyPrintNewLine ();
441453 }
442454 }
@@ -447,6 +459,11 @@ private void writeTag(final Function<Object[], String> function, final Object...
447459 writeRaw (function .apply (allArgs ));
448460 }
449461
462+ private void writeTagLeader (final Function <Object [], String > function ) {
463+ writeRawLeader (function .apply (namespacePrefix ));
464+ }
465+
466+
450467 private void prettyPrintIndentation () {
451468 if (formatted ) {
452469 final String prefix = String .join ("" , Collections .nCopies (indentationLevel , INDENT ));
0 commit comments