View Javadoc

1   /**********************************************
2    * Copyright (C) 2011 Lukas laag
3    * This file is part of lib-gwt-file.
4    * 
5    * lib-gwt-file is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * lib-gwt-file is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   * 
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with lib-gwt-file.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  /**
19   * Documentation is adapted from W3C spec and content available from
20   * http://developer.mozilla.org under http://creativecommons.org/licenses/by-sa/2.5/
21   */
22  package org.vectomatic.file;
23  
24  import org.vectomatic.file.impl.SliceImpl;
25  
26  import com.google.gwt.core.client.GWT;
27  import com.google.gwt.core.client.JavaScriptObject;
28  
29  /**
30   * A Blob object represents a file-like object of raw data. It's used to
31   * represent data that isn't necessarily in a JavaScript-native format. The
32   * {@link org.vectomatic.file.File} interface is based on it, inheriting the
33   * Blob's functionality and expanding it to support files on the user's system.
34   */
35  public class Blob extends JavaScriptObject {
36  	private static SliceImpl sliceImpl;
37  	/**
38  	 * Temporary fix because the File API slice methods are currently prefixed.
39  	 * @return An implementation of the slice methods
40  	 */
41  	private static final SliceImpl getSliceImpl() {
42  		if (sliceImpl == null) {
43  			sliceImpl = GWT.create(SliceImpl.class);
44  		}
45  		return sliceImpl;
46  	}
47  	
48  	protected Blob() {
49  	}
50  
51  	/**
52  	 * The size, in bytes, of the data contained in the Blob object.
53  	 * 
54  	 * @return The size, in bytes, of the data contained in the Blob object.
55  	 */
56  	public final long getSize() {
57  		String size = getSize_();
58  		return Long.parseLong(size);
59  	}
60  	
61  	private final native String getSize_() /*-{
62        return "" + this.size;
63  	}-*/;
64  	
65  	
66  	/**
67  	 * An ASCII-encoded string, in all lower case, indicating the MIME type of
68  	 * the data contained in the Blob. If the type is unknown, this string is
69  	 * empty.
70  	 * 
71  	 * @return The MIME type of the data contained in the Blob
72  	 */
73  	public final native String getType() /*-{
74        return this.type;
75  	}-*/;
76  	
77  	/**
78  	 * Returns a new Blob object containing a full copy of the data in the
79  	 * source Blob.
80  	 * 
81  	 * @return The new Blob object
82  	 */
83  	public final Blob slice() {
84        return getSliceImpl().slice(this);
85  	}
86  
87  	/**
88  	 * Returns a new Blob object containing the data in the specified range of
89  	 * bytes of the source Blob.
90  	 * 
91  	 * @param start
92  	 *            An index into the Blob indicating the first byte to copy into
93  	 *            the new Blob. If you specify a negative value, it's treated as
94  	 *            an offset from the end of the string toward the beginning (for
95  	 *            example, -10 would be the 10th from last byte in the Blob).
96  	 * @return The new Blob object
97  	 */
98  	public final Blob slice(long start) {
99        return getSliceImpl().slice(this, start);
100 	}
101 	
102 	/**
103 	 * Returns a new Blob object containing the data in the specified range of
104 	 * bytes of the source Blob.
105 	 * 
106 	 * @param start
107 	 *            An index into the Blob indicating the first byte to copy into
108 	 *            the new Blob. If you specify a negative value, it's treated as
109 	 *            an offset from the end of the string toward the beginning (for
110 	 *            example, -10 would be the 10th from last byte in the Blob).
111 	 * @param end
112 	 *            An index into the Blob indicating the last byte to copy into
113 	 *            the new Blob. If you specify a negative value, it's treated as
114 	 *            an offset from the end of the string toward the beginning (for
115 	 *            example, -10 would be the 10th from last byte in the Blob).
116 	 * @return The new Blob object
117 	 */
118 	public final Blob slice(long start, long end) {
119       return getSliceImpl().slice(this, start, end);
120 	}
121 	
122 	/**
123 	 * Returns a new Blob object containing the data in the specified range of
124 	 * bytes of the source Blob.
125 	 * 
126 	 * @param start
127 	 *            An index into the Blob indicating the first byte to copy into
128 	 *            the new Blob. If you specify a negative value, it's treated as
129 	 *            an offset from the end of the string toward the beginning (for
130 	 *            example, -10 would be the 10th from last byte in the Blob).
131 	 * @param end
132 	 *            An index into the Blob indicating the last byte to copy into
133 	 *            the new Blob. If you specify a negative value, it's treated as
134 	 *            an offset from the end of the string toward the beginning (for
135 	 *            example, -10 would be the 10th from last byte in the Blob).
136 	 * @param contentType
137 	 *            The content type to assign to the new Blob; this will be the
138 	 *            value of its type property.
139 	 * @return The new Blob object
140 	 */
141 	public final Blob slice(long start, long end, String contentType) {
142       return getSliceImpl().slice(this, start, end, contentType);
143 	}
144 
145 	/**
146 	 * @deprecated Replaced by {@link FileUtils#createObjectURL(org.vectomatic.file.Blob)}
147 	 */
148 	@Deprecated
149 	public final native String createObjectURL() /*-{
150 	  return $wnd.URL.createObjectURL(this);
151 	}-*/;
152 
153 	/**
154 	 * @deprecated Replaced by {@link FileUtils#revokeObjectURL(java.lang.String)}
155 	 */
156 	@Deprecated
157 	public final native void revokeObjectURL(String url) /*-{
158 	  $wnd.URL.revokeObjectURL(url);
159 	}-*/;
160 }