View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.graph.property;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.io.Reader;
30  import net.jcip.annotations.ThreadSafe;
31  
32  /**
33   * A factory for creating {@link Binary} instances. This interface extends the {@link ValueFactory} generic interface and adds
34   * specific methods for creating binary objects.
35   */
36  @ThreadSafe
37  public interface BinaryFactory extends ValueFactory<Binary> {
38  
39      /**
40       * Create a value from the binary content given by the supplied stream, the approximate length, and the SHA-1 secure hash of
41       * the content. If the secure hash is null, then a secure hash is computed from the content. If the secure hash is not null,
42       * it is assumed to be the hash for the content and may not be checked.
43       * 
44       * @param stream the stream containing the content to be used to create the value
45       * @param approximateLength the approximate length of the content (in bytes)
46       * @param secureHash the secure hash of the content in the <code>stream</code>; if null, the secure hash is computed from the
47       *        content, and if not null it is assumed to be the correct secure hash (and is not checked)
48       * @return the value, or null if the supplied stream is null
49       * @throws ValueFormatException if the conversion from an input stream could not be performed
50       * @throws IoException If an unexpected problem occurs while accessing the supplied stream (such as an {@link IOException}).
51       * @throws IllegalArgumentException if the secure hash was discovered to be incorrect
52       */
53      Binary create( InputStream stream,
54                     long approximateLength,
55                     byte[] secureHash ) throws ValueFormatException, IoException;
56  
57      /**
58       * Create a value from the binary content given by the supplied reader, the approximate length, and the SHA-1 secure hash of
59       * the content. If the secure hash is null, then a secure hash is computed from the content. If the secure hash is not null,
60       * it is assumed to be the hash for the content and may not be checked.
61       * 
62       * @param reader the reader containing the content to be used to create the value
63       * @param approximateLength the approximate length of the content (in bytes)
64       * @param secureHash the secure hash of the content in the <code>stream</code>; if null, the secure hash is computed from the
65       *        content, and if not null it is assumed to be the correct secure hash (and is not checked)
66       * @return the value, or null if the supplied string is null
67       * @throws ValueFormatException if the conversion from a reader could not be performed
68       * @throws IoException If an unexpected problem occurs while accessing the supplied reader (such as an {@link IOException}).
69       * @throws IllegalArgumentException if the secure hash was discovered to be incorrect
70       */
71      Binary create( Reader reader,
72                     long approximateLength,
73                     byte[] secureHash ) throws ValueFormatException, IoException;
74  
75      /**
76       * Create a binary value from the given file.
77       * 
78       * @param file the file containing the content to be used
79       * @return the binary value, or null if the file parameter was null
80       * @throws ValueFormatException if the conversion from the file could not be performed
81       * @throws IoException If an unexpected problem occurs while accessing the supplied file (such as an {@link IOException}).
82       */
83      Binary create( File file ) throws ValueFormatException, IoException;
84  
85      /**
86       * Find an existing binary value given the supplied secure hash. If no such binary value exists, null is returned. This method
87       * can be used when the caller knows the secure hash (e.g., from a previously-held Binary object), and would like to reuse an
88       * existing binary value (if possible) rather than recreate the binary value by processing the stream contents. This is
89       * especially true when the size of the binary is quite large.
90       * 
91       * @param secureHash the secure hash of the binary content, which was probably {@link Binary#getHash() obtained} from a
92       *        previously-held {@link Binary} object; a null or empty value is allowed, but will always result in returning null
93       * @return the existing Binary value that has the same secure hash, or null if there is no such value available at this time
94       */
95      Binary find( byte[] secureHash );
96  }