001/* 002 * Copyright (c) 2006-2011 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 * bstefanescu 011 */ 012package org.nuxeo.ecm.automation.client.model; 013 014import java.io.ByteArrayInputStream; 015import java.io.IOException; 016import java.io.InputStream; 017 018/** 019 * An in memory bob containing a string. 020 * 021 * @author bstefanescu 022 */ 023public class StringBlob extends Blob { 024 025 private static final long serialVersionUID = -7170366401800302228L; 026 027 /** content */ 028 protected final String content; 029 030 protected String charset; 031 032 public StringBlob(String content) { 033 this.content = content; 034 } 035 036 /** 037 * Creates a <code>StringBlob</code> that is used in the Blob.Attach call 038 * 039 * @param fileName Name that is used to save the file as 040 * @param content Base64 encoded content 041 */ 042 public StringBlob(String fileName, String content) { 043 super(fileName, null); 044 this.content = content; 045 } 046 047 /** 048 * Creates a <code>StringBlob</code> that is used in the Blob.Attach call 049 * 050 * @param fileName Name that is used to save the file as 051 * @param content Base64 encoded content 052 * @param mimeType Mime type to use for this content 053 */ 054 public StringBlob(String fileName, String content, String mimeType) { 055 super(fileName, mimeType); 056 this.content = content; 057 } 058 059 /** 060 * Set the charset to be used when to transform the content into a byte stream. If npt specified the default charset 061 * will be used. 062 * 063 * @param charset 064 */ 065 public void setCharset(String charset) { 066 this.charset = charset; 067 } 068 069 public String getCharset() { 070 return charset; 071 } 072 073 @Override 074 public InputStream getStream() throws IOException { 075 byte[] bytes = charset == null ? content.getBytes() : content.getBytes(charset); 076 return new ByteArrayInputStream(bytes); 077 } 078 079 @Override 080 public String toString() { 081 return content; 082 } 083 084 @Override 085 public int getLength() { 086 return content.length(); 087 } 088}