Search in sources :

Example 36 with PushbackInputStream

use of java.io.PushbackInputStream in project robovm by robovm.

the class OpenSSLX509CertPath method fromPkcs7Encoding.

private static CertPath fromPkcs7Encoding(InputStream inStream) throws CertificateException {
    try {
        if (inStream == null || inStream.available() == 0) {
            return new OpenSSLX509CertPath(Collections.<X509Certificate>emptyList());
        }
    } catch (IOException e) {
        throw new CertificateException("Problem reading input stream", e);
    }
    final boolean markable = inStream.markSupported();
    if (markable) {
        inStream.mark(PUSHBACK_SIZE);
    }
    /* Attempt to see if this is a PKCS#7 bag. */
    final PushbackInputStream pbis = new PushbackInputStream(inStream, PUSHBACK_SIZE);
    try {
        final byte[] buffer = new byte[PKCS7_MARKER.length];
        final int len = pbis.read(buffer);
        if (len < 0) {
            /* No need to reset here. The stream was empty or EOF. */
            throw new ParsingException("inStream is empty");
        }
        pbis.unread(buffer, 0, len);
        if (len == PKCS7_MARKER.length && Arrays.equals(PKCS7_MARKER, buffer)) {
            return new OpenSSLX509CertPath(OpenSSLX509Certificate.fromPkcs7PemInputStream(pbis));
        }
        return new OpenSSLX509CertPath(OpenSSLX509Certificate.fromPkcs7DerInputStream(pbis));
    } catch (Exception e) {
        if (markable) {
            try {
                inStream.reset();
            } catch (IOException ignored) {
            }
        }
        throw new CertificateException(e);
    }
}
Also used : PushbackInputStream(java.io.PushbackInputStream) ParsingException(org.conscrypt.OpenSSLX509CertificateFactory.ParsingException) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) ParsingException(org.conscrypt.OpenSSLX509CertificateFactory.ParsingException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) CertificateEncodingException(java.security.cert.CertificateEncodingException)

Example 37 with PushbackInputStream

use of java.io.PushbackInputStream in project robovm by robovm.

the class OldAndroidPushbackInputStreamTest method testPushbackInputStream.

public void testPushbackInputStream() throws Exception {
    String str = "AbCdEfGhIjKlM\nOpQrStUvWxYz";
    ByteArrayInputStream aa = new ByteArrayInputStream(str.getBytes());
    ByteArrayInputStream ba = new ByteArrayInputStream(str.getBytes());
    ByteArrayInputStream ca = new ByteArrayInputStream(str.getBytes());
    PushbackInputStream a = new PushbackInputStream(aa, 7);
    try {
        a.unread("push".getBytes());
        Assert.assertEquals("pushAbCdEfGhIjKlM\nOpQrStUvWxYz", read(a));
    } finally {
        a.close();
    }
    PushbackInputStream b = new PushbackInputStream(ba, 9);
    try {
        b.unread('X');
        Assert.assertEquals("XAbCdEfGhI", read(b, 10));
    } finally {
        b.close();
    }
    PushbackInputStream c = new PushbackInputStream(ca);
    try {
        Assert.assertEquals("bdfhjl\nprtvxz", skipRead(c));
    } finally {
        c.close();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PushbackInputStream(java.io.PushbackInputStream)

Example 38 with PushbackInputStream

use of java.io.PushbackInputStream in project jdk8u_jdk by JetBrains.

the class CharacterDecoder method decodeBuffer.

/**
     * Decode the text from the InputStream and write the decoded
     * octets to the OutputStream. This method runs until the stream
     * is exhausted.
     * @exception CEFormatException An error has occurred while decoding
     * @exception CEStreamExhausted The input stream is unexpectedly out of data
     */
public void decodeBuffer(InputStream aStream, OutputStream bStream) throws IOException {
    int i;
    int totalBytes = 0;
    PushbackInputStream ps = new PushbackInputStream(aStream);
    decodeBufferPrefix(ps, bStream);
    while (true) {
        int length;
        try {
            length = decodeLinePrefix(ps, bStream);
            for (i = 0; (i + bytesPerAtom()) < length; i += bytesPerAtom()) {
                decodeAtom(ps, bStream, bytesPerAtom());
                totalBytes += bytesPerAtom();
            }
            if ((i + bytesPerAtom()) == length) {
                decodeAtom(ps, bStream, bytesPerAtom());
                totalBytes += bytesPerAtom();
            } else {
                decodeAtom(ps, bStream, length - i);
                totalBytes += (length - i);
            }
            decodeLineSuffix(ps, bStream);
        } catch (CEStreamExhausted e) {
            break;
        }
    }
    decodeBufferSuffix(ps, bStream);
}
Also used : PushbackInputStream(java.io.PushbackInputStream)

Example 39 with PushbackInputStream

use of java.io.PushbackInputStream in project webservices-axiom by apache.

the class EncodingDetectionHelper method detectEncoding.

public String detectEncoding() throws XMLStreamException {
    byte[] startBytes = new byte[4];
    try {
        if (useMark) {
            stream.mark(4);
        }
        int read = 0;
        do {
            int c = stream.read(startBytes, read, 4 - read);
            if (c == -1) {
                throw new XMLStreamException("Unexpected end of stream");
            }
            read += c;
        } while (read < 4);
        if (useMark) {
            stream.reset();
        } else {
            ((PushbackInputStream) stream).unread(startBytes);
        }
    } catch (IOException ex) {
        throw new XMLStreamException("Unable to read start bytes", ex);
    }
    int marker = ((startBytes[0] & 0xFF) << 24) + ((startBytes[1] & 0xFF) << 16) + ((startBytes[2] & 0xFF) << 8) + (startBytes[3] & 0xFF);
    switch(marker) {
        case 0x0000FEFF:
        case 0xFFFE0000:
        case 0x0000FFFE:
        case 0xFEFF0000:
        case 0x0000003C:
        case 0x3C000000:
        case 0x00003C00:
        case 0x003C0000:
            return "UCS-4";
        case 0x003C003F:
            return "UTF-16BE";
        case 0x3C003F00:
            return "UTF-16LE";
        case 0x3C3F786D:
            return "UTF-8";
        default:
            if ((marker & 0xFFFF0000) == 0xFEFF0000) {
                return "UTF-16BE";
            } else if ((marker & 0xFFFF0000) == 0xFFFE0000) {
                return "UTF-16LE";
            } else {
                return "UTF-8";
            }
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) PushbackInputStream(java.io.PushbackInputStream) IOException(java.io.IOException)

Example 40 with PushbackInputStream

use of java.io.PushbackInputStream in project Minim by ddf.

the class MpegAudioFileReader method getAudioFileFormat.

/**
	 * Returns AudioFileFormat from inputstream and medialength.
	 */
public AudioFileFormat getAudioFileFormat(InputStream inputStream, long mediaLength) throws UnsupportedAudioFileException, IOException {
    system.debug("MpegAudioFileReader.getAudioFileFormat(InputStream inputStream, long mediaLength): begin");
    HashMap<String, Object> aff_properties = new HashMap<String, Object>();
    HashMap<String, Object> af_properties = new HashMap<String, Object>();
    int mLength = (int) mediaLength;
    int size = inputStream.available();
    PushbackInputStream pis = new PushbackInputStream(inputStream, MARK_LIMIT);
    byte[] head = new byte[22];
    pis.read(head);
    system.debug("InputStream : " + inputStream + " =>" + new String(head));
    // Next check for Shoutcast (supported) and OGG (unsupported) streams.
    if ((head[0] == 'R') && (head[1] == 'I') && (head[2] == 'F') && (head[3] == 'F') && (head[8] == 'W') && (head[9] == 'A') && (head[10] == 'V') && (head[11] == 'E')) {
        system.debug("RIFF/WAV stream found");
        int isPCM = ((head[21] << 8) & 0x0000FF00) | ((head[20]) & 0x00000FF);
        if (weak == null) {
            if (isPCM == 1)
                throw new UnsupportedAudioFileException("WAV PCM stream found");
        }
    } else if ((head[0] == '.') && (head[1] == 's') && (head[2] == 'n') && (head[3] == 'd')) {
        system.debug("AU stream found");
        if (weak == null)
            throw new UnsupportedAudioFileException("AU stream found");
    } else if ((head[0] == 'F') && (head[1] == 'O') && (head[2] == 'R') && (head[3] == 'M') && (head[8] == 'A') && (head[9] == 'I') && (head[10] == 'F') && (head[11] == 'F')) {
        system.debug("AIFF stream found");
        if (weak == null)
            throw new UnsupportedAudioFileException("AIFF stream found");
    } else if (((head[0] == 'M') | (head[0] == 'm')) && ((head[1] == 'A') | (head[1] == 'a')) && ((head[2] == 'C') | (head[2] == 'c'))) {
        system.debug("APE stream found");
        if (weak == null)
            throw new UnsupportedAudioFileException("APE stream found");
    } else if (((head[0] == 'F') | (head[0] == 'f')) && ((head[1] == 'L') | (head[1] == 'l')) && ((head[2] == 'A') | (head[2] == 'a')) && ((head[3] == 'C') | (head[3] == 'c'))) {
        system.debug("FLAC stream found");
        if (weak == null)
            throw new UnsupportedAudioFileException("FLAC stream found");
    } else // Shoutcast stream ?
    if (((head[0] == 'I') | (head[0] == 'i')) && ((head[1] == 'C') | (head[1] == 'c')) && ((head[2] == 'Y') | (head[2] == 'y'))) {
        pis.unread(head);
        // Load shoutcast meta data.
        loadShoutcastInfo(pis, aff_properties);
    } else // Ogg stream ?
    if (((head[0] == 'O') | (head[0] == 'o')) && ((head[1] == 'G') | (head[1] == 'g')) && ((head[2] == 'G') | (head[2] == 'g'))) {
        system.debug("Ogg stream found");
        if (weak == null)
            throw new UnsupportedAudioFileException("Ogg stream found");
    } else // No, so pushback.
    {
        pis.unread(head);
    }
    // MPEG header info.
    int nVersion = AudioSystem.NOT_SPECIFIED;
    int nLayer = AudioSystem.NOT_SPECIFIED;
    // int nSFIndex = AudioSystem.NOT_SPECIFIED;
    int nMode = AudioSystem.NOT_SPECIFIED;
    int FrameSize = AudioSystem.NOT_SPECIFIED;
    // int nFrameSize = AudioSystem.NOT_SPECIFIED;
    int nFrequency = AudioSystem.NOT_SPECIFIED;
    int nTotalFrames = AudioSystem.NOT_SPECIFIED;
    float FrameRate = AudioSystem.NOT_SPECIFIED;
    int BitRate = AudioSystem.NOT_SPECIFIED;
    int nChannels = AudioSystem.NOT_SPECIFIED;
    int nHeader = AudioSystem.NOT_SPECIFIED;
    int nTotalMS = AudioSystem.NOT_SPECIFIED;
    boolean nVBR = false;
    AudioFormat.Encoding encoding = null;
    try {
        Bitstream m_bitstream = new Bitstream(pis);
        aff_properties.put("mp3.header.pos", new Integer(m_bitstream.header_pos()));
        Header m_header = m_bitstream.readFrame();
        if (m_header == null) {
            throw new UnsupportedAudioFileException("Unable to read mp3 header");
        }
        // nVersion = 0 => MPEG2-LSF (Including MPEG2.5), nVersion = 1 => MPEG1
        nVersion = m_header.version();
        if (nVersion == 2)
            aff_properties.put("mp3.version.mpeg", Float.toString(2.5f));
        else
            aff_properties.put("mp3.version.mpeg", Integer.toString(2 - nVersion));
        // nLayer = 1,2,3
        nLayer = m_header.layer();
        aff_properties.put("mp3.version.layer", Integer.toString(nLayer));
        // nSFIndex = m_header.sample_frequency();
        nMode = m_header.mode();
        aff_properties.put("mp3.mode", new Integer(nMode));
        nChannels = nMode == 3 ? 1 : 2;
        aff_properties.put("mp3.channels", new Integer(nChannels));
        nVBR = m_header.vbr();
        af_properties.put("vbr", new Boolean(nVBR));
        aff_properties.put("mp3.vbr", new Boolean(nVBR));
        aff_properties.put("mp3.vbr.scale", new Integer(m_header.vbr_scale()));
        FrameSize = m_header.calculate_framesize();
        aff_properties.put("mp3.framesize.bytes", new Integer(FrameSize));
        if (FrameSize < 0) {
            throw new UnsupportedAudioFileException("Invalid FrameSize : " + FrameSize);
        }
        nFrequency = m_header.frequency();
        aff_properties.put("mp3.frequency.hz", new Integer(nFrequency));
        FrameRate = (float) ((1.0 / (m_header.ms_per_frame())) * 1000.0);
        aff_properties.put("mp3.framerate.fps", new Float(FrameRate));
        if (FrameRate < 0) {
            throw new UnsupportedAudioFileException("Invalid FrameRate : " + FrameRate);
        }
        if (mLength != AudioSystem.NOT_SPECIFIED) {
            aff_properties.put("mp3.length.bytes", new Integer(mLength));
            nTotalFrames = m_header.max_number_of_frames(mLength);
            aff_properties.put("mp3.length.frames", new Integer(nTotalFrames));
        }
        BitRate = m_header.bitrate();
        af_properties.put("bitrate", new Integer(BitRate));
        aff_properties.put("mp3.bitrate.nominal.bps", new Integer(BitRate));
        nHeader = m_header.getSyncHeader();
        encoding = sm_aEncodings[nVersion][nLayer - 1];
        aff_properties.put("mp3.version.encoding", encoding.toString());
        if (mLength != AudioSystem.NOT_SPECIFIED) {
            nTotalMS = Math.round(m_header.total_ms(mLength));
            aff_properties.put("duration", new Long((long) nTotalMS * 1000L));
        }
        aff_properties.put("mp3.copyright", new Boolean(m_header.copyright()));
        aff_properties.put("mp3.original", new Boolean(m_header.original()));
        aff_properties.put("mp3.crc", new Boolean(m_header.checksums()));
        aff_properties.put("mp3.padding", new Boolean(m_header.padding()));
        InputStream id3v2 = m_bitstream.getRawID3v2();
        if (id3v2 != null) {
            aff_properties.put("mp3.id3tag.v2", id3v2);
            parseID3v2Frames(id3v2, aff_properties);
        }
        if (TDebug.TraceAudioFileReader)
            TDebug.out(m_header.toString());
    } catch (Exception e) {
        system.debug("not a MPEG stream: " + e.toString());
        throw new UnsupportedAudioFileException("not a MPEG stream: " + e.toString());
    }
    // Deeper checks ?
    int cVersion = (nHeader >> 19) & 0x3;
    if (cVersion == 1) {
        system.debug("not a MPEG stream: wrong version");
        throw new UnsupportedAudioFileException("not a MPEG stream: wrong version");
    }
    int cSFIndex = (nHeader >> 10) & 0x3;
    if (cSFIndex == 3) {
        system.debug("not a MPEG stream: wrong sampling rate");
        throw new UnsupportedAudioFileException("not a MPEG stream: wrong sampling rate");
    }
    // Look up for ID3v1 tag
    if ((size == mediaLength) && (mediaLength != AudioSystem.NOT_SPECIFIED)) {
        // FileInputStream fis = (FileInputStream) inputStream;
        byte[] id3v1 = new byte[128];
        int toSkip = inputStream.available() - id3v1.length;
        if (toSkip > 0) {
            inputStream.skip(inputStream.available() - id3v1.length);
        }
        inputStream.read(id3v1, 0, id3v1.length);
        if ((id3v1[0] == 'T') && (id3v1[1] == 'A') && (id3v1[2] == 'G')) {
            parseID3v1Frames(id3v1, aff_properties);
        }
    }
    AudioFormat format = new MpegAudioFormat(encoding, (float) nFrequency, // SampleSizeInBits
    AudioSystem.NOT_SPECIFIED, // Channels - The
    nChannels, // The number of bytes in
    -1, // FrameRate - The
    FrameRate, // second
    true, af_properties);
    return new MpegAudioFileFormat(MpegFileFormatType.MP3, format, nTotalFrames, mLength, aff_properties);
}
Also used : HashMap(java.util.HashMap) Bitstream(javazoom.jl.decoder.Bitstream) BufferedInputStream(java.io.BufferedInputStream) IcyInputStream(javazoom.spi.mpeg.sampled.file.tag.IcyInputStream) PushbackInputStream(java.io.PushbackInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) MpegAudioFileFormat(javazoom.spi.mpeg.sampled.file.MpegAudioFileFormat) MpegAudioFormat(javazoom.spi.mpeg.sampled.file.MpegAudioFormat) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) IOException(java.io.IOException) AccessControlException(java.security.AccessControlException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) Header(javazoom.jl.decoder.Header) PushbackInputStream(java.io.PushbackInputStream) AudioFormat(javax.sound.sampled.AudioFormat) MpegAudioFormat(javazoom.spi.mpeg.sampled.file.MpegAudioFormat)

Aggregations

PushbackInputStream (java.io.PushbackInputStream)80 IOException (java.io.IOException)42 InputStream (java.io.InputStream)18 ByteArrayInputStream (java.io.ByteArrayInputStream)16 FileInputStream (java.io.FileInputStream)6 CertificateException (java.security.cert.CertificateException)5 Test (org.junit.Test)5 File (java.io.File)4 CRLException (java.security.cert.CRLException)4 CertificateParsingException (java.security.cert.CertificateParsingException)4 InputStreamReader (java.io.InputStreamReader)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)3 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)3 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStream (java.io.OutputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Charset (java.nio.charset.Charset)2