本文整理汇总了Java中java.io.ByteArrayOutputStream.write方法的典型用法代码示例。如果您正苦于以下问题:Java ByteArrayOutputStream.write方法的具体用法?Java ByteArrayOutputStream.write怎么用?Java ByteArrayOutputStream.write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.io.ByteArrayOutputStream
的用法示例。
在下文中一共展示了ByteArrayOutputStream.write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testWriteRandomNumberOfBytes
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public void testWriteRandomNumberOfBytes() throws IOException {
Integer randomBufferSize = randomIntBetween(BUFFER_SIZE, 2 * BUFFER_SIZE);
MockDefaultS3OutputStream out = newS3OutputStream(randomBufferSize);
Integer randomLength = randomIntBetween(1, 2 * BUFFER_SIZE);
ByteArrayOutputStream content = new ByteArrayOutputStream(randomLength);
for (int i = 0; i < randomLength; i++) {
content.write(randomByte());
}
copy(content.toByteArray(), out);
// Checks length & content
assertThat(out.getLength(), equalTo((long) randomLength));
assertThat(Arrays.equals(content.toByteArray(), out.toByteArray()), equalTo(true));
assertThat(out.getBufferSize(), equalTo(randomBufferSize));
int times = (int) Math.ceil(randomLength.doubleValue() / randomBufferSize.doubleValue());
assertThat(out.getFlushCount(), equalTo(times));
if (times > 1) {
assertTrue(out.isMultipart());
} else {
assertFalse(out.isMultipart());
}
}
示例2: readStream
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
private byte[] readStream(InputStream stream) throws Exception
{
int sizeHint = 0;
if (getHttpResponse() != null) {
sizeHint = (int)(getHttpResponse().getEntity().getContentLength());
}
sizeHint = Math.max(sizeHint, BUFFER_SIZE);
ByteArrayOutputStream output = new ByteArrayOutputStream(sizeHint);
byte[] buffer = new byte[BUFFER_SIZE];
int count = 0;
while ((count = stream.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, count);
}
return output.toByteArray();
}
示例3: testSerializeDelimited
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public void testSerializeDelimited() throws Exception {
ByteArrayOutputStream output = new ByteArrayOutputStream();
TestUtil.getAllSet().writeDelimitedTo(output);
output.write(12);
TestUtil.getPackedSet().writeDelimitedTo(output);
output.write(34);
ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
TestUtil.assertAllFieldsSet(TestAllTypes.parseDelimitedFrom(input));
assertEquals(12, input.read());
TestUtil.assertPackedFieldsSet(TestPackedTypes.parseDelimitedFrom(input));
assertEquals(34, input.read());
assertEquals(-1, input.read());
// We're at EOF, so parsing again should return null.
assertTrue(TestAllTypes.parseDelimitedFrom(input) == null);
}
示例4: parse
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public static byte[] parse(String s) {
try {
int n = s.length();
ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);
StringReader r = new StringReader(s);
while (true) {
int b1 = nextNibble(r);
if (b1 < 0) {
break;
}
int b2 = nextNibble(r);
if (b2 < 0) {
throw new RuntimeException("Invalid string " + s);
}
int b = (b1 << 4) | b2;
out.write(b);
}
return out.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例5: readResponseLine
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
static String readResponseLine(InputStream is) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int b;
while ((b = is.read()) != -1) {
if (b == '\r') {
int next = is.read();
if (next == '\n') {
return bos.toString("ISO-8859-1"); // NOI18N
} else if (next == -1) {
return null;
} else {
bos.write(b);
bos.write(next);
}
} else {
bos.write(b);
}
}
return null;
}
示例6: convertImageStreamToByteArray
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
/**
* Converts an InputStream containing an image to a byte array
* @param imageStream An InputStream containing an image
* @return The supplied image in byte[] from
*/
public static byte[] convertImageStreamToByteArray(InputStream imageStream){
// Taken from: https://stackoverflow.com/questions/10296734/image-uri-to-bytesarray
// Date: December 1, 2017
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
try {
while ((len = imageStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
} catch (IOException e){
}
return byteBuffer.toByteArray();
}
示例7: findInClassPath
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
@NotNull
public static TestSource findInClassPath(@NotNull String root, @NotNull String qualifiedClassName) {
InputStream in = ClassLoader
.getSystemClassLoader()
.getResourceAsStream(root + "/" + qualifiedClassName.replace('.', '/') + ".java");
byte[] buffer = new byte[1024];
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
int read;
try {
while ((read = in.read(buffer)) >= 0) {
bOut.write(buffer, 0, read);
}
} catch (IOException e) {
throw new IllegalStateException(String.format("Can't read source text for the class %s from source "
+ "root '%s'", qualifiedClassName, root), e);
}
return new TestSourceImpl(new String(bOut.toByteArray(), StandardCharsets.UTF_8), qualifiedClassName);
}
示例8: addPkcs1PaddingForPrivateKeyOperation
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
/** Añade relleno PKCS#1 para operaciones con clave privada.
* @param in Datos a los que se quiere añadir relleno PKCS#1.
* @param keySize Tamaño de la clave privada que operará posteriormente con estos datos con relleno.
* @return Datos con el relleno PKCS#1 añadido.
* @throws IOException En caso de error el el tratamiento de datos. */
public final static byte[] addPkcs1PaddingForPrivateKeyOperation(final byte[] in, final int keySize) throws IOException {
if (in == null) {
throw new IllegalArgumentException("Los datos de entrada no pueden ser nulos"); //$NON-NLS-1$
}
if (keySize != 1024 && keySize != 2048) {
throw new IllegalArgumentException("Solo se soportan claves de 1024 o 2048 bits, y se ha indicado " + keySize); //$NON-NLS-1$
}
final int len = keySize == 1024 ? PKCS1_LEN_1024 : PKCS1_LEN_2048;
if (in.length > len - 3) {
throw new IllegalArgumentException(
"Los datos son demasiado grandes para el valor de clave indicado: " + in.length + " > " + len + "-3" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
);
}
final ByteArrayOutputStream baos = new ByteArrayOutputStream(len);
baos.write(PKCS1_DELIMIT); // Delimitador : 00
baos.write(PKCS1_BLOCK_TYPE); // Tipo de bloque: 01
while (baos.size() < len - (1 + in.length)) { // Se rellena hasta dejar sitio justo para un delimitador y los datos
baos.write(PKCS1_FILL);
}
baos.write(PKCS1_DELIMIT); // Delimitador : 00
baos.write(in); // Datos
return baos.toByteArray();
}
示例9: decompress
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public static byte[] decompress(byte[] value) throws DataFormatException {
ByteArrayOutputStream bos = new ByteArrayOutputStream(value.length);
Inflater decompressor = new Inflater();
try {
decompressor.setInput(value);
final byte[] buf = new byte[1024];
while (!decompressor.finished()) {
int count = decompressor.inflate(buf);
bos.write(buf, 0, count);
}
} finally {
decompressor.end();
}
return bos.toByteArray();
}
示例10: appVersionData
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
private byte[] appVersionData() throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
String version = RndTool.class.getPackage().getImplementationVersion();
if (version != null) {
bos.write(version.getBytes(StandardCharsets.UTF_8));
}
return bos.toByteArray();
}
示例11: testBinary
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public void testBinary() throws IOException, ClassificationException {
File file = new File("src/test/resources/data/SampleData.txt");
FileInputStream fileInputStream = new FileInputStream(file);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int available;
while ((available = fileInputStream.available()) > 0) {
byte[] data = new byte[available];
fileInputStream.read(data);
byteArrayOutputStream.write(data);
}
fileInputStream.close();
byteArrayOutputStream.close();
Result result1 = classificationClient.getClassifiedDocument(byteArrayOutputStream.toByteArray(), "SampleData.txt");
assertEquals("Hash 1", "f7be152b1d057570b892dbe3dc39bd70", result1.getHash());
Map<String, Collection<String>> metadata = new HashMap<String, Collection<String>>();
Collection<String> cheeses = new Vector<String>();
cheeses.add("Brie");
cheeses.add("Camenbert");
cheeses.add("Cheddar");
metadata.put("cheeses", cheeses);
Result result2 = classificationClient.getClassifiedDocument(byteArrayOutputStream.toByteArray(), "SampleData.txt", new Title("title"), metadata);
assertEquals("Hash 2", "c723555e774c94d0ead71d5c1cc06efc", result2.getHash());
}
示例12: getResourceAsString
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
private static String getResourceAsString(String path) throws IOException {
InputStream inputStream = JrpcService.class.getResourceAsStream(path);
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
return result.toString(StringConstant.messageEncoding);
}
示例13: readTestData
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
private byte[] readTestData(String testDataFileName) throws IOException {
InputStream in = getClass().getResourceAsStream("testdata/" + testDataFileName);
Assert.assertNotNull("test data file doesn't exist: " + testDataFileName, in);
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[32768];
int numRead = 0;
while ((numRead = in.read(buffer)) >= 0) {
result.write(buffer, 0, numRead);
}
return stripNewlineIfNecessary(result.toByteArray());
}
示例14: open
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public void open(AudioInputStream stream) throws LineUnavailableException,
IOException {
if (isOpen()) {
throw new IllegalStateException("Clip is already open with format "
+ getFormat() + " and frame lengh of " + getFrameLength());
}
if (AudioFloatConverter.getConverter(stream.getFormat()) == null)
throw new IllegalArgumentException("Invalid format : "
+ stream.getFormat().toString());
if (stream.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
byte[] data = new byte[(int) stream.getFrameLength()
* stream.getFormat().getFrameSize()];
int readsize = 512 * stream.getFormat().getFrameSize();
int len = 0;
while (len != data.length) {
if (readsize > data.length - len)
readsize = data.length - len;
int ret = stream.read(data, len, readsize);
if (ret == -1)
break;
if (ret == 0)
Thread.yield();
len += ret;
}
open(stream.getFormat(), data, 0, len);
} else {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[512 * stream.getFormat().getFrameSize()];
int r = 0;
while ((r = stream.read(b)) != -1) {
if (r == 0)
Thread.yield();
baos.write(b, 0, r);
}
open(stream.getFormat(), baos.toByteArray(), 0, baos.size());
}
}
示例15: getBytes
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
private static byte[] getBytes(InputStream in) throws IOException {
byte[] bytes = new byte[0];
byte[] buffer = new byte[1024];
ByteArrayOutputStream bout = new ByteArrayOutputStream();
int len = 0;
while((len = in.read(buffer)) > -1) {
bout.write(buffer, 0, len);
}
bytes = bout.toByteArray();
return bytes;
}