Search in sources :

Example 61 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project qpid-broker-j by apache.

the class AbstractEncoder method write.

private void write(Type t, Object value) {
    switch(t) {
        case BIN8:
        case UINT8:
            writeUint8(coerce(Short.class, value));
            break;
        case INT8:
            put(coerce(Byte.class, value));
            break;
        case CHAR:
            put((byte) ((char) coerce(Character.class, value)));
            break;
        case BOOLEAN:
            if (coerce(Boolean.class, value)) {
                put((byte) 1);
            } else {
                put((byte) 0);
            }
            break;
        case BIN16:
        case UINT16:
            writeUint16(coerce(Integer.class, value));
            break;
        case INT16:
            writeUint16(coerce(Short.class, value));
            break;
        case BIN32:
        case UINT32:
            writeUint32(coerce(Long.class, value));
            break;
        case CHAR_UTF32:
        case INT32:
            writeUint32(coerce(Integer.class, value));
            break;
        case FLOAT:
            writeUint32(Float.floatToIntBits(coerce(Float.class, value)));
            break;
        case BIN64:
        case UINT64:
        case INT64:
        case DATETIME:
            writeUint64(coerce(Long.class, value));
            break;
        case DOUBLE:
            long bits = Double.doubleToLongBits(coerce(Double.class, value));
            writeUint64(bits);
            break;
        case UUID:
            writeUuid(coerce(UUID.class, value));
            break;
        case STR8:
            writeStr8(coerce(String.class, value));
            break;
        case STR16:
            writeStr16(coerce(String.class, value));
            break;
        case STR8_LATIN:
        case STR16_LATIN:
            Charset charset;
            try {
                charset = Charset.forName("ISO-8859-15");
            } catch (UnsupportedCharsetException e) {
                // We do not want to start throwing execptions from here so we fall back to ISO_8859_1
                charset = StandardCharsets.ISO_8859_1;
            }
            writeBytes(t, coerce(String.class, value).getBytes(charset));
            break;
        case STR8_UTF16:
        case STR16_UTF16:
            writeBytes(t, coerce(String.class, value).getBytes(StandardCharsets.UTF_16));
            break;
        case MAP:
            writeMap((Map<String, Object>) coerce(Map.class, value));
            break;
        case LIST:
            writeList(coerce(List.class, value));
            break;
        case ARRAY:
            writeArray(coerce(List.class, value));
            break;
        case STRUCT32:
            writeStruct32(coerce(Struct.class, value));
            break;
        case BIN40:
        case DEC32:
        case BIN72:
        case DEC64:
            // XXX: what types are we supposed to use here?
            writeBytes(t, coerce(byte[].class, value));
            break;
        case VOID:
            break;
        default:
            writeBytes(t, coerce(byte[].class, value));
            break;
    }
}
Also used : Charset(java.nio.charset.Charset) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) List(java.util.List) UUID(java.util.UUID)

Example 62 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project openj9 by eclipse.

the class JavaCoreReader method getJavaCoreCodePage.

private Charset getJavaCoreCodePage(ByteArrayInputStream input) throws IOException {
    input.mark(256);
    Charset cs = null;
    try {
        byte[] headBytes = new byte[256];
        input.read(headBytes);
        ByteBuffer headByteBuffer = ByteBuffer.wrap(headBytes);
        String[] estimates = new String[] { // EBCDIC
        "IBM1047", // Multibyte big endian
        "UTF-16BE", // Multibyte Windows
        "UTF-16LE", System.getProperty("file.encoding"), // DOS/ASCII/CP1252/ISO-8859-1/CP437 lookalike.
        "IBM850" };
        try {
            for (int i = 0; i < estimates.length && cs == null; i++) {
                cs = attemptCharset(headByteBuffer, Charset.forName(estimates[i]));
            }
        } catch (UnsupportedCharsetException e) {
        /* This JVM doesn't support the charset we've requested, so skip it */
        }
    } catch (JavacoreFileEncodingException e) {
        /* As it isn't possible to report the error through the API
			 * we have no choice but to silently consume it here. The 
			 * API will continue in a best effort basis.
			 */
        Logger.getLogger(com.ibm.dtfj.image.ImageFactory.DTFJ_LOGGER_NAME).log(Level.INFO, e.getMessage());
    } finally {
        input.reset();
    }
    return cs;
}
Also used : UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) Charset(java.nio.charset.Charset) ByteBuffer(java.nio.ByteBuffer)

Example 63 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project openj9 by eclipse.

the class FileSniffer method attemptCharset.

private static Charset attemptCharset(ByteBuffer headByteBuffer, Charset trialCharset) throws IOException {
    final String sectionEyeCatcher = "0SECTION";
    final String charsetEyeCatcher = "1TICHARSET";
    headByteBuffer.rewind();
    String head = trialCharset.decode(headByteBuffer).toString();
    /* If we can find the section eyecatcher, this encoding is mostly good */
    if (head.indexOf(sectionEyeCatcher) >= 0) {
        int idx = head.indexOf(charsetEyeCatcher);
        /* The charset eyecatcher is much newer, so may not be present */
        if (idx >= 0) {
            idx += charsetEyeCatcher.length();
            String javacoreCharset = head.substring(idx).trim();
            javacoreCharset = javacoreCharset.split("\\s+")[0];
            try {
                Charset trueCharset = Charset.forName(javacoreCharset);
                /*
					 * Check if trueCharset would have encoded the sectionEyeCatcher the
					 * same way.
					 */
                ByteBuffer sanityTrial = trialCharset.encode(sectionEyeCatcher);
                ByteBuffer sanityTrue = trueCharset.encode(sectionEyeCatcher);
                if (sanityTrial.equals(sanityTrue)) {
                    // Encoding matches, the trueCharset from 1TICHARSET will be the best one to return.
                    return trueCharset;
                }
            /*
				* Ignore exceptions, if the trueCharset from 1TICHARSET is illegal or unknown we
				* will return the trialCharset that read 0SECTION correctly.
				*/
            } catch (IllegalCharsetNameException e) {
            } catch (UnsupportedCharsetException e) {
            }
        }
        return trialCharset;
    }
    /* Failed to find section eyecatcher after decoding in this charset. */
    return null;
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) Charset(java.nio.charset.Charset) ByteBuffer(java.nio.ByteBuffer)

Example 64 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project autorest-clientruntime-for-java by Azure.

the class LoggingInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    // get logger
    Request request = chain.request();
    String context = request.header(LOGGING_HEADER);
    String bodyLoggingHeader = request.header(BODY_LOGGING);
    boolean bodyLogging = bodyLoggingHeader == null || Boolean.parseBoolean(bodyLoggingHeader);
    if (context == null) {
        context = "";
    }
    Logger logger = LoggerFactory.getLogger(context);
    // log URL
    if (logLevel != LogLevel.NONE) {
        log(logger, String.format("--> %s %s", request.method(), request.url()));
    }
    // log headers
    if (logLevel == LogLevel.HEADERS || logLevel == LogLevel.BODY_AND_HEADERS) {
        for (String header : request.headers().names()) {
            if (!LOGGING_HEADER.equals(header)) {
                log(logger, String.format("%s: %s", header, Joiner.on(", ").join(request.headers(header))));
            }
        }
    }
    // log body
    if (bodyLogging && (logLevel == LogLevel.BODY || logLevel == LogLevel.BODY_AND_HEADERS)) {
        if (request.body() != null) {
            Buffer buffer = new Buffer();
            request.body().writeTo(buffer);
            Charset charset = Charset.forName("UTF8");
            MediaType contentType = request.body().contentType();
            if (contentType != null) {
                charset = contentType.charset(charset);
            }
            if (isPlaintext(buffer)) {
                String content = buffer.clone().readString(charset);
                if (logLevel.isPrettyJson()) {
                    try {
                        content = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(MAPPER.readValue(content, JsonNode.class));
                    } catch (Exception e) {
                    // swallow, keep original content
                    }
                }
                log(logger, String.format("%s-byte body:\n%s", request.body().contentLength(), content));
                log(logger, "--> END " + request.method());
            } else {
                log(logger, "--> END " + request.method() + " (binary " + request.body().contentLength() + "-byte body omitted)");
            }
        }
    }
    long startNs = System.nanoTime();
    Response response;
    try {
        response = chain.proceed(request);
    } catch (Exception e) {
        if (logLevel != LogLevel.NONE) {
            log(logger, "<-- HTTP FAILED: " + e);
        }
        throw e;
    }
    long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
    ResponseBody responseBody = response.body();
    long contentLength = responseBody.contentLength();
    String bodySize = contentLength != -1 ? contentLength + "-byte" : "unknown-length";
    // log URL
    if (logLevel != LogLevel.NONE) {
        log(logger, String.format("<-- %s %s %s (%s ms, %s body)", response.code(), response.message(), response.request().url(), tookMs, bodySize));
    }
    // log headers
    if (logLevel == LogLevel.HEADERS || logLevel == LogLevel.BODY_AND_HEADERS) {
        for (String header : response.headers().names()) {
            log(logger, String.format("%s: %s", header, Joiner.on(", ").join(response.headers(header))));
        }
    }
    // log body
    if (bodyLogging && (logLevel == LogLevel.BODY || logLevel == LogLevel.BODY_AND_HEADERS)) {
        if (response.body() != null) {
            BufferedSource source = responseBody.source();
            // Buffer the entire body.
            source.request(Long.MAX_VALUE);
            Buffer buffer = source.buffer();
            Charset charset = Charset.forName("UTF8");
            MediaType contentType = responseBody.contentType();
            if (contentType != null) {
                try {
                    charset = contentType.charset(charset);
                } catch (UnsupportedCharsetException e) {
                    log(logger, "Couldn't decode the response body; charset is likely malformed.");
                    log(logger, "<-- END HTTP");
                    return response;
                }
            }
            boolean gzipped = response.header("content-encoding") != null && StringUtils.containsIgnoreCase(response.header("content-encoding"), "gzip");
            if (!isPlaintext(buffer) && !gzipped) {
                log(logger, "<-- END HTTP (binary " + buffer.size() + "-byte body omitted)");
                return response;
            }
            String content;
            if (gzipped) {
                content = CharStreams.toString(new InputStreamReader(new GZIPInputStream(buffer.clone().inputStream())));
            } else {
                content = buffer.clone().readString(charset);
            }
            if (logLevel.isPrettyJson()) {
                try {
                    content = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(MAPPER.readValue(content, JsonNode.class));
                } catch (Exception e) {
                // swallow, keep original content
                }
            }
            log(logger, String.format("%s-byte body:\n%s", buffer.size(), content));
            log(logger, "<-- END HTTP");
        }
    }
    return response;
}
Also used : Buffer(okio.Buffer) InputStreamReader(java.io.InputStreamReader) Request(okhttp3.Request) Charset(java.nio.charset.Charset) Logger(org.slf4j.Logger) IOException(java.io.IOException) EOFException(java.io.EOFException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) GZIPInputStream(java.util.zip.GZIPInputStream) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) MediaType(okhttp3.MediaType) BufferedSource(okio.BufferedSource)

Example 65 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project che by eclipse.

the class TextSearchVisitor method processFile.

/**
//	 * @return returns a map from IFile to IDocument for all open, dirty editors
//	 */
//	private Map evalNonFileBufferDocuments() {
//		Map result = new HashMap();
//		IWorkbench workbench = SearchPlugin.getDefault().getWorkbench();
//		IWorkbenchWindow[] windows = workbench.getWorkbenchWindows();
//		for (int i = 0; i < windows.length; i++) {
//			IWorkbenchPage[] pages = windows[i].getPages();
//			for (int x = 0; x < pages.length; x++) {
//				IEditorReference[] editorRefs = pages[x].getEditorReferences();
//				for (int z = 0; z < editorRefs.length; z++) {
//					IEditorPart ep = editorRefs[z].getEditor(false);
//					if (ep instanceof ITextEditor && ep.isDirty()) { // only dirty editors
//						evaluateTextEditor(result, ep);
//					}
//				}
//			}
//		}
//		return result;
//	}
//	private void evaluateTextEditor(Map result, IEditorPart ep) {
//		IEditorInput input= ep.getEditorInput();
//		if (input instanceof IFileEditorInput) {
//			IFile file= ((IFileEditorInput) input).getFile();
//			if (!result.containsKey(file)) { // take the first editor found
//				ITextFileBufferManager bufferManager= FileBuffers.getTextFileBufferManager();
//				ITextFileBuffer textFileBuffer= bufferManager.getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
//				if (textFileBuffer != null) {
//					// file buffer has precedence
//					result.put(file, textFileBuffer.getDocument());
//				} else {
//					// use document provider
//					IDocument document= ((ITextEditor) ep).getDocumentProvider().getDocument(input);
//					if (document != null) {
//						result.put(file, document);
//					}
//				}
//			}
//		}
//	}
public boolean processFile(IFile file, Map documentsInEditors) {
    try {
        if (!fCollector.acceptFile(file) || fMatcher == null) {
            return true;
        }
        IDocument document = getOpenDocument(file, documentsInEditors);
        if (document != null) {
            DocumentCharSequence documentCharSequence = new DocumentCharSequence(document);
            // assume all documents are non-binary
            locateMatches(file, documentCharSequence);
        } else {
            CharSequence seq = null;
            try {
                seq = fFileCharSequenceProvider.newCharSequence(file);
                if (hasBinaryContent(seq, file) && !fCollector.reportBinaryFile(file)) {
                    return true;
                }
                locateMatches(file, seq);
            } catch (FileCharSequenceProvider.FileCharSequenceException e) {
                e.throwWrappedException();
            } finally {
                if (seq != null) {
                    try {
                        fFileCharSequenceProvider.releaseCharSequence(seq);
                    } catch (IOException e) {
                        SearchPlugin.log(e);
                    }
                }
            }
        }
    } catch (UnsupportedCharsetException e) {
        String[] args = { getCharSetName(file), file.getFullPath().makeRelative().toString() };
        String message = Messages.format(SearchMessages.TextSearchVisitor_unsupportedcharset, args);
        fStatus.add(new Status(IStatus.ERROR, NewSearchUI.PLUGIN_ID, IStatus.ERROR, message, e));
    } catch (IllegalCharsetNameException e) {
        String[] args = { getCharSetName(file), file.getFullPath().makeRelative().toString() };
        String message = Messages.format(SearchMessages.TextSearchVisitor_illegalcharset, args);
        fStatus.add(new Status(IStatus.ERROR, NewSearchUI.PLUGIN_ID, IStatus.ERROR, message, e));
    } catch (IOException e) {
        String[] args = { getExceptionMessage(e), file.getFullPath().makeRelative().toString() };
        String message = Messages.format(SearchMessages.TextSearchVisitor_error, args);
        fStatus.add(new Status(IStatus.ERROR, NewSearchUI.PLUGIN_ID, IStatus.ERROR, message, e));
    } catch (CoreException e) {
        String[] args = { getExceptionMessage(e), file.getFullPath().makeRelative().toString() };
        String message = Messages.format(SearchMessages.TextSearchVisitor_error, args);
        fStatus.add(new Status(IStatus.ERROR, NewSearchUI.PLUGIN_ID, IStatus.ERROR, message, e));
    } catch (StackOverflowError e) {
        String message = SearchMessages.TextSearchVisitor_patterntoocomplex0;
        fStatus.add(new Status(IStatus.ERROR, NewSearchUI.PLUGIN_ID, IStatus.ERROR, message, e));
        return false;
    } finally {
        fNumberOfScannedFiles++;
    }
    if (fProgressMonitor.isCanceled())
        throw new OperationCanceledException(SearchMessages.TextSearchVisitor_canceled);
    return true;
}
Also used : MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IOException(java.io.IOException) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) FileCharSequenceException(org.eclipse.search.internal.core.text.FileCharSequenceProvider.FileCharSequenceException) CoreException(org.eclipse.core.runtime.CoreException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)116 Charset (java.nio.charset.Charset)55 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)43 IOException (java.io.IOException)35 File (java.io.File)11 ByteBuffer (java.nio.ByteBuffer)11 InputStream (java.io.InputStream)10 UnsupportedEncodingException (java.io.UnsupportedEncodingException)10 CoreException (org.eclipse.core.runtime.CoreException)10 CharacterCodingException (java.nio.charset.CharacterCodingException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 HashMap (java.util.HashMap)7 MediaType (okhttp3.MediaType)7 Request (okhttp3.Request)7 Response (okhttp3.Response)7 ResponseBody (okhttp3.ResponseBody)7 Buffer (okio.Buffer)7 BufferedSource (okio.BufferedSource)7 InputStreamReader (java.io.InputStreamReader)6 OutputStream (java.io.OutputStream)6