本文整理汇总了Java中java.io.ByteArrayOutputStream.toByteArray方法的典型用法代码示例。如果您正苦于以下问题:Java ByteArrayOutputStream.toByteArray方法的具体用法?Java ByteArrayOutputStream.toByteArray怎么用?Java ByteArrayOutputStream.toByteArray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.io.ByteArrayOutputStream
的用法示例。
在下文中一共展示了ByteArrayOutputStream.toByteArray方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception
{
RemotableInputStream remotableInputStream = new RemotableInputStream(InetAddress.getLocalHost().getHostName(), 7777, new ByteArrayInputStream("test".getBytes()));
for (int b = -1; (b = remotableInputStream.read()) != -1;)
{
System.out.println((char) b);
}
remotableInputStream = new RemotableInputStream(InetAddress.getLocalHost().getHostName(), 7777, new ByteArrayInputStream("test".getBytes()));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(remotableInputStream);
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
remotableInputStream = (RemotableInputStream) ois.readObject();
for (int b = -1; (b = remotableInputStream.read()) != -1;)
{
System.out.println((char) b);
}
remotableInputStream.close();
}
示例2: encryptObject
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
/**
* {@inheritDoc}
* <p/>
* Serializes and {@link #encrypt(String, AlgorithmParameters, byte[]) encrypts} the input data.
*/
@Override
public Pair<byte[], AlgorithmParameters> encryptObject(String keyAlias, AlgorithmParameters params, Object input)
{
try
{
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(input);
byte[] unencrypted = bos.toByteArray();
return encrypt(keyAlias, params, unencrypted);
}
catch (Exception e)
{
throw new AlfrescoRuntimeException("Failed to serialize or encrypt object", e);
}
}
示例3: testEmptyWorks
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
@Test
public void testEmptyWorks() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CountingOutputStream cos = new CountingOutputStream(baos);
DataOutputStream dos = new DataOutputStream(cos);
Codec codec = new CellCodec();
Codec.Encoder encoder = codec.getEncoder(dos);
encoder.flush();
dos.close();
long offset = cos.getCount();
assertEquals(0, offset);
CountingInputStream cis =
new CountingInputStream(new ByteArrayInputStream(baos.toByteArray()));
DataInputStream dis = new DataInputStream(cis);
Codec.Decoder decoder = codec.getDecoder(dis);
assertFalse(decoder.advance());
dis.close();
assertEquals(0, cis.getCount());
}
示例4: reproduce
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
/**
* Reproduce this.
*
* Generally, data is changed by pre-processors before mapping to bean object.
* Use this to prevent unexpected change of origin fixtureMap.
*
* @return reproduced data
*/
public FixtureMap reproduce() {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024 * 2);
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
oos.flush();
oos.close();
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
Object obj = ois.readObject();
FixtureMap dup = (FixtureMap) obj;
dup.setParent(getParent());
dup.setRoot(isRoot());
dup.setFixtureName(getFixtureName());
return dup;
} catch (Exception e) {
throw new FixtureFormatException(getFixtureName(), e);
}
}
示例5: toByteArray
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public static byte[] toByteArray(String hexData, boolean isHex) {
if (hexData == null || hexData.equals("")) {
return null;
}
if (!isHex) {
return hexData.getBytes();
}
hexData = hexData.replaceAll("\\s+", "");
String hexDigits = "0123456789ABCDEF";
ByteArrayOutputStream baos = new ByteArrayOutputStream(
hexData.length() / 2);
// 将每2位16进制整数组装成一个字节
for (int i = 0; i < hexData.length(); i += 2) {
baos.write((hexDigits.indexOf(hexData.charAt(i)) << 4 | hexDigits
.indexOf(hexData.charAt(i + 1))));
}
byte[] bytes = baos.toByteArray();
try {
baos.close();
} catch (IOException e) {
LogUtils.warn(e);
}
return bytes;
}
示例6: component
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
@Override
public SummaryResponse component(Components components) throws IOException {
if (components == null) {
return new SummaryResponseImpl();
}
ObjectMapper mapper = ObjectMapperHelper.get();
ByteArrayOutputStream out = new ByteArrayOutputStream();
mapper.writeValue(out, components);
ByteArrayInputStream content = new ByteArrayInputStream(out.toByteArray());
Map<String, String> headers = new HashMap<>();
XrayImpl.addContentTypeJsonHeader(headers);
HttpResponse response = xray.post("summary/component", headers, content);
return mapper.readValue(response.getEntity().getContent(), SummaryResponseImpl.class);
}
示例7: geometryToWKB
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
private byte[] geometryToWKB(Geometry geom) throws IOException
{
WKBWriter wkbWriter = new WKBWriter();
ByteArrayOutputStream bytesStream = new ByteArrayOutputStream();
wkbWriter.write(geom, new OutputStreamOutStream(bytesStream));
byte[] geomBytes = bytesStream.toByteArray();
bytesStream.close();
return geomBytes;
}
示例8: toByteArray
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
/**
* Converts the entirety of an {@link InputStream} to a byte array.
*
* @param inputStream the {@link InputStream} to be read. The input stream is not closed by this
* method.
* @return a byte array containing all of the inputStream's bytes.
* @throws IOException if an error occurs reading from the stream.
*/
public static byte[] toByteArray(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024 * 4];
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
return outputStream.toByteArray();
}
示例9: compress
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
/**
* Compresses a given String. It is encoded using ISO-8859-1, So any decompression of the
* compressed string should also use ISO-8859-1
*
* @param str String to be compressed.
* @return compressed bytes
* @throws IOException
*/
public static byte[] compress(String str) throws IOException {
if (str == null || str.length() == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes("UTF-8"));
gzip.close();
byte[] outBytes = out.toByteArray();
return outBytes;
}
示例10: getStreamFromDrawable
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
protected InputStream getStreamFromDrawable(String imageUri) {
try {
int drawableId = Integer.parseInt(imageUri);
BitmapDrawable drawable = (BitmapDrawable) this.context.getResources().getDrawable(drawableId);
Bitmap bitmap = drawable.getBitmap();
ByteArrayOutputStream os = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, os);
return new ByteArrayInputStream(os.toByteArray());
} catch (Exception e) {
Log.e("e", e.toString());
}
return null;
}
示例11: syncMessage
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
private void syncMessage(SyncSessionFactory fromSync, ContactId fromId,
SyncSessionFactory toSync, ContactId toId, int num, boolean valid)
throws IOException, TimeoutException {
// Debug output
String from = "0";
if (fromSync == sync1) from = "1";
else if (fromSync == sync2) from = "2";
String to = "0";
if (toSync == sync1) to = "1";
else if (toSync == sync2) to = "2";
LOG.info("TEST: Sending message from " + from + " to " + to);
ByteArrayOutputStream out = new ByteArrayOutputStream();
// Create an outgoing sync session
SyncSession sessionFrom =
fromSync.createSimplexOutgoingSession(toId, TestPluginConfigModule.MAX_LATENCY, out);
// Write whatever needs to be written
sessionFrom.run();
out.close();
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
// Create an incoming sync session
SyncSession sessionTo = toSync.createIncomingSession(fromId, in);
// Read whatever needs to be read
sessionTo.run();
in.close();
if (valid) {
deliveryWaiter.await(TIMEOUT, num);
} else {
validationWaiter.await(TIMEOUT, num);
}
}
示例12: removeNamespace
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
private String removeNamespace(String xml) throws Exception {
NamespaceRemovingInputStream inputStream = new NamespaceRemovingInputStream(new ByteArrayInputStream(xml.getBytes()));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
IOUtils.copy(inputStream, outputStream);
return new String(outputStream.toByteArray());
}
示例13: serialize
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public static <T> byte[] serialize(T obj){
ByteArrayOutputStream os = new ByteArrayOutputStream();
HessianOutput ho = new HessianOutput(os);
try {
ho.writeObject(obj);
} catch (IOException e) {
throw new IllegalStateException(e.getMessage(), e);
}
return os.toByteArray();
}
示例14: createPullResult
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
private PullResultExt createPullResult(PullMessageRequestHeader requestHeader, PullStatus pullStatus, List<MessageExt> messageExtList) throws Exception {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
for (MessageExt messageExt : messageExtList) {
outputStream.write(MessageDecoder.encode(messageExt, false));
}
return new PullResultExt(pullStatus, requestHeader.getQueueOffset() + messageExtList.size(), 123, 2048, messageExtList, 0, outputStream.toByteArray());
}
示例15: deflate
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
private byte[] deflate(byte[] b) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DeflaterOutputStream dos = new DeflaterOutputStream(baos);
dos.write(b);
dos.close();
return baos.toByteArray();
}