001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.protobuf; 003 004import java.io.ByteArrayOutputStream; 005import java.io.IOException; 006import java.nio.charset.StandardCharsets; 007 008import org.openstreetmap.josm.tools.Utils; 009 010/** 011 * A protobuf record, storing the {@link WireType}, the parsed field number, and the bytes for it. 012 * 013 * @author Taylor Smock 014 * @since 17862 015 */ 016public class ProtobufRecord implements AutoCloseable { 017 private static final byte[] EMPTY_BYTES = {}; 018 private final WireType type; 019 private final int field; 020 private byte[] bytes; 021 022 /** 023 * Create a new Protobuf record 024 * 025 * @param byteArrayOutputStream A reusable ByteArrayOutputStream to avoid unnecessary allocations 026 * @param parser The parser to use to create the record 027 * @throws IOException - if an IO error occurs 028 */ 029 public ProtobufRecord(ByteArrayOutputStream byteArrayOutputStream, ProtobufParser parser) throws IOException { 030 final byte[] varInt = parser.nextVarInt(byteArrayOutputStream); 031 long number = ProtobufParser.convertByteArray(varInt, ProtobufParser.VAR_INT_BYTE_SIZE, 0, varInt.length); 032 // I don't foresee having field numbers > {@code Integer#MAX_VALUE >> 3} 033 this.field = (int) number >> 3; 034 // 7 is 111 (so last three bits) 035 byte wireType = (byte) (number & 7); 036 // By not using a stream, we reduce the number of allocations (for getting the WireType) from 257 MB to 40 MB. 037 // (The remaining 40 MB is from WireType#values). By using the cached getAllValues(), we drop the 40 MB. 038 WireType tType = WireType.UNKNOWN; 039 for (WireType wType : WireType.getAllValues()) { 040 if (wType.getTypeRepresentation() == wireType) { 041 tType = wType; 042 break; 043 } 044 } 045 this.type = tType; 046 047 if (this.type == WireType.VARINT) { 048 this.bytes = parser.nextVarInt(byteArrayOutputStream); 049 } else if (this.type == WireType.SIXTY_FOUR_BIT) { 050 this.bytes = parser.nextFixed64(); 051 } else if (this.type == WireType.THIRTY_TWO_BIT) { 052 this.bytes = parser.nextFixed32(); 053 } else if (this.type == WireType.LENGTH_DELIMITED) { 054 this.bytes = parser.nextLengthDelimited(byteArrayOutputStream); 055 } else { 056 this.bytes = EMPTY_BYTES; 057 } 058 } 059 060 /** 061 * Get as a double ({@link WireType#SIXTY_FOUR_BIT}) 062 * 063 * @return the double 064 */ 065 public double asDouble() { 066 long doubleNumber = ProtobufParser.convertByteArray(asFixed64(), ProtobufParser.BYTE_SIZE).longValue(); 067 return Double.longBitsToDouble(doubleNumber); 068 } 069 070 /** 071 * Get as 32 bits ({@link WireType#THIRTY_TWO_BIT}) 072 * 073 * @return a byte array of the 32 bits (4 bytes) 074 */ 075 public byte[] asFixed32() { 076 // TODO verify, or just assume? 077 // 4 bytes == 32 bits 078 return this.bytes; 079 } 080 081 /** 082 * Get as 64 bits ({@link WireType#SIXTY_FOUR_BIT}) 083 * 084 * @return a byte array of the 64 bits (8 bytes) 085 */ 086 public byte[] asFixed64() { 087 // TODO verify, or just assume? 088 // 8 bytes == 64 bits 089 return this.bytes; 090 } 091 092 /** 093 * Get as a float ({@link WireType#THIRTY_TWO_BIT}) 094 * 095 * @return the float 096 */ 097 public float asFloat() { 098 int floatNumber = ProtobufParser.convertByteArray(asFixed32(), ProtobufParser.BYTE_SIZE).intValue(); 099 return Float.intBitsToFloat(floatNumber); 100 } 101 102 /** 103 * Get the signed var int ({@code WireType#VARINT}). 104 * These are specially encoded so that they take up less space. 105 * 106 * @return The signed var int ({@code sint32} or {@code sint64}) 107 */ 108 public Number asSignedVarInt() { 109 final Number signed = this.asUnsignedVarInt(); 110 return ProtobufParser.decodeZigZag(signed); 111 } 112 113 /** 114 * Get as a string ({@link WireType#LENGTH_DELIMITED}) 115 * 116 * @return The string (encoded as {@link StandardCharsets#UTF_8}) 117 */ 118 public String asString() { 119 return Utils.intern(new String(this.bytes, StandardCharsets.UTF_8)); 120 } 121 122 /** 123 * Get the var int ({@code WireType#VARINT}) 124 * 125 * @return The var int ({@code int32}, {@code int64}, {@code uint32}, {@code uint64}, {@code bool}, {@code enum}) 126 */ 127 public Number asUnsignedVarInt() { 128 return ProtobufParser.convertByteArray(this.bytes, ProtobufParser.VAR_INT_BYTE_SIZE); 129 } 130 131 @Override 132 public void close() { 133 this.bytes = null; 134 } 135 136 /** 137 * Get the raw bytes for this record 138 * 139 * @return The bytes 140 */ 141 public byte[] getBytes() { 142 return this.bytes; 143 } 144 145 /** 146 * Get the field value 147 * 148 * @return The field value 149 */ 150 public int getField() { 151 return this.field; 152 } 153 154 /** 155 * Get the WireType of the data 156 * 157 * @return The {@link WireType} of the data 158 */ 159 public WireType getType() { 160 return this.type; 161 } 162}