Java Code Examples for java.io.ByteArrayInputStream

The following are top voted examples for showing how to use java.io.ByteArrayInputStream. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to product more good examples.

Example 1
Project: sslcertx   File: MySQLProtocol.java View source code 7 votes vote down vote up
public MyX509TrustManager(Properties props) throws Exception{
    boolean trustServerCertificate  =  props.getProperty("trustServerCertificate") != null;
    if (trustServerCertificate)
        return;

    serverCertFile =  props.getProperty("serverSslCert");
    InputStream inStream;

    if (serverCertFile.startsWith("-----BEGIN CERTIFICATE-----")) {
        inStream = new ByteArrayInputStream(serverCertFile.getBytes());
    } else if (serverCertFile.startsWith("classpath:")) {
        // Load it from a classpath relative file
        String classpathFile = serverCertFile.substring("classpath:".length());
        inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(classpathFile);
    } else {
        inStream = new FileInputStream(serverCertFile);
    }

    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509Certificate ca = (X509Certificate) cf.generateCertificate(inStream);
    inStream.close();
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    try {
        // Note: KeyStore requires it be loaded even if you don't load anything into it:
        ks.load(null);
    } catch (Exception e) {

    }
    ks.setCertificateEntry(UUID.randomUUID().toString(), ca);
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(ks);
    for(TrustManager tm : tmf.getTrustManagers()) {
    	if (tm instanceof X509TrustManager) {
  	        trustManager = (X509TrustManager) tm;
  		    break;
        }
    }
    if (trustManager == null) {
        throw new RuntimeException("No X509TrustManager found");
    }
}
 
Example 2
Project: jentrata-msh   File: OutgoingMessageProcessorTest.java View source code 6 votes vote down vote up
@Test
	public void testPlainMessageMIC() throws Exception{
	//Prepare Data
	InputStream ins = FIXTURE_LOADER.getResourceAsStream(MOCK_AS2_MSG);
	ByteArrayInputStream bIns = new ByteArrayInputStream(IOHandler.readBytes(ins));

	partnershipDVO.setIsReceiptSignRequired(true);
	partnershipDVO.setIsReceiptRequired(true);
	
	String mid = RANDOM.toString();
	AS2Message as2Msg = TARGET.storeOutgoingMessage(
			mid, //MessageID
			"xml", 
			partnershipDVO,
			new InputStreamDataSource(bIns, "xml", MOCK_AS2_MSG),
			null);
	
	//Verify MIC value
	ins = FIXTURE_LOADER.getResourceAsStream(MOCK_AS2_MSG);
	bIns = new ByteArrayInputStream(IOHandler.readBytes(ins));
	byte[] content = new byte[bIns.available()];
	bIns.read(content);
	bIns.close();
	String mic = calculateMIC(content);
	
	MessageDVO msgDVO = getStoredMessage(mid);
	Assert.assertEquals( "MIC value is not valid", mic, msgDVO.getMicValue());
}
 
Example 3
Project: JerseyTest   File: FormDataMultiPartBufferTest.java View source code 6 votes vote down vote up
@Override
public ContainerRequest filter(ContainerRequest request) {
   // Buffer
    InputStream in = request.getEntityInputStream();
    if (in.getClass() != ByteArrayInputStream.class) {
        // Buffer input
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ReaderWriter.writeTo(in, baos);
        } catch (IOException ex) {
            throw new ContainerException(ex);
        }
        in = new ByteArrayInputStream(baos.toByteArray());
        request.setEntityInputStream(in);
    }

    // Read entity
    FormDataMultiPart multiPart = request.getEntity(FormDataMultiPart.class);

    assertEquals(3, multiPart.getBodyParts().size());
    assertNotNull(multiPart.getField("foo"));
    assertEquals("bar", multiPart.getField("foo").getValue());
    assertNotNull(multiPart.getField("baz"));
    assertEquals("bop", multiPart.getField("baz").getValue());

    assertNotNull(multiPart.getField("bean"));
    MultiPartBean bean = multiPart.getField("bean").getValueAs(MultiPartBean.class);
    assertEquals("myname", bean.getName());
    assertEquals("myvalue", bean.getValue());
 
    // Reset buffer
    ByteArrayInputStream bais = (ByteArrayInputStream)in;
    bais.reset();

    request.getProperties().put("filtered", "true");
    return request;
}
 
Example 4
Project: TextSecureSMP   File: PduParser.java View source code 6 votes vote down vote up
/**
 * Parse encoded string value.
 *
 * @param pduDataStream pdu data input stream
 * @return the EncodedStringValue
 */
protected static EncodedStringValue parseEncodedStringValue(ByteArrayInputStream pduDataStream){
    /**
     * From OMA-TS-MMS-ENC-V1_3-20050927-C.pdf
     * Encoded-string-value = Text-string | Value-length Char-set Text-string
     */
    assert(null != pduDataStream);
    pduDataStream.mark(1);
    EncodedStringValue returnValue = null;
    int charset = 0;
    int temp = pduDataStream.read();
    assert(-1 != temp);
    int first = temp & 0xFF;
    if (first == 0) {
        return null;    //  Blank subject, bail.
    }

    pduDataStream.reset();
    if (first < TEXT_MIN) {
        parseValueLength(pduDataStream);

        charset = parseShortInteger(pduDataStream); //get the "Charset"
    }

    byte[] textString = parseWapString(pduDataStream, TYPE_TEXT_STRING);

    try {
        if (0 != charset) {
            returnValue = new EncodedStringValue(charset, textString);
        } else {
            returnValue = new EncodedStringValue(textString);
        }
    } catch(Exception e) {
        return null;
    }

    return returnValue;
}
 
Example 5
Project: ProjectModel   File: Serializer.java View source code 6 votes vote down vote up
private static Number readDouble(ByteArrayInputStream stream) {
	stream.skip(1);
	String d = readNumber(stream);
	if (d.equals(__NAN)) {
		return new Double(Double.NaN);
	}
	if (d.equals(__INF)) {
		return new Double(Double.POSITIVE_INFINITY);
	}
	if (d.equals(__NINF)) {
		return new Double(Double.NEGATIVE_INFINITY);
	}
	if ((d.indexOf('.') > 0) || (d.indexOf('e') > 0) || (d.indexOf('E') > 0)) {
		return new Double(d);
	}
	int len = d.length();
	char c = d.charAt(0);
	if ((len < 19) || ((c == '-') && (len < 20))) {
		return new Long(d);
	}
	if ((len > 20) || ((c != '-') && (len > 19))) {
		return new Double(d);
	}
	try {
		return new Long(d);
	} catch (Exception e) {
		return new Double(d);
	}
}
 
Example 6
Project: JDK   File: XMLSignatureInput.java View source code 6 votes vote down vote up
protected InputStream getResetableInputStream() throws IOException{
    if ((_inputOctetStreamProxy instanceof ByteArrayInputStream) ) {
        if (!_inputOctetStreamProxy.markSupported()) {
            throw new RuntimeException("Accepted as Markable but not truly been"+_inputOctetStreamProxy);
        }
        return _inputOctetStreamProxy;
    }
    if (bytes!=null) {
        _inputOctetStreamProxy=new ByteArrayInputStream(bytes);
        return _inputOctetStreamProxy;
    }
    if (_inputOctetStreamProxy ==null)
        return null;
    if (_inputOctetStreamProxy.markSupported()) {
        log.log(java.util.logging.Level.INFO, "Mark Suported but not used as reset");
    }
    bytes=JavaUtils.getBytesFromStream(_inputOctetStreamProxy);
    _inputOctetStreamProxy.close();
    _inputOctetStreamProxy=new ByteArrayInputStream(bytes);
    return _inputOctetStreamProxy;
}