@@ -164,14 +164,12 @@ private FSTCompiler(
164164 boolean allowFixedLengthArcs ,
165165 DataOutput dataOutput ,
166166 float directAddressingMaxOversizingFactor ,
167- int version )
168- throws IOException {
167+ int version ) {
169168 this .allowFixedLengthArcs = allowFixedLengthArcs ;
170169 this .directAddressingMaxOversizingFactor = directAddressingMaxOversizingFactor ;
171170 this .version = version ;
172171 // pad: ensure no node gets address 0 which is reserved to mean
173- // the stop state w/ no arcs
174- dataOutput .writeByte ((byte ) 0 );
172+ // the stop state w/ no arcs. the actual byte will be written lazily
175173 numBytesWritten ++;
176174 this .dataOutput = dataOutput ;
177175 fst =
@@ -344,7 +342,7 @@ public Builder<T> setVersion(int version) {
344342 }
345343
346344 /** Creates a new {@link FSTCompiler}. */
347- public FSTCompiler <T > build () throws IOException {
345+ public FSTCompiler <T > build () {
348346 // create a default DataOutput if not specified
349347 if (dataOutput == null ) {
350348 dataOutput = getOnHeapReaderWriter (15 );
@@ -552,6 +550,10 @@ long addNode(FSTCompiler.UnCompiledNode<T> nodeIn) throws IOException {
552550 }
553551
554552 reverseScratchBytes ();
553+ if (numBytesWritten == 1 ) {
554+ // first time, write the padding byte
555+ dataOutput .writeByte ((byte ) 0 );
556+ }
555557 scratchBytes .writeTo (dataOutput );
556558 numBytesWritten += scratchBytes .getPosition ();
557559
0 commit comments