1717 */
1818package net .raphimc .noteblocklib .format .nbs ;
1919
20- import com .google .common .io .LittleEndianDataInputStream ;
21- import com .google .common .io .LittleEndianDataOutputStream ;
2220import net .raphimc .noteblocklib .format .nbs .model .NbsCustomInstrument ;
2321import net .raphimc .noteblocklib .format .nbs .model .NbsLayer ;
2422import net .raphimc .noteblocklib .format .nbs .model .NbsNote ;
2523import net .raphimc .noteblocklib .format .nbs .model .NbsSong ;
24+ import net .raphimc .noteblocklib .util .io .BinaryInputStream ;
25+ import net .raphimc .noteblocklib .util .io .BinaryOutputStream ;
2626
2727import java .io .BufferedInputStream ;
2828import java .io .BufferedOutputStream ;
2929import java .io .IOException ;
3030import java .io .InputStream ;
3131import java .io .OutputStream ;
32+ import java .nio .ByteOrder ;
3233import java .util .Map ;
3334import java .util .TreeMap ;
3435
@@ -40,15 +41,15 @@ private NbsIo() {
4041 }
4142
4243 public static NbsSong readSong (final InputStream is , final String fileName ) throws IOException {
43- final LittleEndianDataInputStream dis = new LittleEndianDataInputStream (new BufferedInputStream (is , BUFFER_SIZE ));
44+ final BinaryInputStream bis = new BinaryInputStream (new BufferedInputStream (is , BUFFER_SIZE ), ByteOrder . LITTLE_ENDIAN );
4445 final NbsSong song = new NbsSong (fileName );
4546
46- final short length = dis .readShort ();
47+ final short length = bis .readShort ();
4748 if (length == 0 ) {
48- song .setVersion (dis .readUnsignedByte ());
49- song .setVanillaInstrumentCount (dis .readUnsignedByte ());
49+ song .setVersion (bis .readUnsignedByte ());
50+ song .setVanillaInstrumentCount (bis .readUnsignedByte ());
5051 if (song .getVersion () >= 3 ) {
51- song .setLength (dis .readShort ());
52+ song .setLength (bis .readShort ());
5253 } else {
5354 song .setLength ((short ) -1 );
5455 }
@@ -62,63 +63,63 @@ public static NbsSong readSong(final InputStream is, final String fileName) thro
6263 throw new IllegalStateException ("Unsupported NBS version: " + song .getVersion ());
6364 }
6465
65- song .setLayerCount (dis .readShort ());
66- song .setTitle (readString (dis ));
67- song .setAuthor (readString (dis ));
68- song .setOriginalAuthor (readString (dis ));
69- song .setDescription (readString (dis ));
70- song .setTempo (dis .readShort ());
71- song .setAutoSave (dis .readBoolean ());
72- song .setAutoSaveInterval (dis .readUnsignedByte ());
73- song .setTimeSignature (dis .readUnsignedByte ());
74- song .setMinutesSpent (dis .readInt ());
75- song .setLeftClicks (dis .readInt ());
76- song .setRightClicks (dis .readInt ());
77- song .setNoteBlocksAdded (dis .readInt ());
78- song .setNoteBlocksRemoved (dis .readInt ());
79- song .setSourceFileName (readString (dis ));
66+ song .setLayerCount (bis .readShort ());
67+ song .setTitle (readString (bis ));
68+ song .setAuthor (readString (bis ));
69+ song .setOriginalAuthor (readString (bis ));
70+ song .setDescription (readString (bis ));
71+ song .setTempo (bis .readShort ());
72+ song .setAutoSave (bis .readBoolean ());
73+ song .setAutoSaveInterval (bis .readUnsignedByte ());
74+ song .setTimeSignature (bis .readUnsignedByte ());
75+ song .setMinutesSpent (bis .readInt ());
76+ song .setLeftClicks (bis .readInt ());
77+ song .setRightClicks (bis .readInt ());
78+ song .setNoteBlocksAdded (bis .readInt ());
79+ song .setNoteBlocksRemoved (bis .readInt ());
80+ song .setSourceFileName (readString (bis ));
8081
8182 if (song .getVersion () >= 4 ) {
82- song .setLoop (dis .readBoolean ());
83- song .setMaxLoopCount (dis .readUnsignedByte ());
84- song .setLoopStartTick (dis .readShort ());
83+ song .setLoop (bis .readBoolean ());
84+ song .setMaxLoopCount (bis .readUnsignedByte ());
85+ song .setLoopStartTick (bis .readShort ());
8586 }
8687
8788 final Map <Integer , NbsLayer > layers = song .getLayers ();
8889 int tick = -1 ;
8990 while (true ) {
90- final short jumpTicks = dis .readShort ();
91+ final short jumpTicks = bis .readShort ();
9192 if (jumpTicks == 0 ) {
9293 break ;
9394 }
9495 tick += jumpTicks ;
9596
9697 int layer = -1 ;
9798 while (true ) {
98- final short jumpLayers = dis .readShort ();
99+ final short jumpLayers = bis .readShort ();
99100 if (jumpLayers == 0 ) {
100101 break ;
101102 }
102103 layer += jumpLayers ;
103104
104105 final NbsNote note = new NbsNote ();
105- note .setInstrument (dis .readUnsignedByte ());
106- note .setKey (dis .readUnsignedByte ());
106+ note .setInstrument (bis .readUnsignedByte ());
107+ note .setKey (bis .readUnsignedByte ());
107108 if (song .getVersion () >= 4 ) {
108- note .setVelocity (dis .readUnsignedByte ());
109- note .setPanning (dis .readUnsignedByte ());
110- note .setPitch (dis .readShort ());
109+ note .setVelocity (bis .readUnsignedByte ());
110+ note .setPanning (bis .readUnsignedByte ());
111+ note .setPitch (bis .readShort ());
111112 }
112113 layers .computeIfAbsent (layer , k -> new NbsLayer ()).getNotes ().put (tick , note );
113114 }
114115 }
115116
116- if (dis .available () > 0 ) {
117+ if (bis .available () > 0 ) {
117118 for (int i = 0 ; i < song .getLayerCount (); i ++) {
118119 final NbsLayer layer = layers .computeIfAbsent (i , k -> new NbsLayer ());
119- layer .setName (readString (dis ));
120+ layer .setName (readString (bis ));
120121 if (song .getVersion () >= 4 ) {
121- final int lockedByte = dis .readUnsignedByte ();
122+ final int lockedByte = bis .readUnsignedByte ();
122123 switch (lockedByte ) {
123124 case 0 :
124125 layer .setStatus (NbsLayer .Status .NONE );
@@ -131,21 +132,21 @@ public static NbsSong readSong(final InputStream is, final String fileName) thro
131132 break ;
132133 }
133134 }
134- layer .setVolume (dis .readUnsignedByte ());
135+ layer .setVolume (bis .readUnsignedByte ());
135136 if (song .getVersion () >= 2 ) {
136- layer .setPanning (dis .readUnsignedByte ());
137+ layer .setPanning (bis .readUnsignedByte ());
137138 }
138139 }
139140 }
140141
141- if (dis .available () > 0 ) {
142- final int customInstrumentCount = dis .readUnsignedByte ();
142+ if (bis .available () > 0 ) {
143+ final int customInstrumentCount = bis .readUnsignedByte ();
143144 for (int i = 0 ; i < customInstrumentCount ; i ++) {
144145 final NbsCustomInstrument customInstrument = new NbsCustomInstrument ();
145- customInstrument .setName (readString (dis ));
146- customInstrument .setSoundFilePath (readString (dis ));
147- customInstrument .setPitch (dis .readUnsignedByte ());
148- customInstrument .setPressKey (dis .readBoolean ());
146+ customInstrument .setName (readString (bis ));
147+ customInstrument .setSoundFilePath (readString (bis ));
148+ customInstrument .setPitch (bis .readUnsignedByte ());
149+ customInstrument .setPressKey (bis .readBoolean ());
149150 song .getCustomInstruments ().add (customInstrument );
150151 }
151152 }
@@ -161,40 +162,39 @@ public static void writeSong(final NbsSong song, final OutputStream os) throws I
161162 if (song .getLayerCount () > song .getLayers ().size ()) {
162163 throw new IllegalArgumentException ("Layer count must be less than or equal to the amount of layers" );
163164 }
164-
165- final LittleEndianDataOutputStream dos = new LittleEndianDataOutputStream (new BufferedOutputStream (os , BUFFER_SIZE ));
165+ final BinaryOutputStream bos = new BinaryOutputStream (new BufferedOutputStream (os , BUFFER_SIZE ), ByteOrder .LITTLE_ENDIAN );
166166
167167 if (song .getVersion () == 0 ) {
168- dos .writeShort (song .getLength ());
168+ bos .writeShort (song .getLength ());
169169 } else {
170- dos .writeShort (0 );
171- dos . writeByte (song .getVersion ());
172- dos . writeByte (song .getVanillaInstrumentCount ());
170+ bos .writeShort (0 );
171+ bos . writeUnsignedByte (song .getVersion ());
172+ bos . writeUnsignedByte (song .getVanillaInstrumentCount ());
173173 if (song .getVersion () >= 3 ) {
174- dos .writeShort (song .getLength ());
174+ bos .writeShort (song .getLength ());
175175 }
176176 }
177177
178- dos .writeShort (song .getLayerCount ());
179- writeString (dos , song .getTitleOr ("" ));
180- writeString (dos , song .getAuthorOr ("" ));
181- writeString (dos , song .getOriginalAuthorOr ("" ));
182- writeString (dos , song .getDescriptionOr ("" ));
183- dos .writeShort (song .getTempo ());
184- dos .writeBoolean (song .isAutoSave ());
185- dos . writeByte (song .getAutoSaveInterval ());
186- dos . writeByte (song .getTimeSignature ());
187- dos .writeInt (song .getMinutesSpent ());
188- dos .writeInt (song .getLeftClicks ());
189- dos .writeInt (song .getRightClicks ());
190- dos .writeInt (song .getNoteBlocksAdded ());
191- dos .writeInt (song .getNoteBlocksRemoved ());
192- writeString (dos , song .getSourceFileNameOr ("" ));
178+ bos .writeShort (song .getLayerCount ());
179+ writeString (bos , song .getTitleOr ("" ));
180+ writeString (bos , song .getAuthorOr ("" ));
181+ writeString (bos , song .getOriginalAuthorOr ("" ));
182+ writeString (bos , song .getDescriptionOr ("" ));
183+ bos .writeShort (song .getTempo ());
184+ bos .writeBoolean (song .isAutoSave ());
185+ bos . writeUnsignedByte (song .getAutoSaveInterval ());
186+ bos . writeUnsignedByte (song .getTimeSignature ());
187+ bos .writeInt (song .getMinutesSpent ());
188+ bos .writeInt (song .getLeftClicks ());
189+ bos .writeInt (song .getRightClicks ());
190+ bos .writeInt (song .getNoteBlocksAdded ());
191+ bos .writeInt (song .getNoteBlocksRemoved ());
192+ writeString (bos , song .getSourceFileNameOr ("" ));
193193
194194 if (song .getVersion () >= 4 ) {
195- dos .writeBoolean (song .isLoop ());
196- dos . writeByte (song .getMaxLoopCount ());
197- dos .writeShort (song .getLoopStartTick ());
195+ bos .writeBoolean (song .isLoop ());
196+ bos . writeUnsignedByte (song .getMaxLoopCount ());
197+ bos .writeShort (song .getLoopStartTick ());
198198 }
199199
200200 final Map <Integer , Map <Integer , NbsNote >> notes = new TreeMap <>();
@@ -206,74 +206,74 @@ public static void writeSong(final NbsSong song, final OutputStream os) throws I
206206
207207 int lastTick = -1 ;
208208 for (Map .Entry <Integer , Map <Integer , NbsNote >> tickEntry : notes .entrySet ()) {
209- dos .writeShort (tickEntry .getKey () - lastTick );
209+ bos .writeShort (tickEntry .getKey () - lastTick );
210210 lastTick = tickEntry .getKey ();
211211
212212 int lastLayer = -1 ;
213213 for (Map .Entry <Integer , NbsNote > layerEntry : tickEntry .getValue ().entrySet ()) {
214- dos .writeShort (layerEntry .getKey () - lastLayer );
214+ bos .writeShort (layerEntry .getKey () - lastLayer );
215215 lastLayer = layerEntry .getKey ();
216216
217217 final NbsNote note = layerEntry .getValue ();
218- dos . writeByte (note .getInstrument ());
219- dos . writeByte (note .getKey ());
218+ bos . writeUnsignedByte (note .getInstrument ());
219+ bos . writeUnsignedByte (note .getKey ());
220220 if (song .getVersion () >= 4 ) {
221- dos . writeByte (note .getVelocity ());
222- dos . writeByte (note .getPanning ());
223- dos .writeShort (note .getPitch ());
221+ bos . writeUnsignedByte (note .getVelocity ());
222+ bos . writeUnsignedByte (note .getPanning ());
223+ bos .writeShort (note .getPitch ());
224224 }
225225 }
226- dos .writeShort (0 );
226+ bos .writeShort (0 );
227227 }
228- dos .writeShort (0 );
228+ bos .writeShort (0 );
229229
230230 for (int i = 0 ; i < song .getLayerCount (); i ++) {
231231 final NbsLayer layer = song .getLayers ().get (i );
232- writeString (dos , layer .getNameOr ("" ));
232+ writeString (bos , layer .getNameOr ("" ));
233233 if (song .getVersion () >= 4 ) {
234234 switch (layer .getStatus ()) {
235235 case NONE :
236- dos . writeByte (0 );
236+ bos . writeUnsignedByte (0 );
237237 break ;
238238 case LOCKED :
239- dos . writeByte (1 );
239+ bos . writeUnsignedByte (1 );
240240 break ;
241241 case SOLO :
242- dos . writeByte (2 );
242+ bos . writeUnsignedByte (2 );
243243 break ;
244244 default :
245245 throw new IllegalStateException ("Unsupported layer status: " + layer .getStatus ());
246246 }
247247 }
248- dos . writeByte (layer .getVolume ());
248+ bos . writeUnsignedByte (layer .getVolume ());
249249 if (song .getVersion () >= 2 ) {
250- dos . writeByte (layer .getPanning ());
250+ bos . writeUnsignedByte (layer .getPanning ());
251251 }
252252 }
253253
254- dos . writeByte (song .getCustomInstruments ().size ());
254+ bos . writeUnsignedByte (song .getCustomInstruments ().size ());
255255 for (NbsCustomInstrument customInstrument : song .getCustomInstruments ()) {
256- writeString (dos , customInstrument .getNameOr ("" ));
257- writeString (dos , customInstrument .getSoundFilePathOr ("" ));
258- dos . writeByte (customInstrument .getPitch ());
259- dos .writeBoolean (customInstrument .isPressKey ());
256+ writeString (bos , customInstrument .getNameOr ("" ));
257+ writeString (bos , customInstrument .getSoundFilePathOr ("" ));
258+ bos . writeUnsignedByte (customInstrument .getPitch ());
259+ bos .writeBoolean (customInstrument .isPressKey ());
260260 }
261261
262- dos .flush ();
262+ bos .flush ();
263263 }
264264
265- private static String readString (final LittleEndianDataInputStream dis ) throws IOException {
266- final char [] buffer = new char [dis .readInt ()];
265+ private static String readString (final BinaryInputStream bis ) throws IOException {
266+ final char [] buffer = new char [bis .readInt ()];
267267 for (int i = 0 ; i < buffer .length ; i ++) {
268- buffer [i ] = (char ) dis .readUnsignedByte ();
268+ buffer [i ] = (char ) bis .readUnsignedByte ();
269269 }
270270 return new String (buffer );
271271 }
272272
273- private static void writeString (final LittleEndianDataOutputStream dos , final String string ) throws IOException {
274- dos .writeInt (string .length ());
273+ private static void writeString (final BinaryOutputStream bos , final String string ) throws IOException {
274+ bos .writeInt (string .length ());
275275 for (char c : string .toCharArray ()) {
276- dos . writeByte (c );
276+ bos . writeUnsignedByte (c );
277277 }
278278 }
279279
0 commit comments