|
| 1 | +/* |
| 2 | + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion |
| 3 | + * Copyright (C) 2016-2025 ViaVersion and contributors |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in all |
| 13 | + * copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | + * SOFTWARE. |
| 22 | + */ |
| 23 | +package com.viaversion.viaversion.api.type.types.misc; |
| 24 | + |
| 25 | +import com.google.common.base.Preconditions; |
| 26 | +import com.viaversion.nbt.tag.Tag; |
| 27 | +import com.viaversion.viaversion.api.type.Type; |
| 28 | +import com.viaversion.viaversion.api.type.Types; |
| 29 | +import io.netty.buffer.ByteBuf; |
| 30 | + |
| 31 | +public final class LengthPrefixedTagType extends Type<Tag> { |
| 32 | + |
| 33 | + private final int maxLength; |
| 34 | + |
| 35 | + public LengthPrefixedTagType(int maxLength) { |
| 36 | + super(Tag.class); |
| 37 | + this.maxLength = maxLength; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public Tag read(final ByteBuf buffer) { |
| 42 | + final int length = Types.VAR_INT.readPrimitive(buffer); |
| 43 | + if (length <= 0) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + Preconditions.checkArgument(length <= maxLength, |
| 48 | + "Cannot receive tag longer than %s bytes (got %s bytes)", maxLength, length); |
| 49 | + |
| 50 | + return Types.TAG.read(buffer.readSlice(length)); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void write(final ByteBuf buffer, final Tag tag) { |
| 55 | + if (tag == null) { |
| 56 | + Types.VAR_INT.writePrimitive(buffer, 0); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + final ByteBuf tempBuf = buffer.alloc().buffer(); |
| 61 | + try { |
| 62 | + Types.TAG.write(tempBuf, tag); |
| 63 | + |
| 64 | + Preconditions.checkArgument(tempBuf.readableBytes() <= maxLength, |
| 65 | + "Cannot send tag longer than %s bytes (got %s bytes)", maxLength, tempBuf.readableBytes()); |
| 66 | + |
| 67 | + Types.VAR_INT.writePrimitive(buffer, tempBuf.readableBytes()); |
| 68 | + buffer.writeBytes(tempBuf); |
| 69 | + } finally { |
| 70 | + tempBuf.release(); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments