001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Florent Guillaume 018 */ 019package org.nuxeo.ecm.core.api; 020 021import java.io.File; 022import java.io.IOException; 023import java.io.InputStream; 024 025import org.nuxeo.ecm.core.api.impl.blob.ByteArrayBlob; 026import org.nuxeo.ecm.core.api.impl.blob.FileBlob; 027import org.nuxeo.ecm.core.api.impl.blob.StringBlob; 028 029/** 030 * This class consists exclusively of static methods that operate on {@link Blob}s. 031 */ 032public class Blobs { 033 034 private Blobs() { 035 } 036 037 /** 038 * Creates a {@link Blob} backed by the given {@link File}. 039 * 040 * @param file file 041 */ 042 public static Blob createBlob(File file) throws IOException { 043 return new FileBlob(file, null, null); 044 } 045 046 /** 047 * Creates a {@link Blob} backed by the given {@link File}. 048 * 049 * @param file file 050 * @param mimeType the MIME type 051 */ 052 public static Blob createBlob(File file, String mimeType) throws IOException { 053 return new FileBlob(file, mimeType, null); 054 } 055 056 /** 057 * Creates a {@link Blob} backed by the given {@link File}. 058 * 059 * @param file file 060 * @param mimeType the MIME type 061 * @param encoding the encoding 062 */ 063 public static Blob createBlob(File file, String mimeType, String encoding) throws IOException { 064 return new FileBlob(file, mimeType, encoding); 065 } 066 067 /** 068 * Creates a {@link Blob} backed by the given {@link File}. 069 * 070 * @param file file 071 * @param mimeType the MIME type 072 * @param encoding the encoding 073 * @param filename the blob filename 074 */ 075 public static Blob createBlob(File file, String mimeType, String encoding, String filename) throws IOException { 076 return new FileBlob(file, mimeType, encoding, filename, null); 077 } 078 079 /** 080 * Creates a {@link Blob} backed by an empty temporary {@link File} with the given extension. 081 * 082 * @param ext the extension 083 */ 084 public static Blob createBlobWithExtension(String ext) throws IOException { 085 return new FileBlob(ext); 086 } 087 088 /** 089 * Creates a {@link Blob} from an {@link InputStream}, by saving it to a temporary file. 090 * <p> 091 * The input stream is closed. 092 * 093 * @param in the input stream, which is closed after use 094 */ 095 public static Blob createBlob(InputStream in) throws IOException { 096 return new FileBlob(in, null, null, null); 097 } 098 099 /** 100 * Creates a {@link Blob} from an {@link InputStream}, by saving it to a temporary file. 101 * <p> 102 * The input stream is closed. 103 * 104 * @param in the input stream, which is closed after use 105 * @param mimeType the MIME type 106 */ 107 public static Blob createBlob(InputStream in, String mimeType) throws IOException { 108 return new FileBlob(in, mimeType, null, null); 109 } 110 111 /** 112 * Creates a {@link Blob} from an {@link InputStream}, by saving it to a temporary file. 113 * <p> 114 * The input stream is closed. 115 * 116 * @param in the input stream, which is closed after use 117 * @param mimeType the MIME type 118 * @param encoding the encoding 119 */ 120 public static Blob createBlob(InputStream in, String mimeType, String encoding) throws IOException { 121 return new FileBlob(in, mimeType, encoding, null); 122 } 123 124 /** 125 * Creates a {@link Blob} from an {@link InputStream}, by saving it to a temporary file. 126 * <p> 127 * The input stream is closed. 128 * 129 * @param in the input stream, which is closed after use 130 * @param mimeType the MIME type 131 * @param encoding the encoding 132 * @param tmpDir the temporary directory for file creation 133 */ 134 public static Blob createBlob(InputStream in, String mimeType, String encoding, File tmpDir) throws IOException { 135 return new FileBlob(in, mimeType, encoding, tmpDir); 136 } 137 138 /** 139 * Creates a {@link Blob} backed by the given bytes array. 140 * 141 * @param bytes the bytes array 142 */ 143 public static Blob createBlob(byte[] bytes) { 144 return new ByteArrayBlob(bytes, null, null); 145 } 146 147 /** 148 * Creates a {@link Blob} backed by the given bytes array. 149 * 150 * @param bytes the bytes array 151 * @param mimeType the MIME type 152 */ 153 public static Blob createBlob(byte[] bytes, String mimeType) throws IOException { 154 return new ByteArrayBlob(bytes, mimeType, null); 155 } 156 157 /** 158 * Creates a {@link Blob} backed by the given bytes array. 159 * 160 * @param bytes the bytes array 161 * @param mimeType the MIME type 162 * @param encoding the encoding 163 */ 164 public static Blob createBlob(byte[] bytes, String mimeType, String encoding) throws IOException { 165 return new ByteArrayBlob(bytes, mimeType, encoding); 166 } 167 168 /** 169 * Creates a {@link Blob} backed by the given string, with MIME type "text/plain" and encoding "UTF-8". 170 * 171 * @param string the string 172 */ 173 public static Blob createBlob(String string) { 174 return new StringBlob(string); // "text/plain", "UTF-8" 175 } 176 177 /** 178 * Creates a {@link Blob} backed by the given string, with encoding "UTF-8". 179 * 180 * @param string the string 181 * @param mimeType the MIME type 182 */ 183 public static Blob createBlob(String string, String mimeType) { 184 return new StringBlob(string, mimeType); // "UTF-8" 185 } 186 187 /** 188 * Creates a {@link Blob} backed by the given string. 189 * 190 * @param string the string 191 * @param mimeType the MIME type 192 * @param encoding the encoding 193 */ 194 public static Blob createBlob(String string, String mimeType, String encoding) { 195 return new StringBlob(string, mimeType, encoding); 196 } 197 198 /** 199 * Creates a {@link Blob} backed by the given string. 200 * 201 * @param string the string 202 * @param mimeType the MIME type 203 * @param encoding the encoding 204 * @param filename the blob filename 205 */ 206 public static Blob createBlob(String string, String mimeType, String encoding, String filename) { 207 return new StringBlob(string, mimeType, encoding, filename); 208 } 209 210}