1818 */
1919package org .mapsforge .map .reader ;
2020
21+ import org .mapsforge .core .model .Tag ;
2122import org .mapsforge .core .util .Parameters ;
2223
2324import java .io .IOException ;
2425import java .io .RandomAccessFile ;
2526import java .io .UnsupportedEncodingException ;
2627import java .nio .ByteBuffer ;
2728import java .nio .channels .FileChannel ;
29+ import java .util .ArrayList ;
30+ import java .util .List ;
2831import java .util .logging .Logger ;
2932
3033/**
@@ -40,6 +43,8 @@ public class ReadBuffer {
4043 private ByteBuffer bufferWrapper ;
4144 private final FileChannel inputChannel ;
4245
46+ private final List <Integer > tagIds = new ArrayList <>();
47+
4348 ReadBuffer (FileChannel inputChannel ) {
4449 this .inputChannel = inputChannel ;
4550 }
@@ -55,14 +60,13 @@ public byte readByte() {
5560
5661 /**
5762 * Converts four bytes from the read buffer to a float.
63+ * <p/>
64+ * The byte order is big-endian.
5865 *
5966 * @return the float value.
6067 */
6168 public float readFloat () {
62- byte [] bytes = new byte [4 ];
63- System .arraycopy (bufferData , bufferPosition , bytes , 0 , 4 );
64- this .bufferPosition += 4 ;
65- return ByteBuffer .wrap (bytes ).getFloat ();
69+ return Float .intBitsToFloat (readInt ());
6670 }
6771
6872 /**
@@ -186,6 +190,49 @@ public int readSignedInt() {
186190 return variableByteDecode | ((this .bufferData [this .bufferPosition ++] & 0x3f ) << variableByteShift );
187191 }
188192
193+ List <Tag > readTags (Tag [] tagsArray , byte numberOfTags ) {
194+ List <Tag > tags = new ArrayList <>();
195+ tagIds .clear ();
196+
197+ int maxTag = tagsArray .length ;
198+
199+ for (byte tagIndex = numberOfTags ; tagIndex != 0 ; --tagIndex ) {
200+ int tagId = readUnsignedInt ();
201+ if (tagId < 0 || tagId >= maxTag ) {
202+ LOGGER .warning ("invalid tag ID: " + tagId );
203+ return null ;
204+ }
205+ tagIds .add (tagId );
206+ }
207+
208+ for (int tagId : tagIds ) {
209+ Tag tag = tagsArray [tagId ];
210+ // Decode variable values of tags
211+ if (tag .value .charAt (0 ) == '%' && tag .value .length () == 2 ) {
212+ String value = tag .value ;
213+ if (value .charAt (1 ) == 'b' ) {
214+ value = String .valueOf (readByte ());
215+ } else if (value .charAt (1 ) == 'i' ) {
216+ if (tag .key .contains (":colour" )) {
217+ value = "#" + Integer .toHexString (readInt ());
218+ } else {
219+ value = String .valueOf (readInt ());
220+ }
221+ } else if (value .charAt (1 ) == 'f' ) {
222+ value = String .valueOf (readFloat ());
223+ } else if (value .charAt (1 ) == 'h' ) {
224+ value = String .valueOf (readShort ());
225+ } else if (value .charAt (1 ) == 's' ) {
226+ value = readUTF8EncodedString ();
227+ }
228+ tag = new Tag (tag .key , value );
229+ }
230+ tags .add (tag );
231+ }
232+
233+ return tags ;
234+ }
235+
189236 /**
190237 * Converts a variable amount of bytes from the read buffer to an unsigned int.
191238 * <p/>
0 commit comments