Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2016, 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.packager.rpm.header;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import org.eclipse.packager.rpm.ReadableHeader;
import org.eclipse.packager.rpm.RpmBaseTag;
import org.eclipse.packager.rpm.RpmTag;
public class Header implements ReadableHeader {
@FunctionalInterface
public interface ArrayAllocator {
T[] allocate(int length);
}
@FunctionalInterface
public interface Putter {
void put(Header header, T tag, V[] values);
}
@FunctionalInterface
public interface ToShortFunction {
short applyAsShort(T value);
}
private static final class I18nString {
private final String value;
public I18nString(final String value) {
this.value = value;
}
@Override
public String toString() {
return this.value;
}
}
private final Map entries = new LinkedHashMap<>();
private final Charset charset;
public Header(final HeaderEntry[] entries) {
this(entries, StandardCharsets.UTF_8);
}
public Header(final HeaderEntry[] entries, final Charset charset) {
Objects.requireNonNull(charset);
this.charset = charset;
if (entries != null) {
for (final HeaderEntry entry : entries) {
this.entries.put(entry.getTag(), entry);
}
}
}
public Header(final Header other) {
Objects.requireNonNull(other);
this.entries.putAll(other.entries);
this.charset = other.charset;
}
public Header() {
this.charset = StandardCharsets.UTF_8;
}
public int size() {
return this.entries.size();
}
public void putNull(final int tag) {
this.entries.put(tag, null);
}
public void putNull(final T tag) {
this.entries.put(tag.getValue(), null);
}
public void putByte(final int tag, final byte... value) {
Objects.requireNonNull(value);
this.entries.put(tag, makeEntry(tag, value));
}
public void putByte(final T tag, final byte... value) {
Objects.requireNonNull(value);
this.entries.put(tag.getValue(), makeEntry(tag.getValue(), value));
}
public void putShort(final int tag, final short... value) {
Objects.requireNonNull(value);
this.entries.put(tag, makeEntry(tag, value));
}
public void putShort(final T tag, final short... value) {
Objects.requireNonNull(value);
this.entries.put(tag.getValue(), makeEntry(tag.getValue(), value));
}
public void putInt(final int tag, final int... value) {
Objects.requireNonNull(value);
this.entries.put(tag, makeEntry(tag, value));
}
public void putInt(final T tag, final int... value) {
Objects.requireNonNull(value);
this.entries.put(tag.getValue(), makeEntry(tag.getValue(), value));
}
public void putLong(final int tag, final long... value) {
Objects.requireNonNull(value);
this.entries.put(tag, makeEntry(tag, value));
}
public void putLong(final T tag, final long... value) {
Objects.requireNonNull(value);
this.entries.put(tag.getValue(), makeEntry(tag.getValue(), value));
}
public void putString(final int tag, final String value) {
Objects.requireNonNull(value);
this.entries.put(tag, makeEntry(tag, value));
}
public void putString(final T tag, final String value) {
Objects.requireNonNull(value);
this.entries.put(tag.getValue(), makeEntry(tag.getValue(), value));
}
public void putStringOptional(final int tag, final String value) {
if (value == null) {
return;
}
this.entries.put(tag, makeEntry(tag, value));
}
public void putStringOptional(final T tag, final String value) {
if (value == null) {
return;
}
this.entries.put(tag.getValue(), makeEntry(tag.getValue(), value));
}
public void putStringArray(final int tag, final String... value) {
Objects.requireNonNull(value);
this.entries.put(tag, makeEntry(tag, value));
}
public void putStringArray(final T tag, final String... value) {
Objects.requireNonNull(value);
this.entries.put(tag.getValue(), makeEntry(tag.getValue(), value));
}
public void putI18nString(final int tag, final String... value) {
Objects.requireNonNull(value);
this.entries.put(tag, makeEntry(tag, Arrays.stream(value).map(I18nString::new).toArray(I18nString[]::new)));
}
public void putI18nString(final T tag, final String... value) {
Objects.requireNonNull(value);
this.entries.put(tag.getValue(), makeEntry(tag.getValue(), Arrays.stream(value).map(I18nString::new).toArray(I18nString[]::new)));
}
public void putBlob(final int tag, final byte[] value) {
Objects.requireNonNull(value);
this.entries.put(tag, makeEntry(tag, ByteBuffer.wrap(value)));
}
public void putBlob(final int tag, final ByteBuffer value) {
Objects.requireNonNull(value);
this.entries.put(tag, makeEntry(tag, value));
}
public void putBlob(final T tag, final byte[] value) {
Objects.requireNonNull(value);
this.entries.put(tag.getValue(), makeEntry(tag.getValue(), ByteBuffer.wrap(value)));
}
public void putBlob(final T tag, final ByteBuffer value) {
Objects.requireNonNull(value);
this.entries.put(tag.getValue(), makeEntry(tag.getValue(), value));
}
public void putSize(long value, final T intTag, final T longTag) {
Objects.requireNonNull(intTag);
Objects.requireNonNull(longTag);
if (value <= 0) {
value = 0;
}
if (value > Integer.MAX_VALUE) {
putLong(longTag, value);
} else {
putInt(intTag, (int) value);
}
}
public void putAll(final Header headers) {
this.entries.putAll(headers.entries);
}
public void remove(final int tag) {
this.entries.remove(tag);
}
public void remove(final RpmTag tag) {
this.entries.remove(tag.getValue());
}
public Object get(final int tag) {
return this.entries.get(tag).getValue();
}
public Object get(final T tag) {
return this.entries.get(tag.getValue()).getValue();
}
@Override
public Optional