Java Code Examples for com.google.common.base.Charsets#UTF_8

The following examples show how to use com.google.common.base.Charsets#UTF_8 . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Main.java    From p2p with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException, InterruptedException {
    if (System.getProperty(PEER_NAME_SYSTEM_PROPERTY) == null) {
        LOGGER.error("System property peerName should be provided!");
        System.exit(-1);
    }

    final OptionSet options = parseArguments(args);
    final PeerRunner peerRunner = createPeerRunner(options);
    peerRunner.start();

    String line;
    final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, Charsets.UTF_8));
    while ((line = reader.readLine()) != null) {
        final PeerRunner.CommandResult result = peerRunner.handleCommand(line);
        if (result == PeerRunner.CommandResult.SHUT_DOWN) {
            break;
        } else if (result == PeerRunner.CommandResult.INVALID_COMMAND && line.length() > 0) {
            printHelp(line);
        }
    }
}
 
Example 2
Source File: InputStreamFTGSIterator.java    From imhotep with Apache License 2.0 6 votes vote down vote up
private void internalNextField() throws IOException {
    final int fieldType = readByte() & 0xFF;
    if (fieldType == 0) {
        iteratorStatus = 0;
        return; // normal end of stream condition
    }

    fieldIsIntType = fieldType == 1;
    final int fieldNameLength = readVInt();
    final byte[] fieldNameBytes = new byte[fieldNameLength];
    readBytes(fieldNameBytes, 0, fieldNameBytes.length);
    fieldName = new String(fieldNameBytes, Charsets.UTF_8);
    intTermVal = -1;
    stringTermVal = null;
    currentTermLength = 0;
    groupId = -1;
    iteratorStatus = 2;
}
 
Example 3
Source File: LogConfig.java    From buck with Apache License 2.0 6 votes vote down vote up
private static boolean addInputStreamForTemplate(
    Path path,
    LogConfigSetup logConfigSetup,
    ImmutableList.Builder<InputStream> inputStreamsBuilder)
    throws IOException {
  try {
    String template = new String(Files.readAllBytes(path), Charsets.UTF_8);
    ST st = new ST(template);
    st.add(
        "default_file_pattern",
        PathFormatter.pathWithUnixSeparators(logConfigSetup.getLogFilePath()));
    st.add("default_count", logConfigSetup.getCount());
    st.add("default_max_size_bytes", logConfigSetup.getMaxLogSizeBytes());
    String result = st.render();
    inputStreamsBuilder.add(new ByteArrayInputStream(result.getBytes(Charsets.UTF_8)));
    inputStreamsBuilder.add(new ByteArrayInputStream(NEWLINE));
    return true;
  } catch (FileNotFoundException e) {
    return false;
  }
}
 
Example 4
Source File: ResourcesXmlTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testAaptDumpReversedXmlTree() throws Exception {
  try (ZipFile apkZip = new ZipFile(apkPath.toFile())) {
    ResourceTable resourceTable =
        ResourceTable.get(
            ResChunk.wrap(
                ByteStreams.toByteArray(
                    apkZip.getInputStream(apkZip.getEntry("resources.arsc")))));
    ReferenceMapper reversingMapper = ReversingMapper.construct(resourceTable);

    ByteBuffer buf =
        ResChunk.wrap(
            ByteStreams.toByteArray(
                apkZip.getInputStream(apkZip.getEntry("AndroidManifest.xml"))));

    ResourcesXml xml = ResourcesXml.get(buf);
    xml.transformReferences(reversingMapper::map);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    xml.dump(new PrintStream(baos));
    String content = new String(baos.toByteArray(), Charsets.UTF_8);

    Path xmltreeOutput = filesystem.resolve(filesystem.getPath(APK_NAME + ".manifest.reversed"));
    String expected = new String(Files.readAllBytes(xmltreeOutput));
    MoreAsserts.assertLargeStringsEqual(expected, content);
  }
}
 
Example 5
Source File: ImageWriterTest.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
@Test
public void smoke() throws IOException {
  ByteArrayOutputStream os = new ByteArrayOutputStream();
  writer.writeTo(graph, Graph.class, null, null, CustomMediaTypes.IMAGE_PNG_TYPE, null, os);
  String image = new String(os.toByteArray(), Charsets.UTF_8);
  assertThat(image, is(notNullValue()));
}
 
Example 6
Source File: VariableMapTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void cycleTest(ImmutableMap<String, String> map)
    throws ParseException {
  VariableMap in = new VariableMap(map);
  String serialized = new String(in.toBytes(), Charsets.UTF_8);
  VariableMap out = VariableMap.fromBytes(serialized.getBytes());
  assertMapsEquals(in.toMap(), out.toMap());
}
 
Example 7
Source File: DecrypterImpl.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
@Override
public String decrypt(String encrypted) {
  try {
    byte[] decoded = BaseEncoding.base64Url().decode(encrypted);
    Cipher cipher;
    switch (transformation) {
      case AES_CBC_NOPADDING:
        cipher = Cipher.getInstance("AES/CBC/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, key, generateIv(cipher));
        break;
      case RSA_ECB_PKCS1:
        cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, key);        
        break;
      default:
        throw new AssertionError("How could this happen...");
    }
    byte[] decrypted = cipher.doFinal(decoded);
    if (decrypted == null || decrypted.length <= cipher.getBlockSize()) {
      throw new RuntimeException("incorrect decrypted text.");
    }
    byte[] data = new byte[decrypted.length - cipher.getBlockSize()];
    System.arraycopy(decrypted, cipher.getBlockSize(), data, 0, data.length);
    return new String(data, Charsets.UTF_8);
  } catch (BadPaddingException
      | IllegalBlockSizeException
      | InvalidAlgorithmParameterException
      | InvalidKeyException
      | NoSuchAlgorithmException
      | NoSuchPaddingException e) {
    monitor.severe(() -> format("Error decrypting data, length: %s", encrypted.length()), e);
    throw new RuntimeException(e);
  }
}
 
Example 8
Source File: EventStreamReadingAT.java    From nakadi with MIT License 5 votes vote down vote up
@Test(timeout = 10000)
public void whenMemoryOverflowEventsDumped() throws IOException {
    // Create event type
    final EventType loadEt = EventTypeTestBuilder.builder()
            .defaultStatistic(new EventTypeStatistics(PARTITIONS_NUM, PARTITIONS_NUM))
            .build();
    NakadiTestUtils.createEventTypeInNakadi(loadEt);

    // Publish events to event type, that are not fitting memory
    final String evt = "{\"foo\":\"barbarbar\"}";
    final int eventCount = 2 * (10000 / evt.length());
    NakadiTestUtils.publishEvents(loadEt.getName(), eventCount, i -> evt);

    // Configure streaming so it will:(more than 10s and batch_limit
    // - definitely wait for more than test timeout (10s)
    // - collect batch, which size is greater than events published to this event type
    final String url = RestAssured.baseURI + ":" + RestAssured.port + createStreamEndpointUrl(loadEt.getName())
            + "?batch_limit=" + (10 * eventCount)
            + "&stream_limit=" + (10 * eventCount)
            + "&batch_flush_timeout=11"
            + "&stream_timeout=11";
    final HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    // Start from the begin.
    connection.setRequestProperty("X-Nakadi-Cursors",
            "[" + IntStream.range(0, PARTITIONS_NUM)
                    .mapToObj(i -> "{\"partition\": \"" + i + "\",\"offset\":\"begin\"}")
                    .collect(Collectors.joining(",")) + "]");
    Assert.assertEquals(HttpServletResponse.SC_OK, connection.getResponseCode());

    try (InputStream inputStream = connection.getInputStream()){
        final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, Charsets.UTF_8));

        final String line = reader.readLine();
        // If we read at least one line, than it means, that we were able to read data before test timeout reached.
        Assert.assertNotNull(line);
    }
}
 
Example 9
Source File: CheckpointManager.java    From qmq with Apache License 2.0 5 votes vote down vote up
@Override
public IndexCheckpoint fromBytes(byte[] data) {
    try {
        final String checkpoint = new String(data, Charsets.UTF_8);
        int pos = checkpoint.indexOf(SLASH);
        long msgOffset = Long.parseLong(checkpoint.substring(0, pos));
        long indexOffset = Long.parseLong(checkpoint.substring(pos + 1));
        return new IndexCheckpoint(msgOffset, indexOffset);
    } catch (NumberFormatException e) {
        throw new RuntimeException(e);
    }
}
 
Example 10
Source File: Util.java    From big-c with Apache License 2.0 5 votes vote down vote up
/** Create a writer of a local file. */
public static PrintWriter createWriter(File dir, String prefix) throws IOException {
  checkDirectory(dir);
  
  SimpleDateFormat dateFormat = new SimpleDateFormat("-yyyyMMdd-HHmmssSSS");
  for(;;) {
    final File f = new File(dir,
        prefix + dateFormat.format(new Date(System.currentTimeMillis())) + ".txt");
    if (!f.exists())
      return new PrintWriter(new OutputStreamWriter(new FileOutputStream(f), Charsets.UTF_8));

    try {Thread.sleep(10);} catch (InterruptedException e) {}
  }
}
 
Example 11
Source File: TemplateProcessor.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Reads and returns the content of a text file embedded in the jar file.
 * @param templateStream the stream to read the template file from
 * @return null if the file could not be read
 * @throws java.io.IOException
 */
private String readEmbeddedTextFile(InputStream templateStream) throws IOException {
    InputStreamReader reader = new InputStreamReader(templateStream, Charsets.UTF_8);

    try {
        return CharStreams.toString(reader);
    } finally {
        reader.close();
    }
}
 
Example 12
Source File: Http2ClientStreamTransportState.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
/**
 * Inspect the raw metadata and figure out what charset is being used.
 */
private static Charset extractCharset(Metadata headers) {
  String contentType = headers.get(GrpcUtil.CONTENT_TYPE_KEY);
  if (contentType != null) {
    String[] split = contentType.split("charset=", 2);
    try {
      return Charset.forName(split[split.length - 1].trim());
    } catch (Exception t) {
      // Ignore and assume UTF-8
    }
  }
  return Charsets.UTF_8;
}
 
Example 13
Source File: CuratorZookeeperCenterRepository.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Override
public String get(final String key) {
    TreeCache cache = findTreeCache(key);
    if (null == cache) {
        return getDirectly(key);
    }
    ChildData resultInCache = cache.getCurrentData(key);
    if (null != resultInCache) {
        return null == resultInCache.getData() ? null : new String(resultInCache.getData(), Charsets.UTF_8);
    }
    return getDirectly(key);
}
 
Example 14
Source File: FieldCardinalityBase.java    From datawave with Apache License 2.0 4 votes vote down vote up
public void setColumnVisibility(ColumnVisibility columnVisibility) {
    String cvString = (columnVisibility == null) ? null : new String(columnVisibility.getExpression(), Charsets.UTF_8);
    setColumnVisibility(cvString);
}
 
Example 15
Source File: InputStreamToString.java    From levelup-java-examples with Apache License 2.0 4 votes vote down vote up
@Test
public void convert_inputstream_to_string_java () throws IOException {
	
	File file = new File(fileLocation);

	InputStream inputStream = new FileInputStream(file);
	
       StringBuilder fileContent = new StringBuilder();

	BufferedReader bufferedReader = new BufferedReader(
			new InputStreamReader(inputStream, Charsets.UTF_8));
	
       String line = bufferedReader.readLine();
       while (line != null){
       	fileContent.append(line);
           line = bufferedReader.readLine();
       }
       
       bufferedReader.close();
       
	assertEquals("Inputstream to string", fileContent.toString());
}
 
Example 16
Source File: NamenodeWebHdfsMethods.java    From big-c with Apache License 2.0 4 votes vote down vote up
private static StreamingOutput getListingStream(final NamenodeProtocols np, 
    final String p) throws IOException {
  // allows exceptions like FNF or ACE to prevent http response of 200 for
  // a failure since we can't (currently) return error responses in the
  // middle of a streaming operation
  final DirectoryListing firstDirList = getDirectoryListing(np, p,
      HdfsFileStatus.EMPTY_NAME);

  // must save ugi because the streaming object will be executed outside
  // the remote user's ugi
  final UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
  return new StreamingOutput() {
    @Override
    public void write(final OutputStream outstream) throws IOException {
      final PrintWriter out = new PrintWriter(new OutputStreamWriter(
          outstream, Charsets.UTF_8));
      out.println("{\"" + FileStatus.class.getSimpleName() + "es\":{\""
          + FileStatus.class.getSimpleName() + "\":[");

      try {
        // restore remote user's ugi
        ugi.doAs(new PrivilegedExceptionAction<Void>() {
          @Override
          public Void run() throws IOException {
            long n = 0;
            for (DirectoryListing dirList = firstDirList; ;
                 dirList = getDirectoryListing(np, p, dirList.getLastName())
            ) {
              // send each segment of the directory listing
              for (HdfsFileStatus s : dirList.getPartialListing()) {
                if (n++ > 0) {
                  out.println(',');
                }
                out.print(JsonUtil.toJsonString(s, false));
              }
              // stop if last segment
              if (!dirList.hasMore()) {
                break;
              }
            }
            return null;
          }
        });
      } catch (InterruptedException e) {
        throw new IOException(e);
      }
      
      out.println();
      out.println("]}}");
      out.flush();
    }
  };
}
 
Example 17
Source File: ExoResourcesRewriterTest.java    From buck with Apache License 2.0 4 votes vote down vote up
@Test
public void testRewriteResources() throws IOException {
  Path primaryOutput = tmpFolder.getRoot().resolve("primary.apk");
  Path exoOutput = tmpFolder.getRoot().resolve("exo.apk");
  ExoResourcesRewriter.rewriteResources(apkPath, primaryOutput, exoOutput);

  ZipInspector primaryApkInspector = new ZipInspector(primaryOutput);
  assertEquals(
      ImmutableList.of(
          "resources.arsc",
          "AndroidManifest.xml",
          "res/drawable-nodpi-v4/exo_icon.png",
          "res/xml/meta_xml.xml"),
      primaryApkInspector.getZipFileEntries());
  ZipInspector baseApkInspector = new ZipInspector(apkPath);
  ZipInspector exoApkInspector = new ZipInspector(exoOutput);
  assertEquals(baseApkInspector.getZipFileEntries(), exoApkInspector.getZipFileEntries());

  assertArrayEquals(
      primaryApkInspector.getFileContents("AndroidManifest.xml"),
      exoApkInspector.getFileContents("AndroidManifest.xml"));

  assertArrayEquals(
      primaryApkInspector.getFileContents("res/xml/meta_xml.xml"),
      exoApkInspector.getFileContents("res/xml/meta_xml.xml"));

  ResourceTable primaryResourceTable =
      ResourceTable.get(ResChunk.wrap(primaryApkInspector.getFileContents("resources.arsc")));

  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  primaryResourceTable.dump(new PrintStream(baos));
  String content = new String(baos.toByteArray(), Charsets.UTF_8);
  Path expectedPath = filesystem.resolve(filesystem.getPath(APK_NAME + ".resources.primary"));
  String expected = filesystem.readFileIfItExists(expectedPath).get();

  assertEquals(expected, content);

  ResourceTable exoResourceTable =
      ResourceTable.get(ResChunk.wrap(exoApkInspector.getFileContents("resources.arsc")));

  baos = new ByteArrayOutputStream();
  exoResourceTable.dump(new PrintStream(baos));
  content = new String(baos.toByteArray(), Charsets.UTF_8);
  expectedPath = filesystem.resolve(filesystem.getPath(APK_NAME + ".resources.exo"));
  expected = filesystem.readFileIfItExists(expectedPath).get();

  assertEquals(expected, content);
}
 
Example 18
Source File: CredentialEncrypter.java    From emodb with Apache License 2.0 4 votes vote down vote up
/**
 * Recovers the plaintext String from the encrypted value returned by {@link #encryptString(String)}.
 */
public String decryptString(String encryptedCredentials) {
    byte[] plaintextBytes = decryptBytes(encryptedCredentials);
    return new String(plaintextBytes, Charsets.UTF_8);
}
 
Example 19
Source File: Result.java    From wisdom with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the content type of this result to {@link MimeTypes#HTML}.
 *
 * @return the same result where you executed this method on. But the content type is now {@link MimeTypes#HTML}.
 */
public Result html() {
    setContentType(MimeTypes.HTML);
    charset = Charsets.UTF_8;
    return this;
}
 
Example 20
Source File: FileLineIterator.java    From elasticsearch-taste with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a {@link FileLineIterator} over a given file, assuming a UTF-8 encoding.
 *
 * @throws java.io.FileNotFoundException if the file does not exist
 * @throws IOException
 *           if the file cannot be read
 */
public FileLineIterator(File file) throws IOException {
    this(file, Charsets.UTF_8, false);
}