blob: 9b38edaa9b759ca7c37038eb887119725b0a69f3 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.qpid.protonj2.codec.encoders;
import org.apache.qpid.protonj2.buffer.ProtonBuffer;
import org.apache.qpid.protonj2.codec.EncoderState;
import org.apache.qpid.protonj2.codec.EncodingCodes;
/**
* Abstract implementation of the PrimitiveTypeEncoder that implements the common methods
* that most of the primitive type
*
* @param <V> The type that this primitive encoder handles
*/
public abstract class AbstractPrimitiveTypeEncoder<V> implements PrimitiveTypeEncoder<V> {
@Override
public boolean isArrayType() {
return false;
}
@Override
public void writeArray(ProtonBuffer buffer, EncoderState state, Object[] values) {
// Write the Array Type encoding code, we don't optimize here.
buffer.writeByte(EncodingCodes.ARRAY32);
int startIndex = buffer.getWriteIndex();
// Reserve space for the size and write the count of list elements.
buffer.writeInt(0);
buffer.writeInt(values.length);
// Write the array elements after writing the array length
writeRawArray(buffer, state, values);
// Move back and write the size
long writeSize = buffer.getWriteIndex() - startIndex - Integer.BYTES;
if (writeSize > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Cannot encode given array, encoded size too large: " + writeSize);
}
buffer.setInt(startIndex, (int) writeSize);
}
}