/** * Construct an SObject with key and input stream. Note unlike sobject * constructed with String, byte array or file, the sobject constructed * with input stream can only be read for one time. If the program * tries to access the Sobject the second time, it will encountered an * {@link UnexpectedIOException}. Another limit of this sobject is it * does not support {@link org.osgl.storage.ISObject#getLength()} method * <p> * <p>If it needs to construct an SObject without these limits from * an input stream, then it shall first read the inputstream into * a bytearray, and use the byte array to construct the sobject like * following code</p> * <p> * <pre><code> * InputStream is = ... * ... * ISObject sobj = SObject.of(IO.readContent(is)) * </code><pre> */ public static SObject of(String key, InputStream is) { try { return new InputStreamSObject(key, $.requireNotNull(is)); } catch (Exception e) { return getInvalidObject(key, e); } }
/** * Construct an SObject with key and input stream. Note unlike sobject * constructed with String, byte array or file, the sobject constructed * with input stream can only be read for one time. If the program * tries to access the Sobject the second time, it will encountered an * {@link UnexpectedIOException}. Another limit of this sobject is it * does not support {@link org.osgl.storage.ISObject#getLength()} method * <p> * <p>If it needs to construct an SObject without these limits from * an input stream, then it shall first read the inputstream into * a bytearray, and use the byte array to construct the sobject like * following code</p> * <p> * <pre><code> * InputStream is = ... * ... * ISObject sobj = SObject.of(IO.readContent(is)) * </code><pre> */ public static SObject of(String key, InputStream is) { try { return new InputStreamSObject(key, $.requireNotNull(is)); } catch (Exception e) { return getInvalidObject(key, e); } }
/** * Construct an SObject with key and file specified * * @see #of(String, File, Map) */ public static SObject of(String key, File file) { if (file.canRead() && file.isFile()) { SObject sobj = new FileSObject(key, file); String fileName = file.getName(); sobj.setAttribute(ATTR_FILE_NAME, file.getName()); String fileExtension = S.fileExtension(fileName); MimeType mimeType = MimeType.findByFileExtension(fileExtension); String type = null != mimeType ? mimeType.type() : null; sobj.setAttribute(ATTR_CONTENT_TYPE, type); sobj.setAttribute(ATTR_CONTENT_LENGTH, S.string(file.length())); return sobj; } else { return getInvalidObject(key, new IOException("File is a directory or not readable")); } }
/** * Construct an SObject with key and file specified * * @see #of(String, File, Map) */ public static SObject of(String key, File file) { if (file.canRead() && file.isFile()) { SObject sobj = new FileSObject(key, file); String fileName = file.getName(); sobj.setAttribute(ATTR_FILE_NAME, file.getName()); String fileExtension = S.fileExtension(fileName); MimeType mimeType = MimeType.findByFileExtension(fileExtension); String type = null != mimeType ? mimeType.type() : null; sobj.setAttribute(ATTR_CONTENT_TYPE, type); sobj.setAttribute(ATTR_CONTENT_LENGTH, S.string(file.length())); return sobj; } else { return getInvalidObject(key, new IOException("File is a directory or not readable")); } }