001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027package org.apache.http.impl; 028 029import org.apache.http.config.ConnectionConfig; 030 031import java.nio.charset.Charset; 032import java.nio.charset.CharsetDecoder; 033import java.nio.charset.CharsetEncoder; 034import java.nio.charset.CodingErrorAction; 035 036/** 037 * Connection support methods. 038 * 039 * @since 4.3 040 */ 041public final class ConnSupport { 042 043 public static CharsetDecoder createDecoder(final ConnectionConfig cconfig) { 044 if (cconfig == null) { 045 return null; 046 } 047 final Charset charset = cconfig.getCharset(); 048 final CodingErrorAction malformed = cconfig.getMalformedInputAction(); 049 final CodingErrorAction unmappable = cconfig.getUnmappableInputAction(); 050 if (charset != null) { 051 return charset.newDecoder() 052 .onMalformedInput(malformed != null ? malformed : CodingErrorAction.REPORT) 053 .onUnmappableCharacter(unmappable != null ? unmappable: CodingErrorAction.REPORT); 054 } else { 055 return null; 056 } 057 } 058 059 public static CharsetEncoder createEncoder(final ConnectionConfig cconfig) { 060 if (cconfig == null) { 061 return null; 062 } 063 final Charset charset = cconfig.getCharset(); 064 if (charset != null) { 065 final CodingErrorAction malformed = cconfig.getMalformedInputAction(); 066 final CodingErrorAction unmappable = cconfig.getUnmappableInputAction(); 067 return charset.newEncoder() 068 .onMalformedInput(malformed != null ? malformed : CodingErrorAction.REPORT) 069 .onUnmappableCharacter(unmappable != null ? unmappable: CodingErrorAction.REPORT); 070 } else { 071 return null; 072 } 073 } 074 075}