本文整理汇总了Java中java.io.ByteArrayOutputStream.size方法的典型用法代码示例。如果您正苦于以下问题:Java ByteArrayOutputStream.size方法的具体用法?Java ByteArrayOutputStream.size怎么用?Java ByteArrayOutputStream.size使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.io.ByteArrayOutputStream
的用法示例。
在下文中一共展示了ByteArrayOutputStream.size方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeSection
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
/**
* Write a section with header and data.
*
* @param type
* the name of the section
* @param data
* the data of the section
* @param name
* the name, must be set if the id == 0
* @throws IOException
* if any I/O error occur
*/
void writeSection( SectionType type, WasmOutputStream data, String name ) throws IOException {
ByteArrayOutputStream baos = (ByteArrayOutputStream)data.out;
int size = baos.size();
if( size == 0 ) {
return;
}
writeVaruint32( type.ordinal() );
writeVaruint32( size );
if( type == SectionType.Custom ) {
byte[] bytes = name.getBytes( StandardCharsets.ISO_8859_1 );
writeVaruint32( bytes.length );
write( bytes );
}
baos.writeTo( this );
}
示例2: loadResource
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public static Mat loadResource(Context context, int resourceId, int flags) throws IOException
{
InputStream is = context.getResources().openRawResource(resourceId);
ByteArrayOutputStream os = new ByteArrayOutputStream(is.available());
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
is.close();
Mat encoded = new Mat(1, os.size(), CvType.CV_8U);
encoded.put(0, 0, os.toByteArray());
os.close();
Mat decoded = Imgcodecs.imdecode(encoded, flags);
encoded.release();
return decoded;
}
示例3: maybeAppendFragment
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
private static void maybeAppendFragment(ByteArrayOutputStream fragmentBuffer,
String charset,
StringBuilder result) {
if (fragmentBuffer.size() > 0) {
byte[] fragmentBytes = fragmentBuffer.toByteArray();
String fragment;
if (charset == null) {
fragment = new String(fragmentBytes, Charset.forName("UTF-8"));
} else {
try {
fragment = new String(fragmentBytes, charset);
} catch (UnsupportedEncodingException e) {
fragment = new String(fragmentBytes, Charset.forName("UTF-8"));
}
}
fragmentBuffer.reset();
result.append(fragment);
}
}
示例4: getPartLength
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public long getPartLength() {
long bodyLen = getBodyLength();
if (bodyLen < 0) {
return -1;
}
ByteArrayOutputStream overhead = new ByteArrayOutputStream();
try {
writeStart(overhead);
writeDisposition(overhead);
writeContentType(overhead);
writeOtherHeaders(overhead);
writeEndOfHeaders(overhead);
writeEnd(overhead);
return overhead.size() + bodyLen;
} catch (IOException ex) {
return -1;
}
}
示例5: getImageSize
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public static int getImageSize(Bitmap bmp) {
if (bmp == null) {
return 0;
}
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 100, baos);
int size = baos.size();
baos.flush();
baos.close();
return size;
} catch (IOException e) {
e.printStackTrace();
return 0;
}
}
示例6: predictSubfulfillmentLength
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
static int predictSubfulfillmentLength(Fulfillment ff) {
int fulfillmentLength = ff.getCondition().getMaxFulfillmentLength();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OerOutputStream cos = new OerOutputStream(baos);
try {
cos.write16BitUInt(0 /* type Undefined */);
byte[] bytes = new byte[fulfillmentLength];
java.util.Arrays.fill( bytes, (byte) 0 );
cos.writeOctetString(bytes);
int result = baos.size();
return result;
}catch(Exception e) {
throw new RuntimeException(e.toString(), e);
} finally {
cos.close();
}
}
示例7: determineSize
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
private int determineSize(Serializable ser) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(ser);
oos.close();
return baos.size();
}
示例8: convert
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
/**
* Converts a byte array from a source base to a target base using the alphabet.
*/
private byte[] convert(final byte[] message, final int sourceBase, final int targetBase) {
/**
* This algorithm is inspired by: http://codegolf.stackexchange.com/a/21672
*/
final int estimatedLength = estimateOutputLength(message.length, sourceBase, targetBase);
final ByteArrayOutputStream out = new ByteArrayOutputStream(estimatedLength);
byte[] source = message;
while (source.length > 0) {
final ByteArrayOutputStream quotient = new ByteArrayOutputStream(source.length);
int remainder = 0;
for (int i = 0; i < source.length; i++) {
final int accumulator = (source[i] & 0xFF) + remainder * sourceBase;
final int digit = (accumulator - (accumulator % targetBase)) / targetBase;
remainder = accumulator % targetBase;
if (quotient.size() > 0 || digit > 0) {
quotient.write(digit);
}
}
out.write(remainder);
source = quotient.toByteArray();
}
// pad output with zeroes corresponding to the number of leading zeroes in the message
for (int i = 0; i < message.length - 1 && message[i] == 0; i++) {
out.write(0);
}
return reverse(out.toByteArray());
}
示例9: getBytesFromInputStream
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
public static byte[] getBytesFromInputStream(InputStream is, int maxSize) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[0xFFFF];
for (int len; (len = is.read(buffer)) != -1 && os.size() < maxSize; ) {
os.write(buffer, 0, len);
}
if (os.size() >= maxSize) {
throw new IOException("File was too big");
}
os.flush();
return os.toByteArray();
}
示例10: readString
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
/**
* Read a string from the binary model (System default should be UTF-8)
* @param ds input data stream
* @return a String from the model
*/
public static String readString(DataInputStream ds) throws IOException {
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
while (true) {
byte byteValue = ds.readByte();
if ((byteValue != 32) && (byteValue != 10)) {
byteBuffer.write(byteValue);
} else if (byteBuffer.size() > 0) {
break;
}
}
String word = byteBuffer.toString();
byteBuffer.close();
return word;
}
示例11: connect
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
@WorkerThread
public void connect(byte[] infoHash, byte[] peerId) throws IOException, InvalidKeyException, InterruptedException {
if (ValidationUtils.isEmptyArray(infoHash) || ValidationUtils.isEmptyArray(peerId)) {
return;
}
if (mConnection.connect() == false) {
if (DEBUG) {
Log.e(TAG, "Connect to peer failed");
}
return;
}
final ByteArrayOutputStream receivedBytes = new ByteArrayOutputStream();
final CountDownLatch latch = new CountDownLatch(1);
Connection.ConnectionListener listener = new Connection.ConnectionListener() {
@Override
public void onReceived(@NonNull byte[] received) {
try {
receivedBytes.write(received);
} catch (IOException ignore) {
} finally {
if (receivedBytes.size() >= HandshakeMessage.BASE_HANDSHAKE_LENGTH) {
latch.countDown();
}
}
}
};
// hand shake
mConnection.addConnectionListener(listener);
try {
HandshakeMessage handshake = new HandshakeMessage(infoHash, peerId);
requestSendProtocolMessage(handshake);
latch.await(1, TimeUnit.MINUTES);
// validate handshake
HandshakeMessage response = HandshakeMessage.parseFromResponse(ByteBuffer.wrap(receivedBytes.toByteArray()));
if (response == null || handshake.validateHandShake(response) == false) {
if (DEBUG) {
Log.e(TAG, "connect(), Failed to handshake");
}
return;
}
if (DEBUG) {
Log.i(TAG, "Hand shake finished ");
}
mConnection.addConnectionListener(mConnectionListener);
} finally {
mConnection.removeListener(listener);
}
}
示例12: size
import java.io.ByteArrayOutputStream; //导入方法依赖的package包/类
/**
* The count of bytes in the stream. Work only for in memory stream.
*
* @return the data size
*/
int size() {
ByteArrayOutputStream baos = (ByteArrayOutputStream)out;
return baos.size();
}