本文整理汇总了Java中java.io.ByteArrayInputStream类的典型用法代码示例。如果您正苦于以下问题:Java ByteArrayInputStream类的具体用法?Java ByteArrayInputStream怎么用?Java ByteArrayInputStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ByteArrayInputStream类属于java.io包,在下文中一共展示了ByteArrayInputStream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: decompress
import java.io.ByteArrayInputStream; //导入依赖的package包/类
public static String decompress(byte[] compressed) {
if(compressed==null) return null;
ByteArrayInputStream bis = new ByteArrayInputStream(compressed);
try {
GZIPInputStream gis = new GZIPInputStream(bis);
BufferedReader br = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line;
while((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
gis.close();
bis.close();
return sb.toString();
}catch (IOException ex){
ex.printStackTrace();
}
return null;
}
示例2: exportTemplate
import java.io.ByteArrayInputStream; //导入依赖的package包/类
/**
* 导出当前网站当前的模版文件,包含模版页面,模版变量、栏目
* @throws FileNotFoundException
* @throws UnsupportedEncodingException
*/
@RequestMapping("exportTemplate")
public void exportTemplate(HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException, UnsupportedEncodingException {
BaseVO vo = templateService.exportTemplate(request);
String fileName = "template"+DateUtil.currentDate("yyyyMMdd_HHmm")+".wscso".toString(); // 文件的默认保存名
//读到流中
InputStream inStream = new ByteArrayInputStream(vo.getInfo().getBytes("UTF-8"));
AliyunLog.addActionLog(getSiteId(), "导出当前网站当前的模版文件");
// 设置输出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
// 循环取出流中的数据
byte[] b = new byte[1000];
int len;
try {
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
示例3: getResizedMedia
import java.io.ByteArrayInputStream; //导入依赖的package包/类
public MediaStream getResizedMedia(@NonNull Context context,
@NonNull MasterSecret masterSecret,
@NonNull Attachment attachment)
throws IOException
{
if (!canResize(attachment)) {
throw new UnsupportedOperationException("Cannot resize this content type");
}
try {
// XXX - This is loading everything into memory! We want the send path to be stream-like.
return new MediaStream(new ByteArrayInputStream(BitmapUtil.createScaledBytes(context, new DecryptableUri(masterSecret, attachment.getDataUri()), this)),
ContentType.IMAGE_JPEG);
} catch (BitmapDecodingException e) {
throw new IOException(e);
}
}
示例4: testLoadWithoutEncoding
import java.io.ByteArrayInputStream; //导入依赖的package包/类
/**
* Test loadFromXML with a document that does not have an encoding declaration
*/
static void testLoadWithoutEncoding() throws IOException {
System.out.println("testLoadWithoutEncoding");
Properties expected = new Properties();
expected.put("foo", "bar");
String s = "<?xml version=\"1.0\"?>" +
"<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">" +
"<properties>" +
"<entry key=\"foo\">bar</entry>" +
"</properties>";
ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes("UTF-8"));
Properties props = new Properties();
props.loadFromXML(in);
if (!props.equals(expected)) {
System.err.println("loaded: " + props + ", expected: " + expected);
throw new RuntimeException("Test failed");
}
}
示例5: shouldSerializeCorrectly
import java.io.ByteArrayInputStream; //导入依赖的package包/类
@Test
public void shouldSerializeCorrectly() throws Exception {
Transaction transaction = new Transaction("TRANSACTION_TOKEN", "RECOVERY_CODE", "OTP_SECRET");
// save
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(transaction);
objectOutputStream.close();
outputStream.close();
byte[] binaryData = outputStream.toByteArray();
// restore
ByteArrayInputStream inputStream = new ByteArrayInputStream(binaryData);
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
Transaction restoredTransaction = (Transaction) objectInputStream.readObject();
objectInputStream.close();
inputStream.close();
assertThat(restoredTransaction.getTransactionToken(), is(equalTo("TRANSACTION_TOKEN")));
assertThat(restoredTransaction.getRecoveryCode(), is(equalTo("RECOVERY_CODE")));
}
示例6: byteArrayToObject
import java.io.ByteArrayInputStream; //导入依赖的package包/类
/**
*
* 功能描述:自己数组转换成对象 <br>
* 〈功能详细描述〉
*
* @param bytes
* bytes
* @return object
* @throws IOException
* ioexception
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public static Object byteArrayToObject(byte[] bytes) throws IOException {
if (bytes == null || bytes.length <= 0) {
return null;
}
Object obj = null;
try {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(bis));
obj = ois.readObject();
bis.close();
ois.close();
} catch (ClassNotFoundException e) {
logger.error(e.getMessage());
}
return obj;
}
示例7: testSerialization
import java.io.ByteArrayInputStream; //导入依赖的package包/类
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization() {
SignalRenderer r1 = new SignalRenderer();
SignalRenderer r2 = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(buffer);
out.writeObject(r1);
out.close();
ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
r2 = (SignalRenderer) in.readObject();
in.close();
}
catch (Exception e) {
System.out.println(e.toString());
}
assertEquals(r1, r2);
}
示例8: newData
import java.io.ByteArrayInputStream; //导入依赖的package包/类
@Override
public void newData(byte[] bytes) {
if(Dashboard.visionInitialized()){
Mat m = CvProcessing.byteArray2Mat(bytes);
Dashboard.setForVision(m);
}
if(mode == DisplayMode.Normal){
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
Iterator<?> readers = ImageIO.getImageReadersByFormatName("jpeg");
ImageReader reader = (ImageReader) readers.next();
Object source = bis;
try {
ImageInputStream iis = ImageIO.createImageInputStream(source);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
BufferedImage image = reader.read(0, param);
setImage(image);
} catch (IOException e) {
}
}
}
示例9: store
import java.io.ByteArrayInputStream; //导入依赖的package包/类
@Override
public void store(BuildCacheKey key, BuildCacheEntryWriter writer) {
logger.info("Start storing cache entry '{}' in S3 bucket", key.getHashCode());
ObjectMetadata meta = new ObjectMetadata();
meta.setContentType(BUILD_CACHE_CONTENT_TYPE);
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
writer.writeTo(os);
meta.setContentLength(os.size());
try (InputStream is = new ByteArrayInputStream(os.toByteArray())) {
PutObjectRequest request = getPutObjectRequest(key, meta, is);
if(this.reducedRedundancy) {
request.withStorageClass(StorageClass.ReducedRedundancy);
}
s3.putObject(request);
}
} catch (IOException e) {
throw new BuildCacheException("Error while storing cache object in S3 bucket", e);
}
}
示例10: testSerialization
import java.io.ByteArrayInputStream; //导入依赖的package包/类
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization() {
MyComparableObjectSeries s1 = new MyComparableObjectSeries("A");
s1.add(new Integer(1), "ABC");
MyComparableObjectSeries s2 = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(buffer);
out.writeObject(s1);
out.close();
ObjectInput in = new ObjectInputStream(
new ByteArrayInputStream(buffer.toByteArray()));
s2 = (MyComparableObjectSeries) in.readObject();
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
assertEquals(s1, s2);
}
示例11: getTransferData
import java.io.ByteArrayInputStream; //导入依赖的package包/类
@Override
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException {
if (flavor.equals(FLAVORS[0])) { // RTF
return new ByteArrayInputStream(data==null ? new byte[0] : data);
}
else if (flavor.equals(FLAVORS[1])) { // stringFlavor
return data==null ? "" : RtfToText.getPlainText(data);
}
else if (flavor.equals(FLAVORS[2])) { // plainTextFlavor (deprecated)
String text = ""; // Valid if data==null
if (data!=null) {
text = RtfToText.getPlainText(data);
}
return new StringReader(text);
}
else {
throw new UnsupportedFlavorException(flavor);
}
}
示例12: mergeFromJson
import java.io.ByteArrayInputStream; //导入依赖的package包/类
private void mergeFromJson(
String json, boolean ignoringUnknownFields, Builder builder, Message... additionalTypes)
throws IOException {
MessageMarshaller.Builder marshallerBuilder =
MessageMarshaller.builder()
.register(builder.getDefaultInstanceForType())
.ignoringUnknownFields(ignoringUnknownFields);
for (Message prototype : additionalTypes) {
marshallerBuilder.register(prototype);
}
MessageMarshaller marshaller = marshallerBuilder.build();
marshaller.mergeValue(json, builder);
Message.Builder builder2 = builder.build().newBuilderForType();
marshaller.mergeValue(json.getBytes(StandardCharsets.UTF_8), builder2);
assertThat(builder2.build()).isEqualTo(builder.build());
Message.Builder builder3 = builder.build().newBuilderForType();
try (ByteArrayInputStream bis =
new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8))) {
marshaller.mergeValue(bis, builder3);
}
assertThat(builder3.build()).isEqualTo(builder.build());
}
示例13: assertObjectWithType
import java.io.ByteArrayInputStream; //导入依赖的package包/类
<T> void assertObjectWithType(T data, Class<T> clazz) throws Exception {
ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
objectOutput.writeObject(data);
objectOutput.flushBuffer();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
byteArrayOutputStream.toByteArray());
ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
assertEquals(data, (T) deserialize.readObject(clazz));
try {
deserialize.readObject(clazz);
fail();
} catch (IOException expected) {
}
}
示例14: createFileDeleteIfExists
import java.io.ByteArrayInputStream; //导入依赖的package包/类
/**
* Create a file in a folder with the specified name and content
*
* @param fullpath
* @param filename
* @param content
* @throws CoreException
* @throws InterruptedException
*/
public static IFile createFileDeleteIfExists(String fullpath, String filename, String content,
IProgressMonitor monitor) throws CoreException, InterruptedException {
SubMonitor subMonitor = SubMonitor.convert(monitor, 100);
subMonitor.setTaskName("Create file delete if it exists " + fullpath);
IFile newFile;
try {
IWorkspaceRoot wroot = ResourcesPlugin.getWorkspace().getRoot();
IContainer container = (IContainer) wroot.findMember(new Path(fullpath));
newFile = container.getFile(new Path(filename));
if (newFile.exists()) {
JDTManager.rename(newFile, new NullProgressMonitor());
newFile.delete(true, new NullProgressMonitor());
}
subMonitor.split(30);
byte[] source = content.getBytes(Charset.forName("UTF-8"));
newFile.create(new ByteArrayInputStream(source), true, new NullProgressMonitor());
subMonitor.split(70);
} finally {
subMonitor.done();
}
return newFile;
}
示例15: onSuccess
import java.io.ByteArrayInputStream; //导入依赖的package包/类
@Override
public void onSuccess(int code, ByteArrayInputStream is, Object[] args) {
try {
Result res = XmlUtils.toBean(ResultBean.class, is).getResult();
if (res.OK()) {
AppContext.showToastShort(R.string.delete_success);
mAdapter.removeItem(args[0]);
// setTweetCommentCount();
} else {
AppContext.showToastShort(res.getErrorMessage());
}
} catch (Exception e) {
e.printStackTrace();
onFailure(code, e.getMessage(), args);
}
}