@@ -120,7 +120,7 @@ public enum INPUT_TYPE {
120120 * A {@link BytesStore}, used during building, or during reading when the FST is very large (more
121121 * than 1 GB). If the FST is less than 1 GB then bytesArray is set instead.
122122 */
123- final BytesStore bytes ;
123+ final FSTWriter fstWriter ;
124124
125125 private final FSTStore fstStore ;
126126
@@ -395,14 +395,18 @@ private static boolean flag(int flags, int bit) {
395395 }
396396
397397 // make a new empty FST, for building; Builder invokes this
398- FST (INPUT_TYPE inputType , Outputs <T > outputs , int bytesPageBits ) {
398+ FST (INPUT_TYPE inputType , Outputs <T > outputs , FSTWriter fstWriter ) {
399399 this .inputType = inputType ;
400400 this .outputs = outputs ;
401401 fstStore = null ;
402- bytes = new BytesStore ( bytesPageBits ) ;
402+ this . fstWriter = fstWriter ;
403403 // pad: ensure no node gets address 0 which is reserved to mean
404404 // the stop state w/ no arcs
405- bytes .writeByte ((byte ) 0 );
405+ try {
406+ this .fstWriter .writeByte ((byte ) 0 );
407+ } catch (IOException e ) {
408+ throw new RuntimeException (e );
409+ }
406410 emptyOutput = null ;
407411 this .version = VERSION_CURRENT ;
408412 }
@@ -420,7 +424,7 @@ public FST(DataInput metaIn, DataInput in, Outputs<T> outputs) throws IOExceptio
420424 */
421425 public FST (DataInput metaIn , DataInput in , Outputs <T > outputs , FSTStore fstStore )
422426 throws IOException {
423- bytes = null ;
427+ fstWriter = null ;
424428 this .fstStore = fstStore ;
425429 this .outputs = outputs ;
426430
@@ -472,7 +476,7 @@ public long ramBytesUsed() {
472476 if (this .fstStore != null ) {
473477 size += this .fstStore .ramBytesUsed ();
474478 } else {
475- size += bytes .ramBytesUsed ();
479+ size += this . fstWriter .ramBytesUsed ();
476480 }
477481
478482 return size ;
@@ -484,19 +488,19 @@ public String toString() {
484488 }
485489
486490 void finish (long newStartNode ) throws IOException {
487- assert newStartNode <= bytes .getPosition ();
491+ assert newStartNode <= fstWriter .getPosition ();
488492 if (startNode != -1 ) {
489493 throw new IllegalStateException ("already finished" );
490494 }
491495 if (newStartNode == FINAL_END_NODE && emptyOutput != null ) {
492496 newStartNode = 0 ;
493497 }
494498 startNode = newStartNode ;
495- bytes .finish ();
499+ fstWriter .finish ();
496500 }
497501
498502 public long numBytes () {
499- return bytes .getPosition ();
503+ return fstWriter .getPosition ();
500504 }
501505
502506 public T getEmptyOutput () {
@@ -512,6 +516,21 @@ void setEmptyOutput(T v) {
512516 }
513517
514518 public void save (DataOutput metaOut , DataOutput out ) throws IOException {
519+ saveMetadata (metaOut );
520+ if (fstWriter != null ) {
521+ fstWriter .writeTo (out );
522+ } else {
523+ assert fstStore != null ;
524+ fstStore .writeTo (out );
525+ }
526+ }
527+
528+ /**
529+ * Save the metadata to a DataOutput
530+ *
531+ * @param metaOut the DataOutput to save
532+ */
533+ public void saveMetadata (DataOutput metaOut ) throws IOException {
515534 if (startNode == -1 ) {
516535 throw new IllegalStateException ("call finish first" );
517536 }
@@ -552,13 +571,9 @@ public void save(DataOutput metaOut, DataOutput out) throws IOException {
552571 }
553572 metaOut .writeByte (t );
554573 metaOut .writeVLong (startNode );
555- if (bytes != null ) {
556- long numBytes = bytes .getPosition ();
574+ if (fstWriter != null ) {
575+ long numBytes = fstWriter .getPosition ();
557576 metaOut .writeVLong (numBytes );
558- bytes .writeTo (out );
559- } else {
560- assert fstStore != null ;
561- fstStore .writeTo (out );
562577 }
563578 }
564579
@@ -1131,7 +1146,7 @@ public BytesReader getBytesReader() {
11311146 if (this .fstStore != null ) {
11321147 return this .fstStore .getReverseBytesReader ();
11331148 } else {
1134- return bytes .getReverseReader ();
1149+ return fstWriter .getReverseReader ();
11351150 }
11361151 }
11371152
0 commit comments