Example usage for java.io PushbackInputStream PushbackInputStream

List of usage examples for java.io PushbackInputStream PushbackInputStream

Introduction

In this page you can find the example usage for java.io PushbackInputStream PushbackInputStream.

Prototype

public PushbackInputStream(InputStream in, int size) 

Source Link

Document

Creates a PushbackInputStream with a pushback buffer of the specified size, and saves its argument, the input stream in, for later use.

Usage

From source file:org.apache.nutch.protocol.s2jh.HttpResponse.java

public HttpResponse(HttpBase http, URL url, WebPage page) throws ProtocolException, IOException {
    conf = http.getConf();//from  w  w  w .j  a  va 2 s  .c  om
    this.http = http;
    this.url = url;
    Scheme scheme = null;

    if ("http".equals(url.getProtocol())) {
        scheme = Scheme.HTTP;
    } else if ("https".equals(url.getProtocol())) {
        scheme = Scheme.HTTPS;
    } else {
        throw new HttpException("Unknown scheme (not http/https) for url:" + url);
    }

    if (Http.LOG.isTraceEnabled()) {
        Http.LOG.trace("fetching " + url);
    }

    String path = "".equals(url.getFile()) ? "/" : url.getFile();

    // some servers will redirect a request with a host line like
    // "Host: <hostname>:80" to "http://<hpstname>/<orig_path>"- they
    // don't want the :80...

    String host = url.getHost();
    int port;
    String portString;
    if (url.getPort() == -1) {
        if (scheme == Scheme.HTTP) {
            port = 80;
        } else {
            port = 443;
        }
        portString = "";
    } else {
        port = url.getPort();
        portString = ":" + port;
    }
    Socket socket = null;

    try {
        socket = new Socket(); // create the socket
        socket.setSoTimeout(http.getTimeout());

        // connect
        String sockHost = http.useProxy() ? http.getProxyHost() : host;
        int sockPort = http.useProxy() ? http.getProxyPort() : port;
        InetSocketAddress sockAddr = new InetSocketAddress(sockHost, sockPort);
        socket.connect(sockAddr, http.getTimeout());

        if (scheme == Scheme.HTTPS) {
            SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            SSLSocket sslsocket = (SSLSocket) factory.createSocket(socket, sockHost, sockPort, true);
            sslsocket.setUseClientMode(true);

            // Get the protocols and ciphers supported by this JVM
            Set<String> protocols = new HashSet<String>(Arrays.asList(sslsocket.getSupportedProtocols()));
            Set<String> ciphers = new HashSet<String>(Arrays.asList(sslsocket.getSupportedCipherSuites()));

            // Intersect with preferred protocols and ciphers
            protocols.retainAll(http.getTlsPreferredProtocols());
            ciphers.retainAll(http.getTlsPreferredCipherSuites());

            sslsocket.setEnabledProtocols(protocols.toArray(new String[protocols.size()]));
            sslsocket.setEnabledCipherSuites(ciphers.toArray(new String[ciphers.size()]));

            sslsocket.startHandshake();
            socket = sslsocket;
        }

        if (sockAddr != null && conf.getBoolean("store.ip.address", false) == true) {
            String ipString = sockAddr.getAddress().getHostAddress(); // get the ip
                                                                      // address
            page.getMetadata().put(new Utf8("_ip_"), ByteBuffer.wrap(ipString.getBytes()));
        }

        Http.LOG.debug("HTTP fetching: " + url);
        // make request
        OutputStream req = socket.getOutputStream();

        StringBuffer reqStr = new StringBuffer("GET ");
        if (http.useProxy()) {
            reqStr.append(url.getProtocol() + "://" + host + portString + path);
        } else {
            reqStr.append(path);
        }

        reqStr.append(" HTTP/1.0\r\n");

        reqStr.append("Host: ");
        reqStr.append(host);
        reqStr.append(portString);
        reqStr.append("\r\n");

        reqStr.append("Accept-Encoding: x-gzip, gzip\r\n");

        reqStr.append("Accept: ");
        reqStr.append(this.http.getAccept());
        reqStr.append("\r\n");

        String userAgent = http.getUserAgent();
        if ((userAgent == null) || (userAgent.length() == 0)) {
            if (Http.LOG.isErrorEnabled()) {
                Http.LOG.error("User-agent is not set!");
            }
        } else {
            reqStr.append("User-Agent: ");
            reqStr.append(userAgent);
            reqStr.append("\r\n");
        }

        // if (page.isReadable(WebPage.Field.MODIFIED_TIME.getIndex())) {
        reqStr.append("If-Modified-Since: " + HttpDateFormat.toString(page.getModifiedTime()));
        reqStr.append("\r\n");
        // }
        reqStr.append("\r\n");

        byte[] reqBytes = reqStr.toString().getBytes();

        req.write(reqBytes);
        req.flush();

        PushbackInputStream in = // process response
                new PushbackInputStream(new BufferedInputStream(socket.getInputStream(), Http.BUFFER_SIZE),
                        Http.BUFFER_SIZE);

        StringBuffer line = new StringBuffer();

        boolean haveSeenNonContinueStatus = false;
        while (!haveSeenNonContinueStatus) {
            // parse status code line
            this.code = parseStatusLine(in, line);
            // parse headers
            parseHeaders(in, line);
            haveSeenNonContinueStatus = code != 100; // 100 is "Continue"
        }

        if (!url.toString().endsWith("robots.txt")) {
            if (readPlainContent(url.toString(), in)) {
            } else if (readPlainContentByHtmlunit(url)) {
            } else {
                readPlainContentByWebDriver(url);
            }
        }

        if (content != null && content.length > 0) {
            String html = charset == null ? new String(content) : new String(content, charset);
            //System.out.println("URL: " + url + ", CharsetName: " + charset + " , Page HTML=\n" + html);
            Http.LOG_HTML.trace("URL: " + url + ", CharsetName: " + charset + " , Page HTML=\n" + html);
        }

        // add headers in metadata to row
        if (page.getHeaders() != null) {
            page.getHeaders().clear();
        }
        for (String key : headers.names()) {
            page.getHeaders().put(new Utf8(key), new Utf8(headers.get(key)));
        }

    } catch (Exception e) {
        Http.LOG.error(e.getMessage(), e);
    } finally {
        if (socket != null)
            socket.close();
    }

}

From source file:org.apache.nutch.protocol.http.HttpResponse.java

/**
 * Default public constructor./*  w  ww .  ja  va  2 s . c  o m*/
 *
 * @param http
 * @param url
 * @param datum
 * @throws ProtocolException
 * @throws IOException
 */
public HttpResponse(HttpBase http, URL url, CrawlDatum datum) throws ProtocolException, IOException {

    this.http = http;
    this.url = url;
    this.orig = url.toString();
    this.base = url.toString();

    Scheme scheme = null;

    if ("http".equals(url.getProtocol())) {
        scheme = Scheme.HTTP;
    } else if ("https".equals(url.getProtocol())) {
        scheme = Scheme.HTTPS;
    } else {
        throw new HttpException("Unknown scheme (not http/https) for url:" + url);
    }

    if (Http.LOG.isTraceEnabled()) {
        Http.LOG.trace("fetching " + url);
    }

    String path = "".equals(url.getFile()) ? "/" : url.getFile();

    // some servers will redirect a request with a host line like
    // "Host: <hostname>:80" to "http://<hpstname>/<orig_path>"- they
    // don't want the :80...

    LOG.info("Fetching " + url.toString());

    String host = url.getHost();
    int port;
    String portString;
    if (url.getPort() == -1) {
        if (scheme == Scheme.HTTP) {
            port = 80;
        } else {
            port = 443;
        }
        portString = "";
    } else {
        port = url.getPort();
        portString = ":" + port;
    }
    Socket socket = null;

    try {
        socket = new Socket(); // create the socket
        socket.setSoTimeout(http.getTimeout());

        // connect
        String sockHost = http.useProxy(url) ? http.getProxyHost() : host;
        int sockPort = http.useProxy(url) ? http.getProxyPort() : port;
        InetSocketAddress sockAddr = new InetSocketAddress(sockHost, sockPort);
        socket.connect(sockAddr, http.getTimeout());

        if (scheme == Scheme.HTTPS) {
            SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            SSLSocket sslsocket = (SSLSocket) factory.createSocket(socket, sockHost, sockPort, true);
            sslsocket.setUseClientMode(true);

            // Get the protocols and ciphers supported by this JVM
            Set<String> protocols = new HashSet<String>(Arrays.asList(sslsocket.getSupportedProtocols()));
            Set<String> ciphers = new HashSet<String>(Arrays.asList(sslsocket.getSupportedCipherSuites()));

            // Intersect with preferred protocols and ciphers
            protocols.retainAll(http.getTlsPreferredProtocols());
            ciphers.retainAll(http.getTlsPreferredCipherSuites());

            sslsocket.setEnabledProtocols(protocols.toArray(new String[protocols.size()]));
            sslsocket.setEnabledCipherSuites(ciphers.toArray(new String[ciphers.size()]));

            sslsocket.startHandshake();
            socket = sslsocket;
        }

        this.conf = http.getConf();
        if (sockAddr != null && conf.getBoolean("store.ip.address", false) == true) {
            headers.add("_ip_", sockAddr.getAddress().getHostAddress());
        }

        // make request
        OutputStream req = socket.getOutputStream();

        StringBuffer reqStr = new StringBuffer("GET ");
        if (http.useProxy(url)) {
            reqStr.append(url.getProtocol() + "://" + host + portString + path);
        } else {
            reqStr.append(path);
        }

        reqStr.append(" HTTP/1.0\r\n");

        reqStr.append("Host: ");
        reqStr.append(host);
        reqStr.append(portString);
        reqStr.append("\r\n");

        reqStr.append("Accept-Encoding: x-gzip, gzip, deflate\r\n");

        String userAgent = http.getUserAgent();
        if ((userAgent == null) || (userAgent.length() == 0)) {
            if (Http.LOG.isErrorEnabled()) {
                Http.LOG.error("User-agent is not set!");
            }
        } else {
            reqStr.append("User-Agent: ");
            reqStr.append(userAgent);
            reqStr.append("\r\n");
        }

        reqStr.append("Accept-Language: ");
        reqStr.append(this.http.getAcceptLanguage());
        reqStr.append("\r\n");

        reqStr.append("Accept: ");
        reqStr.append(this.http.getAccept());
        reqStr.append("\r\n");

        if (http.isIfModifiedSinceEnabled() && datum.getModifiedTime() > 0) {
            reqStr.append("If-Modified-Since: " + HttpDateFormat.toString(datum.getModifiedTime()));
            reqStr.append("\r\n");
        }
        reqStr.append("\r\n");

        // store the request in the metadata?
        if (conf.getBoolean("store.http.request", false) == true) {
            headers.add("_request_", reqStr.toString());
        }

        byte[] reqBytes = reqStr.toString().getBytes();

        req.write(reqBytes);
        req.flush();

        LOG.info("Processing response..");

        PushbackInputStream in = // process response
                new PushbackInputStream(new BufferedInputStream(socket.getInputStream(), Http.BUFFER_SIZE),
                        Http.BUFFER_SIZE);

        StringBuffer line = new StringBuffer();

        // store the http headers verbatim
        if (conf.getBoolean("store.http.headers", false) == true) {
            httpHeaders = new StringBuffer();
        }

        headers.add("nutch.fetch.time", Long.toString(System.currentTimeMillis()));

        boolean haveSeenNonContinueStatus = false;
        while (!haveSeenNonContinueStatus) {
            // parse status code line
            this.code = parseStatusLine(in, line);
            if (httpHeaders != null)
                httpHeaders.append(line).append("\n");
            // parse headers
            parseHeaders(in, line, httpHeaders);
            haveSeenNonContinueStatus = code != 100; // 100 is "Continue"
        }

        if (httpHeaders != null) {
            headers.add("_response.headers_", httpHeaders.toString());
        }

        String transferEncoding = getHeader(Response.TRANSFER_ENCODING);
        LOG.info("Transfer Encoding for " + url + ":" + transferEncoding);
        if (transferEncoding != null && "chunked".equalsIgnoreCase(transferEncoding.trim())) {
            readChunkedContent(in, line);
        } else {
            readPlainContent(in);
        }

        String contentEncoding = getHeader(Response.CONTENT_ENCODING);
        if ("gzip".equals(contentEncoding) || "x-gzip".equals(contentEncoding)) {
            content = http.processGzipEncoded(content, url);
        } else if ("deflate".equals(contentEncoding)) {
            content = http.processDeflateEncoded(content, url);
        } else {
            if (Http.LOG.isTraceEnabled()) {
                Http.LOG.trace("fetched " + content.length + " bytes from " + url);
            }
        }

        LOG.info("Checking URL:" + url.toString());
        //check if url contains google drive string
        if (url.toString().toLowerCase().contains("https://drive.google.com/")) {
            //split into two string separated by '=' to get the article id
            LOG.info("Google Drive URL Detected!");
            String[] parts = url.toString().split("=");
            url = new URL("http://drive.google.com/uc?export=download&id=" + parts[1]);

            LOG.info("New URL:" + url.toString());
            this.http = http;
            this.url = url;
            this.orig = url.toString();
            this.base = url.toString();

            HttpClient client = new HttpClient();
            GetMethod method = new GetMethod(url.toString());
            int statusCode = client.executeMethod(method);
            content = method.getResponseBody();
            LOG.info("File Size on Drive: " + content.length);
            //   return;

        }

        LOG.info("Fetch Bytes: " + content.length + " bytes from " + url);

    } finally {
        if (socket != null)
            socket.close();
    }

}

From source file:org.apache.axiom.attachments.Attachments.java

/**
* Moves the pointer to the beginning of the first MIME part. Reads
* till first MIME boundary is found or end of stream is reached.
*
* @param inStream//from   w  w w . j  a va  2  s. co  m
* @param contentTypeString
* @param fileCacheEnable
* @param attachmentRepoDir
* @param fileThreshold
* @param contentLength
* @throws OMException
*/
public Attachments(LifecycleManager manager, InputStream inStream, String contentTypeString,
        boolean fileCacheEnable, String attachmentRepoDir, String fileThreshold, int contentLength)
        throws OMException {
    this.manager = manager;
    this.contentLength = contentLength;
    this.attachmentRepoDir = attachmentRepoDir;
    this.fileCacheEnable = fileCacheEnable;
    if (log.isDebugEnabled()) {
        log.debug("Attachments contentLength=" + contentLength + ", contentTypeString=" + contentTypeString);
    }
    if (fileThreshold != null && (!"".equals(fileThreshold))) {
        this.fileStorageThreshold = Integer.parseInt(fileThreshold);
    } else {
        this.fileStorageThreshold = 1;
    }
    attachmentsMap = new TreeMap();
    try {
        contentType = new ContentType(contentTypeString);
    } catch (ParseException e) {
        throw new OMException("Invalid Content Type Field in the Mime Message", e);
    }
    // REVIEW: This conversion is hard-coded to UTF-8.
    // The complete solution is to respect the charset setting of the message.
    // However this may cause problems in BoundaryDelimittedStream and other
    // lower level classes.

    // Boundary always have the prefix "--".
    try {
        String encoding = contentType.getParameter("charset");
        if (encoding == null || encoding.length() == 0) {
            encoding = "UTF-8";
        }
        String boundaryParam = contentType.getParameter("boundary");
        if (boundaryParam == null) {
            throw new OMException("Content-type has no 'boundary' parameter");
        }
        this.boundary = ("--" + boundaryParam).getBytes(encoding);
        if (log.isDebugEnabled()) {
            log.debug("boundary=" + new String(this.boundary));
        }
    } catch (UnsupportedEncodingException e) {
        throw new OMException(e);
    }

    // If the length is not known, install a TeeInputStream
    // so that we can retrieve it later.
    InputStream is = inStream;
    if (contentLength <= 0) {
        filterIS = new DetachableInputStream(inStream);
        is = filterIS;
    }
    pushbackInStream = new PushbackInputStream(is, PUSHBACK_SIZE);

    // Move the read pointer to the beginning of the first part
    // read till the end of first boundary
    while (true) {
        int value;
        try {
            value = pushbackInStream.read();
            if ((byte) value == boundary[0]) {
                int boundaryIndex = 0;
                while ((boundaryIndex < boundary.length) && ((byte) value == boundary[boundaryIndex])) {
                    value = pushbackInStream.read();
                    if (value == -1) {
                        throw new OMException(
                                "Unexpected End of Stream while searching for first Mime Boundary");
                    }
                    boundaryIndex++;
                }
                if (boundaryIndex == boundary.length) { // boundary found
                    pushbackInStream.read();
                    break;
                }
            } else if (value == -1) {
                throw new OMException("Mime parts not found. Stream ended while searching for the boundary");
            }
        } catch (IOException e1) {
            throw new OMException("Stream Error" + e1.toString(), e1);
        }
    }

    // Read the SOAP part and cache it
    getDataHandler(getSOAPPartContentID());

    // Now reset partsRequested. SOAP part is a special case which is always 
    // read beforehand, regardless of request.
    partsRequested = false;
}

From source file:org.talend.dataprep.schema.xls.XlsUtils.java

/**
 *
 * @param inputStream xls sheet inputStream
 * @return the column number from reading sheet metadata or -1 if unknown
 * @throws XMLStreamException/*  ww w .  j a  v  a2s.c  o m*/
 * @throws IOException
 */
public static int getColumnsNumber(InputStream inputStream) throws XMLStreamException, IOException {
    // If doesn't support mark, wrap up
    if (!inputStream.markSupported()) {
        inputStream = new PushbackInputStream(inputStream, 8);
    }

    int colNumber = 0;

    // TDP-1781 xlsx files may not containing dimension so we fallback to col element number

    XMLStreamReader streamReader = XML_INPUT_FACTORY.createXMLStreamReader(inputStream);
    try {
        while (streamReader.hasNext()) {
            switch (streamReader.next()) {
            case START_ELEMENT:
                if (StringUtils.equals(streamReader.getLocalName(), "dimension")) {
                    Map<String, String> attributesValues = getAttributesNameValue(streamReader);
                    if (!attributesValues.isEmpty()) {
                        return getColumnsNumberFromDimension(attributesValues.get("ref"));
                    }
                }
                if (StringUtils.equals(streamReader.getLocalName(), "col")) {
                    colNumber++;
                }
                break;
            case END_ELEMENT:
                if (StringUtils.equals(streamReader.getLocalName(), "cols")) {
                    return colNumber;
                }
            default:
                // no op
            }
        }
    } finally {
        if (streamReader != null) {
            streamReader.close();
        }
    }
    return -1;
}

From source file:edu.mayo.trilliumbridge.webapp.TransformerController.java

/**
 * From http://stackoverflow.com/a/9737529/656853
 *//*from w  ww . j  a  v a2  s  .  co  m*/
private InputStream checkForUtf8BOMAndDiscardIfAny(InputStream inputStream) throws IOException {
    PushbackInputStream pushbackInputStream = new PushbackInputStream(new BufferedInputStream(inputStream), 3);
    byte[] bom = new byte[3];
    if (pushbackInputStream.read(bom) != -1) {
        if (!(bom[0] == (byte) 0xEF && bom[1] == (byte) 0xBB && bom[2] == (byte) 0xBF)) {
            pushbackInputStream.unread(bom);
        }
    }
    return pushbackInputStream;
}

From source file:org.talend.dataprep.schema.xls.XlsSchemaParser.java

/**
 * Parse all xls sheets for old excel document type
 *
 * @param request the xls request./*www  . j  a v  a  2 s  .  c om*/
 * @return The parsed sheets request.
 */
private List<Schema.SheetContent> parseAllSheetsOldFormat(Request request) {

    final Marker marker = Markers.dataset(request.getMetadata().getId());

    try {
        InputStream inputStream = request.getContent();
        if (!inputStream.markSupported()) {
            inputStream = new PushbackInputStream(inputStream, 8);
        }
        Workbook hssfWorkbook = WorkbookFactory.create(inputStream);

        List<Schema.SheetContent> schemas;
        try {
            if (hssfWorkbook == null) {
                throw new IOException("could not open " + request.getMetadata().getId() + " as an excel file");
            }
            int sheetNumber = hssfWorkbook.getNumberOfSheets();
            if (sheetNumber < 1) {
                LOGGER.debug(marker, "has not sheet to read");
                return Collections.emptyList();
            }
            schemas = new ArrayList<>();
            for (int i = 0; i < sheetNumber; i++) {
                Sheet sheet = hssfWorkbook.getSheetAt(i);
                if (sheet.getLastRowNum() < 1) {
                    LOGGER.debug(marker, "sheet '{}' do not have rows skip ip", sheet.getSheetName());
                    continue;
                }
                List<ColumnMetadata> columnsMetadata = parsePerSheet(sheet, //
                        request.getMetadata().getId(), //
                        hssfWorkbook.getCreationHelper().createFormulaEvaluator());
                String sheetName = sheet.getSheetName();
                // update XlsSerializer if this default sheet naming change!!!
                schemas.add(
                        new Schema.SheetContent(sheetName == null ? "sheet-" + i : sheetName, columnsMetadata));
            }
        } finally {
            hssfWorkbook.close();
        }
        return schemas;
    } catch (Exception e) {
        LOGGER.debug(marker, "Exception during parsing xls request :" + e.getMessage(), e);
        throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e);
    }
}

From source file:edu.virginia.speclab.juxta.author.model.JuxtaXMLParser.java

private InputStream stripUtf8Bom(InputStream inputStream) throws IOException {
    PushbackInputStream pushbackInputStream = new PushbackInputStream(new BufferedInputStream(inputStream), 3);
    byte[] bom = new byte[3];
    if (pushbackInputStream.read(bom) != -1) {
        if (!(bom[0] == (byte) 0xEF && bom[1] == (byte) 0xBB && bom[2] == (byte) 0xBF)) {
            pushbackInputStream.unread(bom);
        }/* w  w  w.  j a v a 2s  .  c  o  m*/
    }
    return pushbackInputStream;
}

From source file:XmlReader.java

private XmlReader(InputStream stream) throws IOException {
    super(stream);

    PushbackInputStream pb;// ww w  .  ja  v a  2 s .co  m
    byte buf[];
    int len;

    /*if (stream instanceof PushbackInputStream)
        pb = (PushbackInputStream) stream;
    else*/
    /**
     * Commented out the above code to make sure it works when the
     * document is accessed using http. URL connection in the code uses
     * a PushbackInputStream with size 7 and when we try to push back
     * MAX which default value is set to 512 we get and exception. So
     * that's why we need to wrap the stream irrespective of what type
     * of stream we start off with.
     */
    pb = new PushbackInputStream(stream, MAXPUSHBACK);

    //
    // See if we can figure out the character encoding used
    // in this file by peeking at the first few bytes.
    //
    buf = new byte[4];
    len = pb.read(buf);
    if (len > 0)
        pb.unread(buf, 0, len);

    if (len == 4)
        switch (buf[0] & 0x0ff) {
        case 0:
            // 00 3c 00 3f == illegal UTF-16 big-endian
            if (buf[1] == 0x3c && buf[2] == 0x00 && buf[3] == 0x3f) {
                setEncoding(pb, "UnicodeBig");
                return;
            }
            // else it's probably UCS-4
            break;

        case '<': // 0x3c: the most common cases!
            switch (buf[1] & 0x0ff) {
            // First character is '<'; could be XML without
            // an XML directive such as "<hello>", "<!-- ...",
            // and so on.
            default:
                break;

            // 3c 00 3f 00 == illegal UTF-16 little endian
            case 0x00:
                if (buf[2] == 0x3f && buf[3] == 0x00) {
                    setEncoding(pb, "UnicodeLittle");
                    return;
                }
                // else probably UCS-4
                break;

            // 3c 3f 78 6d == ASCII and supersets '<?xm'
            case '?':
                if (buf[2] != 'x' || buf[3] != 'm')
                    break;
                //
                // One of several encodings could be used:
                // Shift-JIS, ASCII, UTF-8, ISO-8859-*, etc
                //
                useEncodingDecl(pb, "UTF8");
                return;
            }
            break;

        // 4c 6f a7 94 ... some EBCDIC code page
        case 0x4c:
            if (buf[1] == 0x6f && (0x0ff & buf[2]) == 0x0a7 && (0x0ff & buf[3]) == 0x094) {
                useEncodingDecl(pb, "CP037");
                return;
            }
            // whoops, treat as UTF-8
            break;

        // UTF-16 big-endian
        case 0xfe:
            if ((buf[1] & 0x0ff) != 0xff)
                break;
            setEncoding(pb, "UTF-16");
            return;

        // UTF-16 little-endian
        case 0xff:
            if ((buf[1] & 0x0ff) != 0xfe)
                break;
            setEncoding(pb, "UTF-16");
            return;

        // default ... no XML declaration
        default:
            break;
        }

    //
    // If all else fails, assume XML without a declaration, and
    // using UTF-8 encoding.
    //
    setEncoding(pb, "UTF-8");
}

From source file:org.springframework.ws.soap.saaj.SaajSoapMessageFactory.java

/**
 * Checks for the UTF-8 Byte Order Mark, and removes it if present. The SAAJ RI cannot cope with these BOMs.
 *
 * @see <a href="http://jira.springframework.org/browse/SWS-393">SWS-393</a>
 * @see <a href="http://unicode.org/faq/utf_bom.html#22">UTF-8 BOMs</a>
 *///  w  w  w  . j a va2  s  . c om
private InputStream checkForUtf8ByteOrderMark(InputStream inputStream) throws IOException {
    PushbackInputStream pushbackInputStream = new PushbackInputStream(new BufferedInputStream(inputStream), 3);
    byte[] bytes = new byte[3];
    int bytesRead = pushbackInputStream.read(bytes);
    if (bytesRead != -1) {
        // check for the UTF-8 BOM, and remove it if there. See SWS-393
        if (!isByteOrderMark(bytes)) {
            pushbackInputStream.unread(bytes, 0, bytesRead);
        }
    }
    return pushbackInputStream;
}

From source file:com.maverick.ssl.https.HttpsURLConnection.java

public synchronized void connect() throws IOException {

    if (!connected) {

        // #ifdef DEBUG
        log.info(MessageFormat.format(Messages.getString("HttpsURLConnection.connecting"), //$NON-NLS-1$
                new Object[] { url.getHost(), new Integer(url.getPort() == -1 ? 443 : url.getPort()) }));
        // #endif

        String proxyHost = System.getProperty(httpProxyHostProperty);
        if (proxyHost != null && !isNonProxiedHost(url.getHost())) {

            boolean isSecure = Boolean.valueOf(System.getProperty(httpProxySecureProperty, "true")) //$NON-NLS-1$
                    .booleanValue();//w w w .  ja va  2  s.com
            String proxyPort = System.getProperty(httpProxyPortProperty);
            String proxyUsername = System.getProperty(httpProxyUsernameProperty);
            String proxyPassword = System.getProperty(httpProxyPasswordProperty);

            // #ifdef DEBUG
            log.info(MessageFormat.format(Messages.getString("HttpsURLConnection.requiresProxyConnection"), //$NON-NLS-1$
                    new Object[] { isSecure ? "https" : "http://", proxyHost, new Integer(proxyPort) })); //$NON-NLS-1$ //$NON-NLS-2$
            log.info(MessageFormat.format(Messages.getString("HttpsURLConnection.proxyUsername"), new Object[] { //$NON-NLS-1$
                    proxyUsername == null || proxyUsername.equals("") ? "not set" : proxyUsername })); //$NON-NLS-1$ //$NON-NLS-2$
            // #endif
            if (proxyPort == null) {
                throw new IOException(Messages.getString("HttpsURLConnection.noProxyPort")); //$NON-NLS-1$
            }

            try {
                int port = Integer.parseInt(proxyPort);

                HttpClient client = new HttpClient(proxyHost, port, isSecure);
                HttpMethod method = new ConnectMethod(url.getHost(), url.getPort() == -1 ? 443 : url.getPort(),
                        true);

                PasswordCredentials credentials = new PasswordCredentials();
                credentials.setUsername(proxyUsername);
                credentials.setPassword(proxyPassword);

                client.setCredentials(credentials);

                HttpResponse response = client.execute(method);
                socket = response.getConnection().getSocket();
            } catch (HttpException ex) {
                // #ifdef DEBUG
                log.info(MessageFormat.format(Messages.getString("HttpsURLConnection.proxyConnectionFailed"), //$NON-NLS-1$
                        new Object[] { ex.getMessage(), new Integer(ex.getStatus()) }));
                // #endif
                throw new IOException(
                        MessageFormat.format(Messages.getString("HttpsURLConnection.proxyConnectionFailed"), //$NON-NLS-1$
                                new Object[] { ex.getMessage(), new Integer(ex.getStatus()) })); //$NON-NLS-2$ //$NON-NLS-3$
            } catch (UnsupportedAuthenticationException ex) {
                // #ifdef DEBUG
                log.info(Messages.getString("HttpsURLConnection.proxyAuthenticationFailed"), ex); //$NON-NLS-1$
                // #endif
                throw new IOException(ex.getMessage());
            } catch (AuthenticationCancelledException ex) {
                throw new IOException(Messages.getString("HttpsURLConnection.userCancelledAuthenitcation")); //$NON-NLS-1$
            }
        } else {
            String host = url.getHost();
            if (host == null) {
                throw new IOException(Messages.getString("HttpsURLConnection.noHost")); //$NON-NLS-1$
            }
            int port = url.getPort();
            try {
                socket = new SSLSocket(host, port == -1 ? 443 : port);
            } catch (SSLException ex1) {
                throw new SSLIOException(ex1);
            }
        }

        try {
            writeRequest(socket.getOutputStream());
            readResponse(input = new PushbackInputStream(socket.getInputStream(), 2048));
        } catch (IOException ex) {
            try {
                socket.close();
            } catch (IOException ignored) {
            }
            throw ex;
        }
        connected = true;
    }
}