List of usage examples for java.io ByteArrayInputStream read
public synchronized int read()
From source file:com.github.devnied.emvnfccard.utils.TlvUtil.java
public static byte[] readTagIdBytes(final ByteArrayInputStream stream) { ByteArrayOutputStream tagBAOS = new ByteArrayOutputStream(); byte tagFirstOctet = (byte) stream.read(); tagBAOS.write(tagFirstOctet);// w w w.j av a 2 s. c om // Find TAG bytes byte MASK = (byte) 0x1F; if ((tagFirstOctet & MASK) == MASK) { // EMV book 3, Page 178 or Annex B1 (EMV4.3) // Tag field is longer than 1 byte do { int nextOctet = stream.read(); if (nextOctet < 0) { break; } byte tlvIdNextOctet = (byte) nextOctet; tagBAOS.write(tlvIdNextOctet); if (!BytesUtils.matchBitByBitIndex(tlvIdNextOctet, 7) || BytesUtils.matchBitByBitIndex(tlvIdNextOctet, 7) && (tlvIdNextOctet & 0x7f) == 0) { break; } } while (true); } return tagBAOS.toByteArray(); }
From source file:UploadUtils.UguuUpload.java
/** * Send the file to Uguu./*www . j a v a 2 s . c o m*/ * @param b the contents of the file in a byte array * @param conn the connection to use * @throws java.IOException during writing to output stream */ private static void sendFile(byte[] b, HttpURLConnection conn) throws IOException { String first = String.format( "Content-Type: multipart/form-data; boundary=" + boundary + "\"\r\nContent-Length: 30721\r\n"); String second = String .format("Content-Disposition: form-data; name=\"MAX_FILE_SIZE\"\r\n\r\n" + "100000000\r\n"); String data = String.format("Content-Disposition: form-data; name=\"file\";filename=\"" + filename + "." + extension + "\"\r\nContent-type:" + tmpfiletype + "\r\n"); String last = String.format("Content-Disposition: form-data; name=\"name\""); ByteArrayInputStream bais = new ByteArrayInputStream(b); DataOutputStream outstream; try { outstream = new DataOutputStream(conn.getOutputStream()); outstream.writeBytes(first); outstream.writeBytes("\r\n"); outstream.writeBytes("\r\n"); outstream.writeBytes("--" + boundary); outstream.writeBytes(second); outstream.writeBytes("--" + boundary + "\r\n"); outstream.writeBytes(data); outstream.writeBytes("\r\n"); int i; while ((i = bais.read()) > -1) { outstream.write(i); } bais.close(); outstream.writeBytes("\r\n"); outstream.writeBytes("--" + boundary + "\r\n"); outstream.writeBytes(last); outstream.writeBytes("\r\n"); outstream.writeBytes("\r\n"); outstream.writeBytes("\r\n"); outstream.writeBytes("--" + boundary + "--"); outstream.flush(); outstream.close(); } catch (IOException ex) { throw ex; } }
From source file:com.github.devnied.emvnfccard.utils.TlvUtil.java
public static TLV getNextTLV(final ByteArrayInputStream stream) { if (stream.available() < 2) { throw new TlvException("Error parsing data. Available bytes < 2 . Length=" + stream.available()); }// w ww . j av a 2 s . c o m // ISO/IEC 7816 uses neither '00' nor 'FF' as tag value. // Before, between, or after TLV-coded data objects, // '00' or 'FF' bytes without any meaning may occur // (for example, due to erased or modified TLV-coded data objects). stream.mark(0); int peekInt = stream.read(); byte peekByte = (byte) peekInt; // peekInt == 0xffffffff indicates EOS while (peekInt != -1 && (peekByte == (byte) 0xFF || peekByte == (byte) 0x00)) { stream.mark(0); // Current position peekInt = stream.read(); peekByte = (byte) peekInt; } stream.reset(); // Reset back to the last known position without 0x00 or 0xFF if (stream.available() < 2) { throw new TlvException("Error parsing data. Available bytes < 2 . Length=" + stream.available()); } byte[] tagIdBytes = TlvUtil.readTagIdBytes(stream); // We need to get the raw length bytes. // Use quick and dirty workaround stream.mark(0); int posBefore = stream.available(); // Now parse the lengthbyte(s) // This method will read all length bytes. We can then find out how many bytes was read. int length = TlvUtil.readTagLength(stream); // Decoded // Now find the raw (encoded) length bytes int posAfter = stream.available(); stream.reset(); byte[] lengthBytes = new byte[posBefore - posAfter]; if (lengthBytes.length < 1 || lengthBytes.length > 4) { throw new TlvException("Number of length bytes must be from 1 to 4. Found " + lengthBytes.length); } stream.read(lengthBytes, 0, lengthBytes.length); int rawLength = BytesUtils.byteArrayToInt(lengthBytes); byte[] valueBytes; ITag tag = searchTagById(tagIdBytes); // Find VALUE bytes if (rawLength == 128) { // 1000 0000 // indefinite form stream.mark(0); int prevOctet = 1; int curOctet; int len = 0; while (true) { len++; curOctet = stream.read(); if (curOctet < 0) { throw new TlvException( "Error parsing data. TLV " + "length byte indicated indefinite length, but EOS " + "was reached before 0x0000 was found" + stream.available()); } if (prevOctet == 0 && curOctet == 0) { break; } prevOctet = curOctet; } len -= 2; valueBytes = new byte[len]; stream.reset(); stream.read(valueBytes, 0, len); length = len; } else { if (stream.available() < length) { throw new TlvException("Length byte(s) indicated " + length + " value bytes, but only " + stream.available() + " " + (stream.available() > 1 ? "are" : "is") + " available"); } // definite form valueBytes = new byte[length]; stream.read(valueBytes, 0, length); } // Remove any trailing 0x00 and 0xFF stream.mark(0); peekInt = stream.read(); peekByte = (byte) peekInt; while (peekInt != -1 && (peekByte == (byte) 0xFF || peekByte == (byte) 0x00)) { stream.mark(0); peekInt = stream.read(); peekByte = (byte) peekInt; } stream.reset(); // Reset back to the last known position without 0x00 or 0xFF return new TLV(tag, length, lengthBytes, valueBytes); }
From source file:org.commonjava.util.partyline.BinaryFileTest.java
@Test public void shouldReadBinaryFile() throws IOException, InterruptedException { List<String> failures = new ArrayList<>(); File binaryFile = temp.newFile("binary-file.bin"); ReentrantLock lock = new ReentrantLock(); JoinableFile jf = new JoinableFile(binaryFile, new LockOwner(binaryFile.getAbsolutePath(), name.getMethodName(), LockLevel.write), true); OutputStream jos = jf.getOutputStream(); InputStream actual = jf.joinStream(); ByteArrayOutputStream written = new ByteArrayOutputStream(); writeBinaryFile(jos, written);//www. java2 s . com int pos = 0; int exp, act; ByteArrayInputStream expected = new ByteArrayInputStream(written.toByteArray()); while ((exp = expected.read()) != -1) { act = actual.read(); pos++; if (act != exp) { failures.add(String.format("Failure at position %d. Expected %d, got %d", pos, exp, act)); } } if (!failures.isEmpty()) { fail("Failures: " + failures); } }
From source file:com.codebutler.farebot.card.desfire.DesfireFileSettings.java
private DesfireFileSettings(ByteArrayInputStream stream) { fileType = (byte) stream.read(); commSetting = (byte) stream.read(); accessRights = new byte[2]; stream.read(accessRights, 0, accessRights.length); }
From source file:io.cloudslang.engine.queue.entities.ExecutionMessageConverter.java
private void skipPayloadMetaData(ByteArrayInputStream is) throws IOException { for (int i = 0; i < PAYLOAD_META_DATA_INIT_BYTES.length; i++) { is.read();//from ww w .ja v a 2s. c o m } }
From source file:com.codebutler.farebot.card.desfire.raw.RawDesfireFileSettings.java
@NonNull public DesfireFileSettings parse() { ByteArrayInputStream stream = new ByteArrayInputStream(data().bytes()); byte fileType = (byte) stream.read(); byte commSetting = (byte) stream.read(); byte[] accessRights = new byte[2]; stream.read(accessRights, 0, accessRights.length); switch (fileType) { case STANDARD_DATA_FILE: case BACKUP_DATA_FILE: return createStandardDesfireFileSettings(fileType, commSetting, accessRights, stream); case LINEAR_RECORD_FILE: case CYCLIC_RECORD_FILE: return createRecordDesfireFileSettings(fileType, commSetting, accessRights, stream); case VALUE_FILE: return createValueDesfireFileSettings(fileType, commSetting, accessRights, stream); default:/*from w w w . j a v a 2 s . c o m*/ throw new RuntimeException("Unknown file type: " + Integer.toHexString(fileType)); } }
From source file:org.cryptonode.jncryptor.TrailerInputStreamTest.java
@Test public void testSanity() { for (int i = 0; i < 10000; i++) { byte[] inputData = new byte[15]; RANDOM.nextBytes(inputData);// www . j a v a 2s . com ByteArrayInputStream in = new ByteArrayInputStream(inputData); int count = 0; while (in.read() != -1) { count++; } assertTrue(count == 15); } }
From source file:io.cortical.retina.core.ImagesTest.java
/** * {@link Images#compareImage(List, Integer, ImagePlotShape, ImageEncoding)} * // www .j a va2s . c o m * @throws ApiException : should never be thrown. * @throws IOException */ @Test public void compareModelTest() throws ApiException, IOException { ImagePlotShape shape = ImagePlotShape.CIRCLE; ImageEncoding encoding = ImageEncoding.BASE64_PNG; List<Term> terms = Arrays.asList(TERM_1, TERM_2); when(api.getOverlayImage(eq(TERM_1_TERM_2_JSON), eq(NOT_NULL_RETINA), eq(shape.name().toLowerCase()), eq(1), eq(encoding.machineRepresentation()))) .thenReturn(new ByteArrayInputStream(new byte[] { "i".getBytes()[0] })); ByteArrayInputStream stream = images.compareImage(terms, 1, shape, encoding); assertNotNull(stream); assertEquals(105, stream.read()); stream.close(); verify(api, times(1)).getOverlayImage(eq(TERM_1_TERM_2_JSON), eq(NOT_NULL_RETINA), eq(shape.name().toLowerCase()), eq(1), eq(encoding.machineRepresentation())); }
From source file:io.cortical.retina.core.ImagesTest.java
/** * {@link Images#getImage(io.cortical.retina.model.Model, int, ImagePlotShape, ImageEncoding, double)} * /* w w w . ja va 2s. co m*/ * @throws ApiException : should never be thrown. * @throws IOException */ @Test public void testGetImage() throws ApiException, IOException { ImagePlotShape shape = ImagePlotShape.CIRCLE; ImageEncoding encoding = ImageEncoding.BASE64_PNG; double sparsity = 0.02; when(api.getImageForExpression(eq(TERM_1_JSON), eq(NOT_NULL_RETINA), eq(1), eq(shape.name().toLowerCase()), eq(encoding.machineRepresentation()), eq(sparsity))) .thenReturn(new ByteArrayInputStream(new byte[] { "i".getBytes()[0] })); ByteArrayInputStream stream = images.getImage(TERM_1, 1, shape, encoding, sparsity); assertNotNull(stream); assertEquals(105, stream.read()); stream.close(); verify(api, times(1)).getImageForExpression(eq(TERM_1_JSON), eq(NOT_NULL_RETINA), eq(1), eq(shape.name().toLowerCase()), eq(encoding.machineRepresentation()), eq(sparsity)); }