001    /*
002     * JBoss DNA (http://www.jboss.org/dna)
003     * See the COPYRIGHT.txt file distributed with this work for information
004     * regarding copyright ownership.  Some portions may be licensed
005     * to Red Hat, Inc. under one or more contributor license agreements.
006     * See the AUTHORS.txt file in the distribution for a full listing of 
007     * individual contributors. 
008     *
009     * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010     * is licensed to you under the terms of the GNU Lesser General Public License as
011     * published by the Free Software Foundation; either version 2.1 of
012     * the License, or (at your option) any later version.
013     *
014     * JBoss DNA is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     * Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this software; if not, write to the Free
021     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023     */
024    package org.jboss.dna.graph.property;
025    
026    import java.io.File;
027    import java.io.IOException;
028    import java.io.InputStream;
029    import java.io.Reader;
030    
031    /**
032     * A factory for creating {@link Binary} instances. This interface extends the {@link ValueFactory} generic interface and adds
033     * specific methods for creating binary objects.
034     * 
035     * @author Randall Hauch
036     */
037    public interface BinaryFactory extends ValueFactory<Binary> {
038    
039        /**
040         * Create a value from the binary content given by the supplied stream, the approximate length, and the SHA-1 secure hash of
041         * the content. If the secure hash is null, then a secure hash is computed from the content. If the secure hash is not null,
042         * it is assumed to be the hash for the content and may not be checked.
043         * 
044         * @param stream the stream containing the content to be used to create the value
045         * @param approximateLength the approximate length of the content (in bytes)
046         * @param secureHash the secure hash of the content in the <code>stream</code>; if null, the secure hash is computed from the
047         *        content, and if not null it is assumed to be the correct secure hash (and is not checked)
048         * @return the value, or null if the supplied stream is null
049         * @throws ValueFormatException if the conversion from an input stream could not be performed
050         * @throws IoException If an unexpected problem occurs while accessing the supplied stream (such as an {@link IOException}).
051         * @throws IllegalArgumentException if the secure hash was discovered to be incorrect
052         */
053        Binary create( InputStream stream,
054                       long approximateLength,
055                       byte[] secureHash ) throws ValueFormatException, IoException;
056    
057        /**
058         * Create a value from the binary content given by the supplied reader, the approximate length, and the SHA-1 secure hash of
059         * the content. If the secure hash is null, then a secure hash is computed from the content. If the secure hash is not null,
060         * it is assumed to be the hash for the content and may not be checked.
061         * 
062         * @param reader the reader containing the content to be used to create the value
063         * @param approximateLength the approximate length of the content (in bytes)
064         * @param secureHash the secure hash of the content in the <code>stream</code>; if null, the secure hash is computed from the
065         *        content, and if not null it is assumed to be the correct secure hash (and is not checked)
066         * @return the value, or null if the supplied string is null
067         * @throws ValueFormatException if the conversion from a reader could not be performed
068         * @throws IoException If an unexpected problem occurs while accessing the supplied reader (such as an {@link IOException}).
069         * @throws IllegalArgumentException if the secure hash was discovered to be incorrect
070         */
071        Binary create( Reader reader,
072                       long approximateLength,
073                       byte[] secureHash ) throws ValueFormatException, IoException;
074    
075        /**
076         * Create a binary value from the given file.
077         * 
078         * @param file the file containing the content to be used
079         * @return the binary value, or null if the file parameter was null
080         * @throws ValueFormatException if the conversion from the file could not be performed
081         * @throws IoException If an unexpected problem occurs while accessing the supplied file (such as an {@link IOException}).
082         */
083        Binary create( File file ) throws ValueFormatException, IoException;
084    
085        /**
086         * Find an existing binary value given the supplied secure hash. If no such binary value exists, null is returned. This method
087         * can be used when the caller knows the secure hash (e.g., from a previously-held Binary object), and would like to reuse an
088         * existing binary value (if possible) rather than recreate the binary value by processing the stream contents. This is
089         * especially true when the size of the binary is quite large.
090         * 
091         * @param secureHash the secure hash of the binary content, which was probably {@link Binary#getHash() obtained} from a
092         *        previously-held {@link Binary} object; a null or empty value is allowed, but will always result in returning null
093         * @return the existing Binary value that has the same secure hash, or null if there is no such value available at this time
094         */
095        Binary find( byte[] secureHash );
096    }