org.mule.runtime.core.api.util.IOUtils.toByteArray()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(9.1k)|赞(0)|评价(0)|浏览(363)

本文整理了Java中org.mule.runtime.core.api.util.IOUtils.toByteArray()方法的一些代码示例,展示了IOUtils.toByteArray()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。IOUtils.toByteArray()方法的具体详情如下:
包路径:org.mule.runtime.core.api.util.IOUtils
类名称:IOUtils
方法名:toByteArray

IOUtils.toByteArray介绍

[英]This method wraps org.apache.commons.io.IOUtils' toByteArray(InputStream) method but catches any IOException and wraps it into a RuntimeException.
[中]这个方法包装了组织。阿帕奇。平民木卫一。IOUtils'toByteArray(InputStream)方法,但捕获任何IOException并将其包装为RuntimeException。

代码示例

代码示例来源:origin: mulesoft/mule

public static boolean compare(InputStream input1, InputStream input2) {
  byte[] bytes1 = IOUtils.toByteArray(input1);
  byte[] bytes2 = IOUtils.toByteArray(input2);
  return Arrays.equals(bytes1, bytes2);
 }
}

代码示例来源:origin: mulesoft/mule

private <T> T createCustomInstance(String classLocation) {
 InputStream classStream = null;
 try {
  classStream = this.getClass().getResourceAsStream(classLocation);
  byte[] classBytes = IOUtils.toByteArray(classStream);
  classStream.close();
  Class clazz = this.defineClass(null, classBytes, 0, classBytes.length);
  return (T) clazz.newInstance();
 } catch (Exception e) {
  throw new RuntimeException("Can not create instance from resource: " + classLocation, e);
 } finally {
  closeQuietly(classStream);
 }
}

代码示例来源:origin: mulesoft/mule

/**
 * {@inheritDoc}
 */
@Override
protected byte[] doSerialize(Object object) throws Exception {
 //TODO: MULE-11939
 if (object instanceof CursorStreamProvider) {
  try (CursorStream cursor = ((CursorStreamProvider) object).openCursor()) {
   object = toByteArray(cursor);
  }
 }
 validateForSerialization(object);
 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(512);
 try (ObjectOutputStream out = new ArtifactClassLoaderObjectOutputStream(classLoaderRepository, outputStream)) {
  out.writeObject(object);
 } catch (IOException ex) {
  throw new SerializationException("Cannot serialize object", ex);
 }
 return outputStream.toByteArray();
}

代码示例来源:origin: mulesoft/mule

public HttpMessage(HttpExchange httpExchange) {
 this.body = IOUtils.toByteArray(httpExchange.getRequestBody());
 ImmutableMultimap.Builder<String, String> headersBuilder = ImmutableMultimap.builder();
 Set<String> headerNames = httpExchange.getRequestHeaders().keySet();
 headerNames.stream()
   .forEach(headerName -> headersBuilder.putAll(headerName, httpExchange.getRequestHeaders().get(headerName)));
 this.headers = headersBuilder.build();
 uri = httpExchange.getRequestURI().getPath();
 queryParams = queryToMap(httpExchange.getRequestURI().getQuery());
}

代码示例来源:origin: mulesoft/mule

@Override
 public Class<?> loadClass(String name) throws ClassNotFoundException {
  if (Message.class.getName().equals(name)) {
   byte[] classBytes;
   try {
    classBytes = toByteArray(this.getClass().getResourceAsStream("/org/mule/runtime/api/message/Message.class"));
    return this.defineClass(null, classBytes, 0, classBytes.length);
   } catch (Exception e) {
    return super.loadClass(name);
   }
  } else {
   return super.loadClass(name);
  }
 }
};

代码示例来源:origin: mulesoft/mule

@Override
 public Class<?> loadClass(String name) throws ClassNotFoundException {
  if (Delegator.class.getName().equals(name)) {
   byte[] classBytes;
   try {
    classBytes =
      toByteArray(this.getClass().getResourceAsStream("/org/mule/runtime/core/internal/component/Delegator.class"));
    return this.defineClass(null, classBytes, 0, classBytes.length);
   } catch (Exception e) {
    return super.loadClass(name);
   }
  } else {
   return super.loadClass(name);
  }
 }
};

代码示例来源:origin: mulesoft/mule

@Override
public boolean compareResults(Object expected, Object result) {
 if (expected instanceof InputStream) {
  InputStream input = (InputStream) expected;
  byte[] bytes = IOUtils.toByteArray(input);
  return super.compareResults(bytes, result);
 }
 return super.compareResults(expected, result);
}

代码示例来源:origin: mulesoft/mule

public static Resource[] convert(ConfigResource[] resources) {
 Resource[] configResources = new Resource[resources.length];
 for (int i = 0; i < resources.length; i++) {
  ConfigResource resource = resources[i];
  if (resource.getUrl() != null) {
   configResources[i] = new UrlResource(resource.getUrl());
  } else {
   try {
    configResources[i] = new ByteArrayResource(IOUtils.toByteArray(resource.getInputStream()), resource.getResourceName());
   } catch (IOException e) {
    throw new RuntimeException(e);
   }
  }
 }
 return configResources;
}

代码示例来源:origin: mulesoft/mule

@Test
 public void decodeWithoutUnzipping() throws Exception {
  final String payload = RandomStringUtils.randomAlphabetic(1024);
  ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(payload.getBytes());
  GZIPCompressorInputStream gzipCompressorInputStream = new GZIPCompressorInputStream(byteArrayInputStream);

  String encoded = Base64.encodeBytes(IOUtils.toByteArray(gzipCompressorInputStream), Base64.DONT_BREAK_LINES);
  GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(Base64.decodeWithoutUnzipping(encoded)));

  assertThat(IOUtils.toString(gzipInputStream), is(payload));
 }
}

代码示例来源:origin: org.mule.services/mule-service-http

@Override
public byte[] getBytes() throws UnsupportedOperationException {
 return IOUtils.toByteArray(content);
}

代码示例来源:origin: org.mule.runtime/mule-core

/**
 * This method wraps {@link org.apache.commons.io.IOUtils}' <code>toByteArray(InputStream)</code> method but catches any
 * {@link IOException} and wraps it into a {@link RuntimeException}.
 */
public static byte[] toByteArray(CursorStreamProvider cursorStreamProvider) {
 try (InputStream input = cursorStreamProvider.openCursor()) {
  return toByteArray(input);
 } catch (IOException e) {
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: org.mule.runtime/mule-core

private byte[] handleStream(InputStream input) {
  try {
   return toByteArray(input);
  } finally {
   closeQuietly(input);
  }
 }
}

代码示例来源:origin: org.mule.runtime/mule-core-tests

public static boolean compare(InputStream input1, InputStream input2) {
  byte[] bytes1 = IOUtils.toByteArray(input1);
  byte[] bytes2 = IOUtils.toByteArray(input2);
  return Arrays.equals(bytes1, bytes2);
 }
}

代码示例来源:origin: org.mule.services/mule-service-scheduler

@Override
 public Class<?> loadClass(String name) throws ClassNotFoundException {
  if (Delegator.class.getName().equals(name)) {
   byte[] classBytes;
   try {
    classBytes =
      toByteArray(this.getClass().getResourceAsStream("/org/mule/service/scheduler/internal/util/Delegator.class"));
    return this.defineClass(null, classBytes, 0, classBytes.length);
   } catch (Exception e) {
    return super.loadClass(name);
   }
  } else {
   return super.loadClass(name);
  }
 }
};

代码示例来源:origin: org.mule.runtime/mule-core

/**
 * {@inheritDoc}
 */
@Override
protected byte[] doSerialize(Object object) throws Exception {
 //TODO: MULE-11939
 if (object instanceof CursorStreamProvider) {
  try (CursorStream cursor = ((CursorStreamProvider) object).openCursor()) {
   return org.apache.commons.lang3.SerializationUtils.serialize(toByteArray(cursor));
  }
 }
 validateForSerialization(object);
 return org.apache.commons.lang3.SerializationUtils.serialize((Serializable) object);
}

代码示例来源:origin: org.mule.runtime/mule-core-tests

@Override
public boolean compareResults(Object expected, Object result) {
 if (expected instanceof InputStream) {
  InputStream input = (InputStream) expected;
  byte[] bytes = IOUtils.toByteArray(input);
  return super.compareResults(bytes, result);
 }
 return super.compareResults(expected, result);
}

代码示例来源:origin: org.mule.runtime/mule-core

/**
 * {@inheritDoc}
 */
@Override
public void serialize(Object object, OutputStream out) throws SerializationException {
 if (object instanceof CursorStreamProvider) {
  try (CursorStream cursor = ((CursorStreamProvider) object).openCursor()) {
   doSerialize(toByteArray(cursor), out);
  } catch (IOException e) {
   throw new SerializationException(createStaticMessage("Could not serialize cursor stream"), e);
  }
 } else {
  doSerialize(object, out);
 }
}

代码示例来源:origin: org.mule.connectors/mule-ftp-connector

@Test
public void writeWithCustomEncoding() throws Exception {
 final String defaultEncoding = muleContext.getConfiguration().getDefaultEncoding();
 assertThat(defaultEncoding, is(notNullValue()));
 final String customEncoding =
   availableCharsets().keySet().stream().filter(encoding -> !encoding.equals(defaultEncoding)).findFirst().orElse(null);
 assertThat(customEncoding, is(notNullValue()));
 final String filename = "encoding.txt";
 doWrite("write", filename, HELLO_WORLD, CREATE_NEW, false, customEncoding);
 String path = Paths.get(testHarness.getWorkingDirectory()).resolve(filename).toString();
 InputStream content = (InputStream) readPath(path, false).getPayload().getValue();
 assertThat(Arrays.equals(toByteArray(content), HELLO_WORLD.getBytes(customEncoding)), is(true));
}

代码示例来源:origin: org.mule.runtime/mule-core-tests

@Test
 public void decodeWithoutUnzipping() throws Exception {
  final String payload = RandomStringUtils.randomAlphabetic(1024);
  ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(payload.getBytes());
  GZIPCompressorInputStream gzipCompressorInputStream = new GZIPCompressorInputStream(byteArrayInputStream);

  String encoded = Base64.encodeBytes(IOUtils.toByteArray(gzipCompressorInputStream), Base64.DONT_BREAK_LINES);
  GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(Base64.decodeWithoutUnzipping(encoded)));

  assertThat(IOUtils.toString(gzipInputStream), is(payload));
 }
}

代码示例来源:origin: org.mule.connectors/mule-ftp-connector

@Override
public InternalEvent process(InternalEvent event) throws MuleException {
 Collection<Message> messageList = (Collection<Message>) event.getMessage().getPayload().getValue();
 for (Message message : messageList) {
  fileContents.add(new String(toByteArray(((CursorStreamProvider) message.getPayload().getValue()).openCursor())));
 }
 return event;
}

相关文章