org.eclipse.packager.rpm.header.Header Maven / Gradle / Ivy
/**
* 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
{
public T[] allocate ( int length );
}
@FunctionalInterface
public interface Putter
{
public void put ( Header header, T tag, V[] values );
}
@FunctionalInterface
public interface ToShortFunction
{
public short applyAsShort ( T value );
}
private static final class I18nString
{
private final String value;
public I18nString ( final String value )
{
this.value = 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, value );
}
public void putByte ( final T tag, final byte... value )
{
Objects.requireNonNull ( value );
this.entries.put ( tag.getValue (), value );
}
public void putShort ( final int tag, final short... value )
{
Objects.requireNonNull ( value );
this.entries.put ( tag, value );
}
public void putShort ( final T tag, final short... value )
{
Objects.requireNonNull ( value );
this.entries.put ( tag.getValue (), value );
}
public void putInt ( final int tag, final int... value )
{
Objects.requireNonNull ( value );
this.entries.put ( tag, value );
}
public void putInt ( final T tag, final int... value )
{
Objects.requireNonNull ( value );
this.entries.put ( tag.getValue (), value );
}
public void putLong ( final int tag, final long... value )
{
Objects.requireNonNull ( value );
this.entries.put ( tag, value );
}
public void putLong ( final T tag, final long... value )
{
Objects.requireNonNull ( value );
this.entries.put ( tag.getValue (), value );
}
public void putString ( final int tag, final String value )
{
Objects.requireNonNull ( value );
this.entries.put ( tag, value );
}
public void putString ( final T tag, final String value )
{
Objects.requireNonNull ( value );
this.entries.put ( tag.getValue (), value );
}
public void putStringOptional ( final int tag, final String value )
{
if ( value == null )
{
return;
}
this.entries.put ( tag, value );
}
public void putStringOptional ( final T tag, final String value )
{
if ( value == null )
{
return;
}
this.entries.put ( tag.getValue (), value );
}
public void putStringArray ( final int tag, final String... value )
{
Objects.requireNonNull ( value );
this.entries.put ( tag, value );
}
public void putStringArray ( final T tag, final String... value )
{
Objects.requireNonNull ( value );
this.entries.put ( tag.getValue (), value );
}
public void putI18nString ( final int tag, final String... value )
{
Objects.requireNonNull ( value );
this.entries.put ( tag, Arrays.stream ( value ).map ( v -> new I18nString ( v ) ).toArray ( I18nString[]::new ) );
}
public void putI18nString ( final T tag, final String... value )
{
Objects.requireNonNull ( value );
this.entries.put ( tag.getValue (), Arrays.stream ( value ).map ( v -> new I18nString ( v ) ).toArray ( I18nString[]::new ) );
}
public void putBlob ( final int tag, final byte[] value )
{
Objects.requireNonNull ( value );
this.entries.put ( tag, ByteBuffer.wrap ( value ) );
}
public void putBlob ( final int tag, final ByteBuffer value )
{
Objects.requireNonNull ( value );
this.entries.put ( tag, value );
}
public void putBlob ( final T tag, final byte[] value )
{
Objects.requireNonNull ( value );
this.entries.put ( tag.getValue (), ByteBuffer.wrap ( value ) );
}
public void putBlob ( final T tag, final ByteBuffer value )
{
Objects.requireNonNull ( value );
this.entries.put ( 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 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 );
}
public Object get ( final T tag )
{
return this.entries.get ( tag.getValue () );
}
@Override
public Optional
© 2015 - 2025 Weber Informatics LLC | Privacy Policy