Java Code Examples for java.nio.charset.Charset

The following code examples are extracted from open source projects. You can click to vote up the examples that are useful to you.

Example 1

From project adbcj, under directory /postgresql/codec/src/main/java/org/adbcj/postgresql/codec/backend/.

Source file: BackendMessageDecoder.java

  33 
vote

private AbstractBackendMessage decodeParameterStatus(DecoderInputStream input) throws IOException {
  Charset charset=connectionState.getBackendCharset();
  String name=input.readString(charset);
  String value=input.readString(charset);
  ConfigurationVariable cv=ConfigurationVariable.fromName(name);
  if (cv == null) {
    logger.warn("No ConfigurationVariable entry for {}",name);
  }
  return new ParameterMessage(cv,value);
}
 

Example 2

From project alljoyn_java, under directory /src/org/alljoyn/bus/.

Source file: BusAttachment.java

  33 
vote

/** 
 * Convert to UTF-8 for native code.  This is intended for sensitive string data (i.e. passwords).  The native code must take care of scrubbing the buffer when done. This method can be called from a listener object and must therefore be MT-Safe.
 * @param charArray the sensitive string
 * @return the UTF-8 encoded version of the string
 */
static byte[] encode(char[] charArray){
  try {
    Charset charset=Charset.forName("UTF-8");
    CharsetEncoder encoder=charset.newEncoder();
    ByteBuffer bb=encoder.encode(CharBuffer.wrap(charArray));
    byte[] ba=new byte[bb.limit()];
    bb.get(ba);
    return ba;
  }
 catch (  CharacterCodingException ex) {
    BusException.log(ex);
    return null;
  }
}
 

Example 3

From project big-data-plugin, under directory /src/org/pentaho/amazon/s3/.

Source file: S3FileOutputDialog.java

  33 
vote

private void setEncodings(){
  if (!gotEncodings) {
    gotEncodings=true;
    wEncoding.removeAll();
    List<Charset> values=new ArrayList<Charset>(Charset.availableCharsets().values());
    for (int i=0; i < values.size(); i++) {
      Charset charSet=(Charset)values.get(i);
      wEncoding.add(charSet.displayName());
    }
    String defEncoding=Const.getEnvironmentVariable("file.encoding","UTF-8");
    int idx=Const.indexOfString(defEncoding,wEncoding.getItems());
    if (idx >= 0)     wEncoding.select(idx);
  }
}
 

Example 4

From project commons-io, under directory /src/test/java/org/apache/commons/io/input/.

Source file: CharSequenceInputStreamTest.java

  33 
vote

@Test public void testCharsetMismatchInfiniteLoop() throws IOException {
  char[] inputChars=new char[]{(char)0xE0,(char)0xB2,(char)0xA0};
  Charset charset=Charset.forName("ASCII");
  InputStream stream=new CharSequenceInputStream(new String(inputChars),charset,512);
  try {
    while (stream.read() != -1) {
    }
  }
  finally {
    stream.close();
  }
}
 

Example 5

From project android_packages_apps_Tag, under directory /canon/src/com/android/apps/tagcanon/.

Source file: TagCanon.java

  32 
vote

public static NdefRecord newTextRecord(String text,Locale locale,boolean encodeInUtf8){
  Preconditions.checkNotNull(text);
  Preconditions.checkNotNull(locale);
  byte[] langBytes=locale.getLanguage().getBytes(Charsets.US_ASCII);
  Charset utfEncoding=encodeInUtf8 ? Charsets.UTF_8 : Charset.forName("UTF-16");
  byte[] textBytes=text.getBytes(utfEncoding);
  int utfBit=encodeInUtf8 ? 0 : (1 << 7);
  char status=(char)(utfBit + langBytes.length);
  byte[] data=Bytes.concat(new byte[]{(byte)status},langBytes,textBytes);
  return new NdefRecord(NdefRecord.TNF_WELL_KNOWN,NdefRecord.RTD_TEXT,new byte[0],data);
}
 

Example 6

From project components-ness-httpclient, under directory /client/src/main/java/com/nesscomputing/httpclient/factory/httpclient4/.

Source file: BetterStringEntity.java

  32 
vote

/** 
 * Creates a StringEntity with the specified content and charset
 * @param string content to be used. Not {@code null}.
 * @param charset character set to be used. May be {@code null}, in which case the default is  {@link HTTP#DEFAULT_CONTENT_CHARSET} i.e. "ISO-8859-1"
 * @throws IllegalArgumentException if the string parameter is null
 */
BetterStringEntity(final String string,Charset charset){
  super();
  Preconditions.checkArgument(string != null,"Source string may not be null");
  final Charset charsetObj=ObjectUtils.firstNonNull(charset,Charsets.ISO_8859_1);
  this.content=string.getBytes(charsetObj);
  setContentType(HTTP.PLAIN_TEXT_TYPE + HTTP.CHARSET_PARAM + charsetObj.name());
}
 

Example 7

From project connectbot, under directory /src/sk/vx/connectbot/service/.

Source file: Relay.java

  32 
vote

public void setCharset(String encoding){
  Log.d("ConnectBot.Relay","changing charset to " + encoding);
  Charset charset;
  if (encoding.equals("CP437"))   charset=new IBM437("IBM437",new String[]{"IBM437","CP437"});
 else   charset=Charset.forName(encoding);
  if (charset == currentCharset || charset == null)   return;
  CharsetDecoder newCd=charset.newDecoder();
  newCd.onUnmappableCharacter(CodingErrorAction.REPLACE);
  newCd.onMalformedInput(CodingErrorAction.REPLACE);
  currentCharset=charset;
synchronized (this) {
    decoder=newCd;
  }
}
 

Example 8

From project core_4, under directory /impl/src/main/java/org/richfaces/resource/.

Source file: UserResourceWrapperImpl.java

  32 
vote

@Override public InputStream getInputStream() throws IOException {
  Charset charset=Util.getCharsetFromContentType(getContentType());
  FacesContextWrapperImpl wrappedContext=FacesContextWrapperImpl.wrap(charset);
  try {
    encode(wrappedContext);
    return wrappedContext.getWrittenDataAsStream();
  }
  finally {
    FacesContextWrapperImpl.unwrap();
  }
}
 

Example 9

From project crest, under directory /core/src/main/java/org/codegist/crest/entity/.

Source file: UrlEncodedFormEntityWriter.java

  32 
vote

/** 
 * @inheritDoc
 */
public void writeTo(Request request,OutputStream out) throws IOException {
  Charset charset=request.getMethodConfig().getCharset();
  Writer writer=new OutputStreamWriter(out,charset);
  join(writer,request.getEncodedParamsIterator(FORM),'&');
  writer.flush();
}
 

Example 10

From project agraph-java-client, under directory /src/com/franz/agraph/http/.

Source file: AGHttpRepoClient.java

  31 
vote

public void upload(final Reader contents,String baseURI,final RDFFormat dataFormat,boolean overwrite,Resource... contexts) throws RDFParseException, AGHttpException {
  final Charset charset=dataFormat.hasCharset() ? dataFormat.getCharset() : Charset.forName("UTF-8");
  RequestEntity entity=new RequestEntity(){
    public long getContentLength(){
      return -1;
    }
    public String getContentType(){
      String format=dataFormat.getDefaultMIMEType();
      if (format.contains("turtle")) {
        format="text/turtle";
      }
      return format + "; charset=" + charset.name();
    }
    public boolean isRepeatable(){
      return false;
    }
    public void writeRequest(    OutputStream out) throws IOException {
      OutputStreamWriter writer=new OutputStreamWriter(out,charset);
      IOUtil.transfer(contents,writer);
      writer.flush();
    }
  }
;
  upload(entity,baseURI,overwrite,null,null,null,contexts);
}
 

Example 11

From project archive-commons, under directory /archive-commons/src/test/java/org/archive/format/gzip/zipnum/.

Source file: ZipNumWriterTest.java

  31 
vote

public void testAddRecord() throws IOException {
  Charset UTF8=Charset.forName("UTF-8");
  File main=File.createTempFile("test-znw",".main");
  File summ=File.createTempFile("test-znw",".summ");
  main.deleteOnExit();
  summ.deleteOnExit();
  System.out.format("Summ: %s\n",summ.getAbsolutePath());
  int limit=10;
  ZipNumWriter znw=new ZipNumWriter(new FileOutputStream(main,false),new FileOutputStream(summ,false),limit);
  for (int i=0; i < 1000; i++) {
    znw.addRecord(String.format("%06d\n",i).getBytes(UTF8));
  }
  znw.close();
  InputStreamReader isr=new InputStreamReader(new FileInputStream(summ),UTF8);
  BufferedReader br=new BufferedReader(isr);
  String line=null;
  int count=0;
  while (true) {
    line=br.readLine();
    if (line == null) {
      break;
    }
    String parts[]=line.split("\t");
    FileChannel fc=new RandomAccessFile(main,"r").getChannel();
    long offset=Long.parseLong(parts[0]);
    int len=Integer.parseInt(parts[1]);
    byte[] gz=new byte[len];
    ByteBuffer bb=ByteBuffer.wrap(gz);
    int amt=fc.read(bb,offset);
    assertEquals(amt,len);
    ByteArrayInputStream bais=new ByteArrayInputStream(gz);
    GZIPMemberSeries gzms=new GZIPMemberSeries(new SimpleStream(bais));
    GZIPSeriesMember m=gzms.getNextMember();
    m.skipMember();
    gzms.close();
    count++;
  }
  assertEquals(count,100);
  br.close();
}
 

Example 12

From project boilerpipe_1, under directory /boilerpipe-core/src/main/de/l3s/boilerpipe/sax/.

Source file: HTMLFetcher.java

  31 
vote

/** 
 * Fetches the document at the given URL, using  {@link URLConnection}.
 * @param url
 * @return
 * @throws IOException
 */
public static HTMLDocument fetch(final URL url) throws IOException {
  final URLConnection conn=url.openConnection();
  final String ct=conn.getContentType();
  Charset cs=Charset.forName("Cp1252");
  if (ct != null) {
    Matcher m=PAT_CHARSET.matcher(ct);
    if (m.find()) {
      final String charset=m.group(1);
      try {
        cs=Charset.forName(charset);
      }
 catch (      UnsupportedCharsetException e) {
      }
    }
  }
  InputStream in=conn.getInputStream();
  final String encoding=conn.getContentEncoding();
  if (encoding != null) {
    if ("gzip".equalsIgnoreCase(encoding)) {
      in=new GZIPInputStream(in);
    }
 else {
      System.err.println("WARN: unsupported Content-Encoding: " + encoding);
    }
  }
  ByteArrayOutputStream bos=new ByteArrayOutputStream();
  byte[] buf=new byte[4096];
  int r;
  while ((r=in.read(buf)) != -1) {
    bos.write(buf,0,r);
  }
  in.close();
  final byte[] data=bos.toByteArray();
  return new HTMLDocument(data,cs);
}
 

Example 13

From project cdk, under directory /maven-resources-plugin/src/main/java/org/richfaces/cdk/.

Source file: ProcessMojo.java

  31 
vote

private Collection<ResourceProcessor> getDefaultResourceProcessors(){
  Charset charset=Charset.defaultCharset();
  if (!Strings.isNullOrEmpty(encoding)) {
    charset=Charset.forName(encoding);
  }
 else {
    getLog().warn("Encoding is not set explicitly, CDK resources plugin will use default platform encoding for processing char-based resources");
  }
  if (compress) {
    return Arrays.<ResourceProcessor>asList(new JavaScriptCompressingProcessor(charset,getLog()),new CSSCompressingProcessor(charset));
  }
 else {
    return Arrays.<ResourceProcessor>asList(new JavaScriptPackagingProcessor(charset));
  }
}
 

Example 14

From project clustermeister, under directory /provisioning/src/main/java/com/github/nethad/clustermeister/provisioning/utils/.

Source file: SSHClientImpl.java

  31 
vote

@Override public void setCredentials(KeyPairCredentials credentials) throws SSHClientException {
  this.credentials=credentials;
  try {
    Charset charset=credentials.getKeySourceCharset();
    Optional<String> publicKeyOptional=credentials.getPublicKey();
    byte[] publicKey;
    if (publicKeyOptional.isPresent()) {
      publicKey=publicKeyOptional.get().getBytes(charset);
    }
 else {
      publicKey=null;
    }
    this.addIdentity(credentials.getName(),credentials.getPrivateKey().getBytes(charset),publicKey,null);
  }
 catch (  IOException ex) {
    throw new SSHClientException(ex);
  }
}
 

Example 15

From project coala, under directory /modules/coala-communication/src/main/java/org/openehealth/coala/communication/.

Source file: PdqMessageBuilderImpl.java

  31 
vote

@Override public String buildPdqRequest(String thePatientId,String thePatientIdAssigningAuthorityUniversalId,String theGivenName,String theLastName,Date theDoB){
  String thePatientIdStr="", theGivenNameStr="", theLastNameStr="", theDoBStr="", isAnd="";
  if (!isNullorEmpty(thePatientId) && !isNullorEmpty(thePatientIdAssigningAuthorityUniversalId)) {
    thePatientIdStr=PID_PatientID + thePatientId + "~"+ PID_PatientIdAssigningAuthorityUniversalId+ thePatientIdAssigningAuthorityUniversalId+ "~"+ PID_PatientIdAssigningAuthorityUniversalIdType+ HL7_PatientIdAssigningAuthorityUniversalIdType;
    isAnd="~";
  }
  Charset charset=Charset.forName("ISO-8859-1");
  if (!isNullorEmpty(theGivenName)) {
    String givenNameISO=new String(theGivenName.getBytes(),charset);
    theGivenName=givenNameISO;
    theGivenNameStr=isAnd + PID_GivenName + theGivenName;
    isAnd="~";
  }
  if (!isNullorEmpty(theLastName)) {
    String lastNameISO=new String(theLastName.getBytes(),charset);
    theLastName=lastNameISO;
    theLastNameStr=isAnd + PID_LastName + theLastName;
    isAnd="~";
  }
  if (theDoB != null) {
    theDoBStr=isAnd + PID_DoB + pxsDateConverter.DateToShortString(theDoB);
  }
  final String QBP_MESSAGE_TYPE="QBP^Q22";
  String aMessageHeader=generateMSH(QBP_MESSAGE_TYPE);
  String aQPDSegment="QPD|Q22^Find Candidates" + HL7_SEPERATOR + getRandomNumber(17)+ getRandomNumber(18)+ HL7_SEPERATOR+ thePatientIdStr+ theGivenNameStr+ theLastNameStr+ theDoBStr+ HL7_SEPERATOR;
  final String RPC_SEGMENT="RCP" + HL7_SEPERATOR + "I"+ HL7_SEPERATOR;
  String hl7request=aMessageHeader + "\n" + aQPDSegment+ "\n"+ RPC_SEGMENT;
  LOG.info(hl7request);
  return hl7request;
}
 

Example 16

From project cogroo4, under directory /cogroo-gc/src/main/java/org/cogroo/gc/cmdline/dictionary/.

Source file: POSDictionaryBuilderTool.java

  31 
vote

public void run(String[] args){
  Params params=validateAndParseParams(args,Params.class);
  File dictInFile=params.getInputFile();
  File dictOutFile=params.getOutputFile();
  File corpusFile=params.getCorpus();
  Charset encoding=params.getEncoding();
  CmdLineUtil.checkInputFile("dictionary input file",dictInFile);
  CmdLineUtil.checkOutputFile("dictionary output file",dictOutFile);
  CmdLineUtil.checkInputFile("corpus input file",corpusFile);
  InputStreamReader in=null;
  OutputStream out=null;
  try {
    ADFeaturizerSampleStream sentenceStream=new ADFeaturizerSampleStream(new FileInputStream(corpusFile),"ISO-8859-1",false);
    Set<String> knownFeats=new HashSet<String>();
    Set<String> knownPostags=new HashSet<String>();
    FeatureSample sample=sentenceStream.read();
    while (sample != null) {
      Collections.addAll(knownFeats,sample.getFeatures());
      Collections.addAll(knownPostags,sample.getTags());
      sample=sentenceStream.read();
    }
    in=new InputStreamReader(new FileInputStream(dictInFile),encoding);
    out=new FileOutputStream(dictOutFile);
    ExtendedPOSDictionary dict=MyPOSDictionary.parseOneEntryPerLine(in,new JspellTagInterpreter(),new FlorestaTagInterpreter(),knownFeats,knownPostags,params.getAllowInvalidFeats());
    dict.serialize(out);
  }
 catch (  IOException e) {
    throw new TerminateToolException(-1,"IO error while reading training data or indexing data: " + e.getMessage());
  }
 finally {
    try {
      in.close();
      out.close();
    }
 catch (    IOException e) {
    }
  }
}
 

Example 17

From project commons-compress, under directory /src/main/java/org/apache/commons/compress/archivers/zip/.

Source file: ZipEncodingHelper.java

  31 
vote

/** 
 * Instantiates a zip encoding.
 * @param name The name of the zip encoding. Specify {@code null} forthe platform's default encoding.
 * @return A zip encoding for the given encoding name.
 */
public static ZipEncoding getZipEncoding(String name){
  if (isUTF8(name)) {
    return UTF8_ZIP_ENCODING;
  }
  if (name == null) {
    return new FallbackZipEncoding();
  }
  SimpleEncodingHolder h=simpleEncodings.get(name);
  if (h != null) {
    return h.getEncoding();
  }
  try {
    Charset cs=Charset.forName(name);
    return new NioZipEncoding(cs);
  }
 catch (  UnsupportedCharsetException e) {
    return new FallbackZipEncoding(name);
  }
}
 

Example 18

From project core_1, under directory /security/src/main/java/org/switchyard/security/credential/extract/.

Source file: AuthorizationHeaderCredentialsExtractor.java

  31 
vote

/** 
 * Constructs a new AuthorizationHeaderCredentialsExtractor, with the specified charset name.
 * @param charsetName the specified charset name
 */
public AuthorizationHeaderCredentialsExtractor(String charsetName){
  if (charsetName != null) {
    Charset charset;
    try {
      charset=Charset.forName(charsetName);
    }
 catch (    Throwable t) {
      LOGGER.error("charsetName [" + charsetName + "] + is illegal or unsupported; using platform-default");
      charset=Charset.defaultCharset();
    }
    _charset=charset;
  }
 else {
    LOGGER.warn("charsetName is null; using platform-default");
    _charset=Charset.defaultCharset();
  }
}
 

Example 19

From project acceleo-webapp-generator, under directory /plugins/org.eclipse.acceleo.tutorial/src/org/eclipse/acceleo/tutorial/wizard/.

Source file: EclipseConWizard.java

  29 
vote

/** 
 * Tries and find an encoding value on the very first line of the file contents.
 * @param fileContent The content from which to read an encoding value.
 * @return The charset name if it exists and is supported, <code>null</code> otherwise
 */
private static String getCharset(String fileContent){
  String trimmedContent=fileContent.trim();
  String charsetName=null;
  if (trimmedContent.length() > 0) {
    BufferedReader reader=new BufferedReader(new StringReader(trimmedContent));
    String firstLine=trimmedContent;
    try {
      firstLine=reader.readLine();
    }
 catch (    IOException e) {
    }
    Pattern encodingPattern=Pattern.compile("encoding\\s*=\\s*(\"|\')?([-a-zA-Z0-9]+)\1?");
    Matcher matcher=encodingPattern.matcher(firstLine);
    if (matcher.find()) {
      charsetName=matcher.group(2);
    }
  }
  if (charsetName != null && Charset.isSupported(charsetName)) {
    return charsetName;
  }
  return null;
}
 

Example 20

From project action-core, under directory /src/main/java/com/ning/metrics/action/hdfs/data/parser/.

Source file: WritableRowSerializer.java

  29 
vote

@Override public Rows toRows(Registrar r,Object value) throws RowAccessException {
  Row row;
  if (value instanceof Text) {
    String[] data=value.toString().split("\t");
    List<ColumnKey> columnKeyList=new ArrayList<ColumnKey>();
    for (int i=0; i < data.length; i++) {
      columnKeyList.add(new DynamicColumnKey(String.valueOf("col-" + i)));
    }
    row=RowFactory.getRow(new RowSchema("Text",columnKeyList),Arrays.asList(data));
  }
 else   if (value instanceof BytesWritable) {
    byte[] data=((BytesWritable)value).getBytes();
    ArrayList<String> listData=new ArrayList<String>();
    listData.add(new String(data,Charset.forName("UTF-8")));
    row=RowFactory.getRow(new RowSchema("BytesWritable",new DynamicColumnKey(String.valueOf("col-1"))),listData);
  }
 else   if (value instanceof RowThrift) {
    row=(RowThrift)value;
  }
 else {
    throw new RowAccessException(String.format("Writable [%s] is not a known row type",value == null ? null : value.getClass()));
  }
  Rows rows=new Rows();
  rows.add(row);
  return rows;
}
 

Example 21

From project agileBase, under directory /gtpb_server/src/com/gtwm/pb/model/manageData/.

Source file: ViewTools.java

  29 
vote

public String getCommitUrl() throws IOException, CantDoThatException {
  String commitFileName=this.request.getSession().getServletContext().getRealPath("/lastcommit.txt");
  File commitFile=new File(commitFileName);
  try {
    InputStream inputStream=new FileInputStream(commitFileName);
    BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream,Charset.forName("UTF-8")));
    String commitLine=reader.readLine();
    String commitId=commitLine.replace("commit ","");
    inputStream.close();
    return "https://github.com/okohll/agileBase/commit/" + commitId;
  }
 catch (  FileNotFoundException fnfex) {
    logger.error("Commit file " + commitFileName + " not found: "+ fnfex);
    throw new CantDoThatException("Commit log not found");
  }
}
 

Example 22

From project aim3-tu-berlin, under directory /seminar/exercises/datamining/core/src/test/java/de/tuberlin/dima/aim/exercises/.

Source file: HadoopAndPactTestcase.java

  29 
vote

protected static void writeLines(File file,Iterable<String> lines) throws FileNotFoundException {
  PrintWriter writer=new PrintWriter(new OutputStreamWriter(new FileOutputStream(file),Charset.forName("UTF-8")));
  try {
    for (    String line : lines) {
      writer.println(line);
    }
  }
  finally {
    writer.close();
  }
}
 

Example 23

From project AirReceiver, under directory /src/main/java/org/phlo/AirReceiver/.

Source file: RtspLoggingHandler.java

  29 
vote

@Override public void messageReceived(final ChannelHandlerContext ctx,final MessageEvent evt) throws Exception {
  final HttpRequest req=(HttpRequest)evt.getMessage();
  final Level level=Level.FINE;
  if (s_logger.isLoggable(level)) {
    final String content=req.getContent().toString(Charset.defaultCharset());
    final StringBuilder s=new StringBuilder();
    s.append(">");
    s.append(req.getMethod());
    s.append(" ");
    s.append(req.getUri());
    s.append("\n");
    for (    final Map.Entry<String,String> header : req.getHeaders()) {
      s.append("  ");
      s.append(header.getKey());
      s.append(": ");
      s.append(header.getValue());
      s.append("\n");
    }
    s.append(content);
    s_logger.log(Level.FINE,s.toString());
  }
  super.messageReceived(ctx,evt);
}
 

Example 24

From project ambrose, under directory /common/src/main/java/com/twitter/ambrose/util/.

Source file: JSONUtil.java

  29 
vote

public static String readFile(String path) throws IOException {
  FileInputStream stream=new FileInputStream(new File(path));
  try {
    FileChannel fc=stream.getChannel();
    MappedByteBuffer bb=fc.map(FileChannel.MapMode.READ_ONLY,0,fc.size());
    return Charset.defaultCharset().decode(bb).toString();
  }
  finally {
    stream.close();
  }
}
 

Example 25

From project AmDroid, under directory /httpclientandroidlib/src/ch/boye/httpclientandroidlib/entity/mime/content/.

Source file: StringBody.java

  29 
vote

/** 
 * @since 4.1
 */
public static StringBody create(final String text,final String mimeType,final Charset charset) throws IllegalArgumentException {
  try {
    return new StringBody(text,mimeType,charset);
  }
 catch (  UnsupportedEncodingException ex) {
    throw new IllegalArgumentException("Charset " + charset + " is not supported",ex);
  }
}
 

Example 26

From project Android-Terminal-Emulator, under directory /libraries/emulatorview/src/jackpal/androidterm/emulatorview/.

Source file: TerminalEmulator.java

  29 
vote

/** 
 * Construct a terminal emulator that uses the supplied screen
 * @param session the terminal session the emulator is attached to
 * @param screen the screen to render characters into.
 * @param columns the number of columns to emulate
 * @param rows the number of rows to emulate
 * @param scheme the default color scheme of this emulator
 */
public TerminalEmulator(TermSession session,Screen screen,int columns,int rows,ColorScheme scheme){
  mSession=session;
  mScreen=screen;
  mRows=rows;
  mColumns=columns;
  mTabStop=new boolean[mColumns];
  setColorScheme(scheme);
  mUTF8ByteBuffer=ByteBuffer.allocate(4);
  mInputCharBuffer=CharBuffer.allocate(2);
  mUTF8Decoder=Charset.forName("UTF-8").newDecoder();
  mUTF8Decoder.onMalformedInput(CodingErrorAction.REPLACE);
  mUTF8Decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
  reset();
}
 

Example 27

From project android_external_guava, under directory /src/com/google/common/io/.

Source file: CharStreams.java

  29 
vote

/** 
 * Returns a factory that will supply instances of  {@link InputStreamReader}, using the given  {@link InputStream} factory and character set.
 * @param in the factory that will be used to open input streams
 * @param charset the character set used to decode the input stream
 * @return the factory
 */
public static InputSupplier<InputStreamReader> newReaderSupplier(final InputSupplier<? extends InputStream> in,final Charset charset){
  Preconditions.checkNotNull(in);
  Preconditions.checkNotNull(charset);
  return new InputSupplier<InputStreamReader>(){
    public InputStreamReader getInput() throws IOException {
      return new InputStreamReader(in.getInput(),charset);
    }
  }
;
}
 

Example 28

From project android_packages_apps_Exchange, under directory /src/com/android/exchange/utility/.

Source file: UriCodec.java

  29 
vote

/** 
 * Encodes  {@code s} and appends the result to {@code builder}.
 * @param isPartiallyEncoded true to fix input that has already beenpartially or fully encoded. For example, input of "hello%20world" is unchanged with isPartiallyEncoded=true but would be double-escaped to "hello%2520world" otherwise.
 */
private void appendEncoded(StringBuilder builder,String s,Charset charset,boolean isPartiallyEncoded){
  if (s == null) {
    throw new NullPointerException();
  }
  int escapeStart=-1;
  for (int i=0; i < s.length(); i++) {
    char c=s.charAt(i);
    if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')|| isRetained(c)|| (c == '%' && isPartiallyEncoded)) {
      if (escapeStart != -1) {
        appendHex(builder,s.substring(escapeStart,i),charset);
        escapeStart=-1;
      }
      if (c == '%' && isPartiallyEncoded) {
        builder.append(s,i,i + 3);
        i+=2;
      }
 else       if (c == ' ') {
        builder.append('+');
      }
 else {
        builder.append(c);
      }
    }
 else     if (escapeStart == -1) {
      escapeStart=i;
    }
  }
  if (escapeStart != -1) {
    appendHex(builder,s.substring(escapeStart,s.length()),charset);
  }
}
 

Example 29

From project android_packages_apps_Nfc, under directory /src/com/android/nfc/handover/.

Source file: HandoverManager.java

  29 
vote

BluetoothHandoverData parseNokia(ByteBuffer payload){
  BluetoothHandoverData result=new BluetoothHandoverData();
  result.valid=false;
  try {
    payload.position(1);
    byte[] address=new byte[6];
    payload.get(address);
    result.device=mBluetoothAdapter.getRemoteDevice(address);
    result.valid=true;
    payload.position(14);
    int nameLength=payload.get();
    byte[] nameBytes=new byte[nameLength];
    payload.get(nameBytes);
    result.name=new String(nameBytes,Charset.forName("UTF-8"));
  }
 catch (  IllegalArgumentException e) {
    Log.i(TAG,"nokia: invalid BT address");
  }
catch (  BufferUnderflowException e) {
    Log.i(TAG,"nokia: payload shorter than expected");
  }
  if (result.valid && result.name == null)   result.name="";
  return result;
}
 

Example 30

From project androlog, under directory /androlog/src/main/java/de/akquinet/android/androlog/reporter/.

Source file: PostReporter.java

  29 
vote

/** 
 * Executes the given request as a HTTP POST action.
 * @param url the url
 * @param params the parameter
 * @return the response as a String.
 * @throws IOException if the server cannot be reached
 */
public static void post(URL url,String params) throws IOException {
  URLConnection conn=url.openConnection();
  if (conn instanceof HttpURLConnection) {
    ((HttpURLConnection)conn).setRequestMethod("POST");
    conn.setRequestProperty("Content-Type","application/json");
  }
  OutputStreamWriter writer=null;
  try {
    conn.setDoOutput(true);
    writer=new OutputStreamWriter(conn.getOutputStream(),Charset.forName("UTF-8"));
    writer.write(params);
    writer.flush();
  }
  finally {
    if (writer != null) {
      writer.close();
    }
  }
}
 

Example 31

From project ant4eclipse, under directory /org.ant4eclipse.ant.jdt/src/org/ant4eclipse/ant/jdt/ecj/.

Source file: A4ECompilerAdapter.java

  29 
vote

/** 
 * <p> Helper method. Returns the default encoding of the eclipse workspace. </p>
 * @return the default encoding
 */
private String getDefaultEncoding(){
  String property=getProject().getProperty(ANT4ECLIPSE_DEFAULT_FILE_ENCODING);
  if (property != null) {
    return property;
  }
  String encoding=getJavac().getEncoding();
  if (encoding != null) {
    return encoding;
  }
  if (Os.isFamily(Os.FAMILY_WINDOWS) && Charset.isSupported("Cp1252")) {
    return "Cp1252";
  }
 else   if (Os.isFamily(Os.FAMILY_UNIX) && Charset.isSupported("UTF-8")) {
    return "UTF-8";
  }
 else   if (Os.isFamily(Os.FAMILY_MAC) && Charset.isSupported("MacRoman")) {
    return "MacRoman";
  }
  return System.getProperty("file.encoding");
}
 

Example 32

From project any23, under directory /core/src/main/java/org/apache/any23/extractor/html/.

Source file: TagSoupParser.java

  29 
vote

public TagSoupParser(InputStream input,String documentURI,String encoding){
  if (encoding != null && !Charset.isSupported(encoding))   throw new UnsupportedCharsetException(String.format("Charset %s is not supported",encoding));
  this.input=input;
  this.documentURI=documentURI;
  this.encoding=encoding;
}
 

Example 33

From project ARCInputFormat, under directory /src/org/commoncrawl/util/shared/.

Source file: ArcFileReader.java

  29 
vote

/** 
 * construct a reader given a list of ByteBuffers 
 */
private static InputStreamReader readerFromScanBufferList(LinkedList<ByteBuffer> buffers,Charset charset) throws IOException {
  Vector<InputStream> inputStreams=new Vector<InputStream>();
  for (  ByteBuffer buffer : buffers) {
    inputStreams.add(newInputStream(buffer));
  }
  buffers.clear();
  SequenceInputStream seqInputStream=new SequenceInputStream(inputStreams.elements());
  ;
  return new InputStreamReader(seqInputStream,charset);
}
 

Example 34

From project arquillian-container-openshift, under directory /openshift-express/src/main/java/org/jboss/arquillian/container/openshift/express/.

Source file: OpenShiftExpressContainer.java

  29 
vote

@Override public void deploy(Descriptor descriptor) throws DeploymentException {
  long beforeDeploy=System.currentTimeMillis();
  OpenShiftRepository repo=repository.get();
  InputStream is=new ByteArrayInputStream(descriptor.getDescriptorName().getBytes(Charset.defaultCharset()));
  repo.addAndPush(descriptor.getDescriptorName(),is);
  if (log.isLoggable(Level.FINE)) {
    log.fine("Deployment of " + descriptor.getDescriptorName() + " took "+ (System.currentTimeMillis() - beforeDeploy)+ "ms");
  }
}
 

Example 35

From project arquillian-extension-warp, under directory /impl/src/main/java/org/jboss/arquillian/warp/impl/client/execution/.

Source file: DefaultResponseDeenrichmentService.java

  29 
vote

@Override public void deenrichResponse(HttpResponse response){
  try {
    final ChannelBuffer content=response.getContent();
    long originalLength=HttpHeaders.getContentLength(response);
    int payloadLength=Integer.valueOf(getHeader(response));
    String responseEnrichment=content.toString(0,payloadLength,Charset.defaultCharset());
    content.readerIndex(payloadLength);
    content.discardReadBytes();
    ResponsePayload payload=SerializationUtils.deserializeFromBase64(responseEnrichment);
    HttpHeaders.setContentLength(response,originalLength - payloadLength);
    HttpResponseStatus status=HttpResponseStatus.valueOf(payload.getStatus());
    response.setStatus(status);
    AssertionHolder.addResponse(new ResponseEnrichment(payload));
  }
 catch (  Exception originalException) {
    ResponsePayload exceptionPayload=new ResponsePayload();
    ClientWarpExecutionException explainingException=new ClientWarpExecutionException("deenriching response failed: " + originalException.getMessage(),originalException);
    exceptionPayload.setTestResult(new TestResult(Status.FAILED,explainingException));
    AssertionHolder.addResponse(new ResponseEnrichment(exceptionPayload));
  }
}
 

Example 36

From project arquillian-graphene, under directory /graphene-selenium/graphene-selenium-impl/src/main/java/org/jboss/arquillian/ajocado/utils/.

Source file: URLUtils.java

  29 
vote

/** 
 * Encodes credentials using Base64 encoding, which can be used to build a header for HTTP Basic authorization
 * @param username User name to be encoded
 * @param password Password to be encoded
 * @return A string {@code username:password} encoded with Base64
 */
public static String encodeBase64Credentials(String username,String password){
  String credentials=username + ":" + password;
  try {
    String defaultCharsetName=Charset.defaultCharset().displayName();
    byte[] credentialsAsBytes=credentials.getBytes(defaultCharsetName);
    byte[] encodedCredentials=Base64.encodeBase64(credentialsAsBytes);
    return new String(encodedCredentials,defaultCharsetName);
  }
 catch (  UnsupportedEncodingException e) {
    throw new IllegalStateException(e);
  }
}
 

Example 37

From project artifactory-plugin, under directory /src/test/java/org/jfrog/hudson/release/maven/.

Source file: PomTransformerTest.java

  29 
vote

@Test public void transformSimplePom() throws Exception {
  File pomFile=getResourceAsFile("/poms/parentonly/pom.xml");
  HashMap<ModuleName,String> modules=Maps.newHashMap();
  modules.put(new ModuleName("org.jfrog.test","parent"),"2.2");
  new PomTransformer(new ModuleName("org.jfrog.test","one"),modules,"",false).invoke(pomFile,null);
  String pomStr=Files.toString(pomFile,Charset.defaultCharset());
  String expectedStr=Files.toString(getResourceAsFile("/poms/parentonly/pom.expected.xml"),Charset.defaultCharset());
  assertEquals(expectedStr,pomStr);
}
 

Example 38

From project asterisk-java, under directory /src/main/java/org/asteriskjava/manager/event/.

Source file: SkypeChatMessageEvent.java

  29 
vote

/** 
 * Returns the decoded message.
 * @return the decoded message.
 */
public String getDecodedMessage(){
  if (message == null) {
    return null;
  }
  return new String(Base64.base64ToByteArray(message),Charset.forName("UTF-8"));
}
 

Example 39

From project astyanax, under directory /src/main/java/com/netflix/astyanax/test/.

Source file: EmbeddedCassandra.java

  29 
vote

public EmbeddedCassandra(File dataDir,String clusterName,int port,int storagePort) throws IOException {
  LOG.info("Starting cassandra in dir " + dataDir);
  this.dataDir=dataDir;
  dataDir.mkdirs();
  URL templateUrl=ClassLoader.getSystemClassLoader().getResource("cassandra-template.yaml");
  Preconditions.checkNotNull(templateUrl,"Cassandra config template is null");
  String baseFile=Resources.toString(templateUrl,Charset.defaultCharset());
  String newFile=baseFile.replace("$DIR$",dataDir.getPath());
  newFile=newFile.replace("$PORT$",Integer.toString(port));
  newFile=newFile.replace("$STORAGE_PORT$",Integer.toString(storagePort));
  newFile=newFile.replace("$CLUSTER$",clusterName);
  File configFile=new File(dataDir,"cassandra.yaml");
  Files.write(newFile,configFile,Charset.defaultCharset());
  LOG.info("Cassandra config file: " + configFile.getPath());
  System.setProperty("cassandra.config","file://" + configFile.getPath());
  try {
    cassandra=new CassandraDaemon();
    cassandra.init(null);
  }
 catch (  IOException e) {
    LOG.error("Error initializing embedded cassandra",e);
    throw e;
  }
  LOG.info("Started cassandra deamon");
}
 

Example 40

From project atlas, under directory /src/main/java/com/ning/atlas/components/chef/.

Source file: UbuntuChefSoloInstaller.java

  29 
vote

private String initServer(Host host,String nodeJson,Deployment d) throws IOException {
  final SSHCredentials creds=lookup(d.getSpace(),credentialName).otherwise(defaultCredentials(d.getSpace())).otherwise(new IllegalStateException("unable to locate any ssh credentials"));
  Server server=d.getSpace().get(host.getId(),Server.class,Missing.RequireAll).getValue();
  SSH ssh=new SSH(creds,server.getExternalAddress());
  try {
    String remote_path="/home/" + creds.getUserName() + "/ubuntu-chef-solo-init.sh";
    ssh.scpUpload(this.chefSoloInitFile,remote_path);
    ssh.exec("chmod +x " + remote_path);
    logger.debug("about to execute chef init script remotely");
    ssh.exec(remote_path);
    File node_json=File.createTempFile("node","json");
    Files.write(nodeJson,node_json,Charset.forName("UTF-8"));
    ssh.scpUpload(node_json,"/tmp/node.json");
    ssh.exec("sudo mv /tmp/node.json /etc/chef/node.json");
    ssh.scpUpload(soloRbFile,"/tmp/solo.rb");
    ssh.exec("sudo mv /tmp/solo.rb /etc/chef/solo.rb");
    ssh.scpUpload(s3InitFile,"/tmp/s3_init.rb");
    ssh.exec("sudo mv /tmp/s3_init.rb /etc/chef/s3_init.rb");
    logger.debug("about to execute initial chef-solo");
    return ssh.exec("sudo chef-solo");
  }
  finally {
    ssh.close();
  }
}
 

Example 41

From project AudioBox.fm-JavaLib, under directory /audiobox.fm-core/src/main/java/fm/audiobox/core/models/.

Source file: MediaFile.java

  29 
vote

public IConnectionMethod upload(boolean async,UploadHandler uploadHandler,boolean customFields) throws ServiceException, LoginException {
  String path=IConnector.URI_SEPARATOR.concat(Actions.upload.toString());
  MultipartEntity entity=new MultipartEntity();
  String mime=MimeTypes.getMime(uploadHandler.getFile());
  if (mime == null) {
    throw new ServiceException("mime type error: file is not supported by AudioBox2");
  }
  entity.addPart("files[]",uploadHandler);
  if (customFields) {
    List<NameValuePair> fields=this.toQueryParametersCustomFields(false);
    for (    NameValuePair field : fields) {
      try {
        entity.addPart(field.getName(),new StringBody(field.getValue(),"text/plain",Charset.forName("UTF-8")));
      }
 catch (      UnsupportedEncodingException e) {
        log.warn("Entity " + field.getName() + " cannot be added due to: "+ e.getMessage());
      }
    }
  }
  IConnectionMethod request=this.getConnector(IConfiguration.Connectors.NODE).post(this,path,null,null);
  request.send(async,entity);
  return request;
}
 

Example 42

From project autopsy, under directory /KeywordSearch/src/org/sleuthkit/autopsy/keywordsearch/.

Source file: AbstractFileChunk.java

  29 
vote

public boolean index(Ingester ingester,byte[] content,long contentSize,Charset indexCharset) throws IngesterException {
  boolean success=true;
  ByteContentStream bcs=new ByteContentStream(content,contentSize,parent.getSourceFile(),indexCharset);
  try {
    ingester.ingest(this,bcs,content.length);
  }
 catch (  Exception ingEx) {
    success=false;
    throw new IngesterException("Problem ingesting file string chunk: " + parent.getSourceFile().getId() + ", chunk: "+ chunkID,ingEx);
  }
  return success;
}
 

Example 43

From project avro, under directory /lang/java/ipc/src/test/java/org/apache/avro/ipc/.

Source file: TestNettyServer.java

  29 
vote

@Test public void testBadRequest() throws IOException {
  int port=server.getPort();
  String msg="GET /status HTTP/1.1\n\n";
  InetSocketAddress sockAddr=new InetSocketAddress("127.0.0.1",port);
  Socket sock=new Socket();
  sock.connect(sockAddr);
  OutputStream out=sock.getOutputStream();
  out.write(msg.getBytes(Charset.forName("UTF-8")));
  out.flush();
  byte[] buf=new byte[2048];
  int bytesRead=sock.getInputStream().read(buf);
  Assert.assertTrue("Connection should have been closed",bytesRead == -1);
}
 

Example 44

From project badgescanner, under directory /src/net/tjohns/badgescanner/.

Source file: NfcField.java

  29 
vote

public void readFromTag(NfcConnection nfcConnection) throws IOException {
  byte[] data=new byte[NFC_BLOCK_LENGTH * mBlockCount];
  int dataLen=0;
  for (int i=mStartBlock; i < mStartBlock + mBlockCount; i++) {
    try {
      byte[] block=nfcConnection.readBlock(i);
      for (int j=0; j < NFC_BLOCK_LENGTH; j++) {
        data[dataLen++]=block[j];
      }
    }
 catch (    NfcConnection.NonDataRegionException e) {
    }
  }
  mValue=new String(data,0,dataLen,Charset.forName("ISO-8859-15")).trim();
}
 

Example 45

From project BMach, under directory /src/bmach/ui/gui/.

Source file: BMachPanel.java

  29 
vote

private void fileDropped(File f){
  try {
    docIOMngr.open(this,f,new BMAFileFilter(),Charset.defaultCharset());
  }
 catch (  IOException ioe) {
    showError("BMach - Error","I/O error:","An error occured while trying to save the file");
  }
}
 

Example 46

From project bndtools, under directory /bndtools.jareditor/src/bndtools/jareditor/internal/.

Source file: JAREntryPart.java

  29 
vote

public JAREntryPart(IEditorPart editor,Composite composite,FormToolkit toolkit){
  this.editor=editor;
  SortedMap<String,Charset> charsetMap=Charset.availableCharsets();
  charsets=new String[charsetMap.size()];
  int i=0;
  for (Iterator<String> iter=charsetMap.keySet().iterator(); iter.hasNext(); i++) {
    charsets[i]=iter.next();
  }
  setSelectedCharset(DEFAULT_CHARSET);
  createContent(composite,toolkit);
}
 

Example 47

From project book, under directory /src/main/java/com/tamingtext/util/.

Source file: SplitInput.java

  29 
vote

/** 
 * Count the lines in the file specified as returned by <code>BufferedReader.readLine()</code>
 * @param inputFile the file whose lines will be counted
 * @param charset the charset of the file to read
 * @return the number of lines in the input file.
 * @throws IOException if there is a problem opening or reading the file.
 */
public static int countLines(FileSystem fs,Path inputFile,Charset charset) throws IOException {
  int lineCount=0;
  BufferedReader countReader=new BufferedReader(new InputStreamReader(fs.open(inputFile),charset));
  try {
    while (countReader.readLine() != null) {
      lineCount++;
    }
  }
  finally {
    IOUtils.close(Collections.singleton(countReader));
  }
  return lineCount;
}
 

Example 48

From project box-android-sdk, under directory /BoxAndroidLibrary/src/com/box/androidlib/FileTransfer/.

Source file: BoxFileUpload.java

  29 
vote

@Override protected String generateContentType(final String boundary,final Charset charset){
  StringBuilder buffer=new StringBuilder();
  buffer.append("multipart/form-data; boundary=");
  buffer.append(boundary);
  if (charset != null) {
  }
  return buffer.toString();
}
 

Example 49

From project bson4jackson, under directory /src/main/java/de/undercouch/bson4jackson/io/.

Source file: DynamicOutputBuffer.java

  29 
vote

/** 
 * @return the lazily created UTF-8 character set
 */
private Charset getUTF8Charset(){
  if (_utf8 == null) {
    _utf8=Charset.forName("UTF-8");
    ;
  }
  return _utf8;
}
 

Example 50

From project build-info, under directory /build-info-extractor/src/test/java/org/jfrog/build/extractor/release/.

Source file: PropertiesTransformerTest.java

  29 
vote

@Test public void testTransformNoEols() throws Exception {
  File properties=File.createTempFile("temp","properties");
  Files.write("momo=1.0popo=2.0",properties,Charset.forName("utf-8"));
  Map<String,String> versions=Maps.newHashMap();
  versions.put("momo","2.0");
  versions.put("popo","3.0");
  boolean transformed=new PropertiesTransformer(properties,versions).transform();
  assertTrue(transformed);
  String transformedProps=Files.toString(properties,Charset.forName("utf-8"));
  assertFalse(transformedProps.contains("\n"),"Unexpected EOL.");
  assertFalse(transformedProps.contains("\r"),"Unexpected EOL.");
}
 

Example 51

From project c10n, under directory /tools/src/main/java/c10n/tools/bundles/.

Source file: Main.java

  29 
vote

private static void generateBundle(File parent,String fileName,Map<String,String> builder,String localeSuffix,String valuePrefix) throws IOException {
  Properties bundle=new Properties();
  for (  Entry<String,String> entry : builder.entrySet()) {
    String value=entry.getValue() == null ? "" : entry.getValue();
    if (null != valuePrefix) {
      value=valuePrefix + value;
    }
    bundle.put(entry.getKey(),value);
  }
  FileOutputStream fout=null;
  try {
    fout=new FileOutputStream(new File(parent,fileName + localeSuffix + ".properties"));
    bundle.store(new OutputStreamWriter(fout,Charset.forName("UTF-8")),"Generated C10N bundle");
  }
  finally {
    if (null != fout) {
      fout.close();
    }
  }
}
 

Example 52

From project cider, under directory /src/net/yacy/cider/util/.

Source file: FileUtils.java

  29 
vote

public static int copy(final File source,final Charset inputCharset,final Writer dest) throws IOException {
  InputStream fis=null;
  try {
    fis=new FileInputStream(source);
    return copy(fis,dest,inputCharset);
  }
  finally {
    if (fis != null)     try {
      fis.close();
    }
 catch (    final Exception e) {
    }
  }
}
 

Example 53

From project cloudhopper-commons-util, under directory /src/main/java/com/cloudhopper/commons/util/codec/.

Source file: URLCodec.java

  29 
vote

public static void encode(CharSequence in,Charset charset,Appendable out) throws IOException {
  CharBuffer cb;
  if (in instanceof CharBuffer) {
    cb=(CharBuffer)in;
  }
 else   if (in != null) {
    cb=CharBuffer.wrap(in);
  }
 else {
    return;
  }
  encode(charset.encode(cb),out);
}
 

Example 54

From project Codeable_Objects, under directory /SoftObjects/src/com/ui/.

Source file: FileImport.java

  29 
vote

public String readFile(String path) throws IOException {
  FileInputStream stream=new FileInputStream(new File(path));
  try {
    FileChannel fc=stream.getChannel();
    MappedByteBuffer bb=fc.map(FileChannel.MapMode.READ_ONLY,0,fc.size());
    try {
      System.out.println(Charset.defaultCharset().decode(bb).toString());
      this.getPoints(Charset.defaultCharset().decode(bb).toString());
    }
 catch (    Exception e) {
      e.printStackTrace();
    }
 finally {
      System.out.println("file import fail");
    }
    return Charset.defaultCharset().decode(bb).toString();
  }
  finally {
    stream.close();
  }
}
 

Example 55

From project coffeescript-netbeans, under directory /src/coffeescript/nb/.

Source file: CoffeeScriptUtils.java

  29 
vote

public static void writeJS(final String js,String name,FileObject folder,Charset encoding){
  try {
    FileObject file=folder.getFileObject(name,"js");
    if (file == null) {
      file=folder.createData(name,"js");
    }
    if (!file.asText().equals(js)) {
      OutputStream out=file.getOutputStream();
      try {
        out.write(js.getBytes(encoding));
        out.flush();
      }
  finally {
        if (out != null) {
          out.close();
        }
      }
    }
  }
 catch (  Exception e) {
    Exceptions.printStackTrace(e);
  }
}
 

Example 56

From project community-plugins, under directory /deployit-cli-plugins/mustachifier/src/test/java/ext/deployit/community/cli/mustachify/.

Source file: MustachifierTest.java

  29 
vote

private static String getContents(File dar,String entryPath,Charset charset) throws IOException {
  TFileInputStream entryBytes=new TFileInputStream(getEntry(dar,entryPath));
  try {
    return new String(toByteArray(entryBytes),charset);
  }
  finally {
    entryBytes.close();
  }
}
 

Example 57

From project CommunityCase, under directory /src/org/community/intellij/plugins/communitycase/.

Source file: ContentRevision.java

  29 
vote

public ContentRevision(@NotNull FilePath file,@NotNull VcsRevisionNumber revision,@NotNull Project project,Charset charset){
  myProject=project;
  myFile=file;
  myRevision=revision;
  myCharset=charset;
}
 

Example 58

From project components, under directory /http/src/main/java/org/switchyard/component/http/composer/.

Source file: HttpBindingData.java

  29 
vote

/** 
 * Set the HTTP body.
 * @param body the body as String
 */
public void setBody(String body){
  if (_contentType != null) {
    _body=body.getBytes(Charset.forName(_contentType.getCharset()));
  }
 else {
    _body=body.getBytes();
  }
}
 

Example 59

From project components-ness-httpserver_1, under directory /src/main/java/com/nesscomputing/httpserver/log/syslog/.

Source file: SyslogRequestLog.java

  29 
vote

@Inject public SyslogRequestLog(final SyslogRequestLogConfig requestLogConfig,final Map<String,LogField> knownFields){
  this.blackList=requestLogConfig.getBlacklist();
  final List<String> logFields=requestLogConfig.getLogFields();
  LogFields.validateLogFields(knownFields,logFields);
  this.logFields=logFields;
  this.knownFields=knownFields;
  this.ianaIdentifier=requestLogConfig.getIanaIdentifier();
  final SyslogIF syslog=Syslog.getInstance(requestLogConfig.getProtocol());
  if (syslog == null) {
    LOG.warn("No syslog instance for protocol '%s' available!",requestLogConfig.getProtocol());
  }
 else {
    final SyslogConfigIF syslogConfig=syslog.getConfig();
    syslogConfig.setUseStructuredData(true);
    syslogConfig.setTruncateMessage(true);
    syslogConfig.setMaxMessageLength(syslogConfig.getMaxMessageLength());
    syslogConfig.setCharSet(Charset.forName(requestLogConfig.getCharset()));
    syslogConfig.setFacility(requestLogConfig.getFacility());
    syslogConfig.setHost(requestLogConfig.getSyslogHost());
    syslogConfig.setPort(requestLogConfig.getSyslogPort());
    syslogConfig.setIdent("");
    syslogConfig.setLocalName(requestLogConfig.getHostname());
    final StructuredSyslogMessageProcessor messageProcessor=new StructuredSyslogMessageProcessor(requestLogConfig.getAppname());
    syslog.setStructuredMessageProcessor(messageProcessor);
  }
  this.syslog=syslog;
}
 

Example 60

From project components-ness-jdbi, under directory /src/main/java/com/nesscomputing/jdbi/template/.

Source file: TemplateGroupLoader.java

  29 
vote

public static StringTemplateGroup load(final String name,final URL resourceUrl){
  if (resourceUrl == null) {
    throw new TemplateLoaderException("Error loading StringTemplate: Resource %s does not exist!",name);
  }
  Reader reader;
  try {
    reader=new InputStreamReader(resourceUrl.openStream(),Charset.forName("UTF-8"));
  }
 catch (  IOException ex) {
    throw new TemplateLoaderException(ex,"Error loading StringTemplate: %s",name);
  }
  final AtomicBoolean error=new AtomicBoolean(false);
  final StringTemplateGroup result=new StringTemplateGroup(reader,AngleBracketTemplateLexer.class,new StringTemplateErrorListener(){
    @Override public void error(    final String msg,    final Throwable e){
      LOG.error(e,msg);
      error.set(true);
    }
    @Override public void warning(    final String msg){
      LOG.warn(msg);
    }
  }
);
  if (error.get()) {
    throw new TemplateLoaderException("Error loading StringTemplate: %s",name);
  }
  return result;
}
 

Example 61

From project core_7, under directory /src/main/java/io/s4/client/.

Source file: GenericJsonClientStub.java

  29 
vote

@Override public EventWrapper eventWrapperFromBytes(byte[] v){
  try {
    String s=new String(v,Charset.forName("UTF8"));
    JSONObject json=new JSONObject(s);
    String streamName=json.getString("stream");
    String className=json.getString("class");
    Class<?> clazz;
    try {
      clazz=Class.forName(className);
    }
 catch (    ClassNotFoundException e) {
      throw new ObjectBuilder.Exception("bad class name for json-encoded object: " + className,e);
    }
    String[] keyNames=null;
    JSONArray keyArray=json.optJSONArray("keys");
    if (keyArray != null) {
      keyNames=new String[keyArray.length()];
      for (int i=0; i < keyNames.length; ++i) {
        keyNames[i]=keyArray.optString(i);
      }
    }
    String jevent=json.getString("object");
    Object obj=GsonUtil.get().fromJson(jevent,clazz);
    return new EventWrapper(streamName,keyNames,obj);
  }
 catch (  JSONException e) {
    logger.error("problem with event JSON",e);
  }
catch (  ObjectBuilder.Exception e) {
    logger.error("failed to build object from JSON",e);
  }
  return null;
}
 

Example 62

From project Countandra, under directory /src/org/countandra/unittests/.

Source file: CountandraTestUtils.java

  29 
vote

public static void insertData(String content){
  Channel channel=null;
  HttpRequest request;
  ChannelBuffer buffer;
  try {
    channel=client.connect(new InetSocketAddress("localhost",httpPort)).awaitUninterruptibly().getChannel();
    request=new DefaultHttpRequest(HttpVersion.HTTP_1_1,HttpMethod.POST,"insert");
    buffer=ChannelBuffers.copiedBuffer(content,Charset.defaultCharset());
    request.addHeader(org.jboss.netty.handler.codec.http.HttpHeaders.Names.CONTENT_LENGTH,buffer.readableBytes());
    request.setContent(buffer);
    channel.write(request).awaitUninterruptibly().getChannel().getCloseFuture().awaitUninterruptibly();
  }
 catch (  Exception e) {
    System.err.println("Error: " + e.getMessage());
  }
  channel.getCloseFuture().awaitUninterruptibly();
}
 

Example 63

From project cp-common-utils, under directory /src/com/clarkparsia/common/base/.

Source file: Strings2.java

  29 
vote

/** 
 * URL encode the given string using the given Charset
 * @param theString the string to encode
 * @param theCharset the charset to encode the string using
 * @return the string encoded with the given charset
 */
public static String urlEncode(String theString,Charset theCharset){
  try {
    return URLEncoder.encode(theString,theCharset.displayName());
  }
 catch (  UnsupportedEncodingException e) {
    return null;
  }
}