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