All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.vesalainen.io.CompressedInput Maven / Gradle / Ivy

/*
 * Copyright (C) 2015 tkv
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package org.vesalainen.io;

import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.Spliterators.AbstractSpliterator;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.vesalainen.util.BitArray;

/**
 * A class for reading compressed data.
 * @author tkv
 * @param 
 */
public class CompressedInput extends CompressedIO
{
    private final InputStream in;
    private final DataInputStream dis;
    private final ArrayInputStream array;
    private final DataInputStream data;
    public CompressedInput(InputStream in, T obj) throws IOException
    {
        super(obj);
        
        this.in = in;
        this.dis = new DataInputStream(in);
        
        String classname = dis.readUTF();
        if (!cls.getName().equals(classname))
        {
            throw new IOException("Data input from "+classname+" not from "+cls);
        }
        short fieldCount = dis.readShort();
        Field[] flds = cls.getFields();
        if (fieldCount != flds.length)
        {
            throw new IOException("Field count "+fieldCount+" differs from "+fields.length);
        }
        fields = new Field[fieldCount];
        for (int ii=0;ii "+fld);
            }
            fields[ii] = fld;
        }
        long mostSigBits = dis.readLong();
        long leastSigBits = dis.readLong();
        uuid = new UUID(mostSigBits, leastSigBits);
        for (Field field : fields)
        {
            Class type = field.getType();
            bytes += getBytes(type.getName());
        }
        buf1 = new byte[bytes];
        array = new ArrayInputStream(buf1);
        data = new DataInputStream(array);
        bitArray = new BitArray(bytes);
        bits = bitArray.getArray();
        
    }
    /**
     * Update objects fields. Throws EOFException when eof.
     * @return Compression rate
     * @throws IOException 
     */
    public float read() throws IOException
    {
        dis.readFully(bits);

        int cnt = 0;
        for (int ii=0;iiNote that streamed objects are always the same as given in 
     * constructor!
     * @return 
     */
    public Stream stream()
    {
        return StreamSupport.stream(new SpliteratorImpl(), false);
    }
    private class SpliteratorImpl extends AbstractSpliterator
    {

        public SpliteratorImpl()
        {
            super(Long.MAX_VALUE, 0);
        }

        @Override
        public boolean tryAdvance(Consumer action)
        {
            try
            {
                read();
                action.accept(obj);
                return true;
            }
            catch (EOFException ex)
            {
                return false;
            }
            catch (IOException ex)
            {
                throw new IllegalArgumentException(ex);
            }
        }
        
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy