001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.feature.local.list;
031
032import java.io.BufferedInputStream;
033import java.io.DataOutput;
034import java.io.IOException;
035import java.io.InputStream;
036import java.io.PrintWriter;
037import java.lang.reflect.Array;
038
039import org.openimaj.feature.local.LocalFeature;
040import org.openimaj.io.IOUtils;
041import org.openimaj.util.list.AbstractStreamBackedList;
042
043/**
044 * A list of {@link LocalFeature}s backed by an input stream. The list is
045 * read-only, and can only be read in order (i.e. random access is not
046 * possible).
047 * 
048 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
049 * 
050 * @param <T>
051 *            the type of local feature
052 */
053public class StreamLocalFeatureList<T extends LocalFeature<?, ?>> extends AbstractStreamBackedList<T>
054                implements
055                LocalFeatureList<T>
056{
057        int veclen;
058
059        protected StreamLocalFeatureList(InputStream stream, int size, boolean isBinary, int headerLength, int recordLength,
060                        int veclen, Class<T> clz)
061        {
062                super(stream, size, isBinary, headerLength, recordLength, clz);
063                this.veclen = veclen;
064        }
065
066        /**
067         * Construct a new StreamLocalFeatureList from the given input stream.
068         * 
069         * @param <T>
070         *            the type of local feature
071         * @param stream
072         *            the input stream
073         * @param clz
074         *            the class of local feature to read
075         * @return a new list
076         * @throws IOException
077         *             if an error occurs reading from the stream
078         */
079        public static <T extends LocalFeature<?, ?>> StreamLocalFeatureList<T> read(InputStream stream, Class<T> clz)
080                        throws IOException
081        {
082                return read(new BufferedInputStream(stream), clz);
083        }
084
085        /**
086         * Construct a new StreamLocalFeatureList from the given input stream.
087         * 
088         * @param <T>
089         *            the type of local feature
090         * @param stream
091         *            the input stream
092         * @param clz
093         *            the class of local feature to read
094         * @return a new list
095         * @throws IOException
096         *             if an error occurs reading from the stream
097         */
098        public static <T extends LocalFeature<?, ?>> StreamLocalFeatureList<T> read(BufferedInputStream stream, Class<T> clz)
099                        throws IOException
100        {
101                final boolean isBinary = IOUtils.isBinary(stream, LocalFeatureList.BINARY_HEADER);
102
103                // read header
104                final int[] header = LocalFeatureListUtils.readHeader(stream, isBinary, false);
105                final int size = header[0];
106                final int veclen = header[1];
107                final int headerLength = header[2];
108
109                final int recordLength = veclen + 4 * 4;
110
111                return new StreamLocalFeatureList<T>(stream, size, isBinary, headerLength, recordLength, veclen, clz);
112        }
113
114        @SuppressWarnings("unchecked")
115        @Override
116        public <Q> Q[] asDataArray(Q[] a) {
117                if (a.length < size()) {
118                        a = (Q[]) Array.newInstance(a.getClass().getComponentType(), size());
119                }
120
121                int i = 0;
122                for (final T t : this) {
123                        a[i++] = (Q) t.getFeatureVector().getVector();
124                }
125
126                return a;
127        }
128
129        @Override
130        public int vecLength() {
131                return veclen;
132        }
133
134        @Override
135        public void writeBinary(DataOutput out) throws IOException {
136                LocalFeatureListUtils.writeBinary(out, this);
137        }
138
139        @Override
140        public void writeASCII(PrintWriter out) throws IOException {
141                LocalFeatureListUtils.writeASCII(out, this);
142        }
143
144        @Override
145        public byte[] binaryHeader() {
146                return LocalFeatureList.BINARY_HEADER;
147        }
148
149        @Override
150        public String asciiHeader() {
151                return "";
152        }
153
154        @Override
155        protected T newElementInstance() {
156                return LocalFeatureListUtils.newInstance(clz, this.veclen);
157        }
158
159        @Override
160        public MemoryLocalFeatureList<T> subList(int fromIndex, int toIndex) {
161                return new MemoryLocalFeatureList<T>(super.subList(fromIndex, toIndex));
162        }
163}