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

The following are top voted examples for showing how to use com.google.common.base.Charsets. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: flume-release-1.7.0   File: EncryptionTestUtils.java   View source code 6 votes vote down vote up
public static void createKeyStore(File keyStoreFile, File keyStorePasswordFile,
                                  Map<String, File> keyAliasPassword) throws Exception {
  KeyStore ks = KeyStore.getInstance("jceks");
  ks.load(null);
  List<String> keysWithSeperatePasswords = Lists.newArrayList();
  for (String alias : keyAliasPassword.keySet()) {
    Key key = newKey();
    char[] password = null;
    File passwordFile = keyAliasPassword.get(alias);
    if (passwordFile == null) {
      password = Files.toString(keyStorePasswordFile, Charsets.UTF_8).toCharArray();
    } else {
      keysWithSeperatePasswords.add(alias);
      password = Files.toString(passwordFile, Charsets.UTF_8).toCharArray();
    }
    ks.setKeyEntry(alias, key, password, null);
  }
  char[] keyStorePassword = Files.toString(keyStorePasswordFile, Charsets.UTF_8).toCharArray();
  FileOutputStream outputStream = new FileOutputStream(keyStoreFile);
  ks.store(outputStream, keyStorePassword);
  outputStream.close();
}
 
Example 2
Project: dotwebstack-framework   File: FileConfigurationBackendTest.java   View source code 6 votes vote down vote up
@Test
public void loadPrefixes_ThrowConfigurationException_FoundUnknownPrefix() throws Exception {
  // Arrange
  Resource resource = mock(Resource.class);
  when(resource.getInputStream()).thenReturn(
      new ByteArrayInputStream(new String("@prefix dbeerpedia: <http://dbeerpedia.org#> .\n"
          + "@prefix elmo: <http://dotwebstack.org/def/elmo#> .\n"
          + "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n"
          + "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n"
          + "this is not a valid prefix").getBytes(Charsets.UTF_8)));
  when(resource.getFilename()).thenReturn("_prefixes.trig");
  when(((ResourcePatternResolver) resourceLoader).getResources(any())).thenReturn(
      new Resource[] {resource});

  // Assert
  thrown.expect(ConfigurationException.class);
  thrown.expectMessage("Found unknown prefix format <this is not a valid prefix> at line <5>");

  // Act
  backend.loadResources();
}
 
Example 3
Project: flume-release-1.7.0   File: TestTimestampInterceptor.java   View source code 6 votes vote down vote up
/**
 * Ensure timestamp is NOT overwritten when preserveExistingTimestamp == true
 */
@Test
public void testPreserve() throws ClassNotFoundException,
    InstantiationException, IllegalAccessException {

  Context ctx = new Context();
  ctx.put("preserveExisting", "true");

  InterceptorBuilderFactory factory = new InterceptorBuilderFactory();
  Interceptor.Builder builder = InterceptorBuilderFactory.newInstance(
      InterceptorType.TIMESTAMP.toString());
  builder.configure(ctx);
  Interceptor interceptor = builder.build();

  long originalTs = 1L;
  Event event = EventBuilder.withBody("test event", Charsets.UTF_8);
  event.getHeaders().put(Constants.TIMESTAMP, Long.toString(originalTs));
  Assert.assertEquals(Long.toString(originalTs),
      event.getHeaders().get(Constants.TIMESTAMP));

  Long now = System.currentTimeMillis();
  event = interceptor.intercept(event);
  String timestampStr = event.getHeaders().get(Constants.TIMESTAMP);
  Assert.assertNotNull(timestampStr);
  Assert.assertTrue(Long.parseLong(timestampStr) == originalTs);
}
 
Example 4
Project: hadoop   File: TestTableMapping.java   View source code 6 votes vote down vote up
@Test(timeout=60000)
public void testBadFile() throws IOException {
  File mapFile = File.createTempFile(getClass().getSimpleName() +
      ".testBadFile", ".txt");
  Files.write("bad contents", mapFile, Charsets.UTF_8);
  mapFile.deleteOnExit();
  TableMapping mapping = new TableMapping();

  Configuration conf = new Configuration();
  conf.set(NET_TOPOLOGY_TABLE_MAPPING_FILE_KEY, mapFile.getCanonicalPath());
  mapping.setConf(conf);

  List<String> names = new ArrayList<String>();
  names.add(hostName1);
  names.add(hostName2);

  List<String> result = mapping.resolve(names);
  assertEquals(names.size(), result.size());
  assertEquals(result.get(0), NetworkTopology.DEFAULT_RACK);
  assertEquals(result.get(1), NetworkTopology.DEFAULT_RACK);
}
 
Example 5
Project: flume-release-1.7.0   File: TestTaildirEventReader.java   View source code 6 votes vote down vote up
@Test
public void testNewLineBoundaries() throws IOException {
  File f1 = new File(tmpDir, "file1");
  Files.write("file1line1\nfile1line2\rfile1line2\nfile1line3\r\nfile1line4\n",
              f1, Charsets.UTF_8);

  ReliableTaildirEventReader reader = getReader();
  List<String> out = Lists.newArrayList();
  for (TailFile tf : reader.getTailFiles().values()) {
    out.addAll(bodiesAsStrings(reader.readEvents(tf, 5)));
    reader.commit();
  }
  assertEquals(4, out.size());
  //Should treat \n as line boundary
  assertTrue(out.contains("file1line1"));
  //Should not treat \r as line boundary
  assertTrue(out.contains("file1line2\rfile1line2"));
  //Should treat \r\n as line boundary
  assertTrue(out.contains("file1line3"));
  assertTrue(out.contains("file1line4"));
}
 
Example 6
Project: kafka-junit   File: KafkaTestUtils.java   View source code 6 votes vote down vote up
/**
 * Produce randomly generated records into the defined kafka namespace.
 *
 * @param numberOfRecords how many records to produce
 * @param topicName the namespace name to produce into.
 * @param partitionId the partition to produce into.
 * @return List of ProducedKafkaRecords.
 */
public List<ProducedKafkaRecord<byte[], byte[]>> produceRecords(
    final int numberOfRecords,
    final String topicName,
    final int partitionId
) {
    Map<byte[], byte[]> keysAndValues = new HashMap<>();

    // Generate random & unique data
    for (int x = 0; x < numberOfRecords; x++) {
        // Construct key and value
        long timeStamp = Clock.systemUTC().millis();
        String key = "key" + timeStamp;
        String value = "value" + timeStamp;

        // Add to map
        keysAndValues.put(key.getBytes(Charsets.UTF_8), value.getBytes(Charsets.UTF_8));
    }

    return produceRecords(keysAndValues, topicName, partitionId);
}
 
Example 7
Project: n4js   File: EclipseBasedProjectModelSetup.java   View source code 6 votes vote down vote up
private void createManifest(String projectName, String string) throws CoreException, UnsupportedEncodingException {
	IProject project = workspace.getProject(projectName);
	IFile manifestFile = project.getFile(IN4JSProject.N4MF_MANIFEST);
	@SuppressWarnings("resource")
	StringInputStream content = new StringInputStream(string, Charsets.UTF_8.name());
	manifestFile.create(content, false, null);
	manifestFile.setCharset(Charsets.UTF_8.name(), null);

	IFolder src = project.getFolder("src");
	src.create(false, true, null);
	IFolder sub = src.getFolder("sub");
	sub.create(false, true, null);
	IFolder leaf = sub.getFolder("leaf");
	leaf.create(false, true, null);
	src.getFile("A.js").create(new ByteArrayInputStream(new byte[0]), false, null);
	src.getFile("B.js").create(new ByteArrayInputStream(new byte[0]), false, null);
	sub.getFile("B.js").create(new ByteArrayInputStream(new byte[0]), false, null);
	sub.getFile("C.js").create(new ByteArrayInputStream(new byte[0]), false, null);
	leaf.getFile("D.js").create(new ByteArrayInputStream(new byte[0]), false, null);
}
 
Example 8
Project: CustomWorldGen   File: ModelBakery.java   View source code 6 votes vote down vote up
private ModelBlockDefinition loadModelBlockDefinition(ResourceLocation location, IResource resource)
{
    InputStream inputstream = null;
    ModelBlockDefinition lvt_4_1_;

    try
    {
        inputstream = resource.getInputStream();
        lvt_4_1_ = ModelBlockDefinition.parseFromReader(new InputStreamReader(inputstream, Charsets.UTF_8));
    }
    catch (Exception exception)
    {
        throw new RuntimeException("Encountered an exception when loading model definition of \'" + location + "\' from: \'" + resource.getResourceLocation() + "\' in resourcepack: \'" + resource.getResourcePackName() + "\'", exception);
    }
    finally
    {
        IOUtils.closeQuietly(inputstream);
    }

    return lvt_4_1_;
}
 
Example 9
Project: tac-kbp-eal   File: PerDocResultWriter.java   View source code 6 votes vote down vote up
static void writeArgPerDoc(final List<EALScorer2015Style.ArgResult> perDocResults,
    final File outFile)
    throws IOException {
  Files.asCharSink(outFile, Charsets.UTF_8).write(
      String.format("%40s\t%10s\n", "Document", "Arg") +
          Joiner.on("\n").join(
              FluentIterable.from(perDocResults)
                  .transform(new Function<EALScorer2015Style.ArgResult, String>() {
                    @Override
                    public String apply(final EALScorer2015Style.ArgResult input) {
                      return String.format("%40s\t%10.2f",
                          input.docID(),
                          100.0 * input.scaledArgumentScore());
                    }
                  })));
}
 
Example 10
Project: cmakeify   File: TestCmakeify.java   View source code 6 votes vote down vote up
@Test
public void testSQLite() throws IOException {
  File yaml = new File("test-files/testScript/cmakeify.yml");
  yaml.getParentFile().mkdirs();
  Files.write("targets: [android]\n" +
          "buildTarget: sqlite\n" +
          "android:\n" +
          "  ndk:\n" +
          "    runtimes: [c++, gnustl, stlport]\n" +
          "    platforms: [12, 21]\n" +
          "example: |\n" +
          "   #include <sqlite3.h>\n" +
          "   void test() {\n" +
          "     sqlite3_initialize();\n" +
          "   }",
      yaml, StandardCharsets.UTF_8);
  main("-wf", yaml.getParent(),
      "--host", "Linux",
      "--group-id", "my-group-id",
      "--artifact-id", "my-artifact-id",
      "--target-version", "my-target-version");
  File scriptFile = new File(".cmakeify/build.sh");
  String script = Joiner.on("\n").join(Files.readLines(scriptFile, Charsets.UTF_8));
  assertThat(script).contains("cmake-3.7.2-Linux-x86_64.tar.gz");
}
 
Example 11
Project: vrap   File: BaseUriReplacer.java   View source code 6 votes vote down vote up
public StringWriter preprocess(Context ctx, final Path filePath, final Api api) throws IOException {
    final Integer port = ctx.getServerConfig().getPort();
    final StringWriter stringWriter = new StringWriter();
    final String baseUri = api.baseUri().value();
    final List<SecurityScheme> oauthSchemes = api.securitySchemes().stream().filter(securityScheme -> securityScheme.type().equals("OAuth 2.0")).collect(Collectors.toList());
    String content = new String(Files.readAllBytes(filePath), Charsets.UTF_8);

    ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
    final JsonNode file = mapper.readValue(filePath.toFile(), JsonNode.class);
    if (file.has("baseUri")) {
        content = content.replaceAll(baseUri, "http://localhost:" + port.toString() + "/api");
    }

    if (!oauthSchemes.isEmpty()) {
        for (SecurityScheme scheme : oauthSchemes) {
            content = content.replaceAll(scheme.settings().accessTokenUri().value(), "http://localhost:" + port.toString() + "/auth/" + scheme.name());
        }
    }

    return stringWriter.append(content);
}
 
Example 12
Project: QDrill   File: TestDistributedFragmentRun.java   View source code 6 votes vote down vote up
@Test
public void oneBitOneExchangeTwoEntryRunLogical() throws Exception{
    RemoteServiceSet serviceSet = RemoteServiceSet.getLocalServiceSet();

    try(Drillbit bit1 = new Drillbit(CONFIG, serviceSet); DrillClient client = new DrillClient(CONFIG, serviceSet.getCoordinator());){
        bit1.run();
        client.connect();
        List<QueryDataBatch> results = client.runQuery(QueryType.LOGICAL, Files.toString(FileUtils.getResourceAsFile("/scan_screen_logical.json"), Charsets.UTF_8));
        int count = 0;
        for(QueryDataBatch b : results){
            count += b.getHeader().getRowCount();
            b.release();
        }
        assertEquals(100, count);
    }


}
 
Example 13
Project: BUbiNG   File: VisitStateSetTest.java   View source code 6 votes vote down vote up
@Test
public void testResize() {
	final VisitState[] visitState = new VisitState[2000];
	for(int i = visitState.length; i-- != 0;) visitState[i] = new VisitState(null, Integer.toString(i).getBytes(Charsets.ISO_8859_1));

	VisitStateSet s = new VisitStateSet();
	for(int i = 2000; i-- != 0;) assertTrue(s.add(visitState[i]));
	assertEquals(2000, s.size());
	for(int i = 2000; i-- != 0;) assertFalse(s.add(new VisitState(null, Integer.toString(i).getBytes(Charsets.ISO_8859_1))));
	for(int i = 1000; i-- != 0;) assertTrue(s.remove(visitState[i]));

	for(int i = 1000; i-- != 0;) assertFalse(s.remove(visitState[i]));
	for(int i = 1000; i-- != 0;) assertNull(s.get(Integer.toString(i).getBytes(Charsets.ISO_8859_1)));
	for(int i = 2000; i-- != 1000;) assertSame(visitState[i], s.get(Integer.toString(i).getBytes(Charsets.ISO_8859_1)));
	assertEquals(1000, s.size());
	assertFalse(s.isEmpty());
	s.clear();
	assertEquals(0, s.size());
	assertTrue(s.isEmpty());
	s.clear();
	assertEquals(0, s.size());
	assertTrue(s.isEmpty());
}
 
Example 14
Project: uavstack   File: TailFile.java   View source code 6 votes vote down vote up
private String readLine() throws IOException {

        ByteArrayDataOutput out = ByteStreams.newDataOutput(300);
        int i = 0;
        int c;
        while ((c = raf.read()) != -1) {
            i++;
            out.write((byte) c);
            if (c == LINE_SEP.charAt(0)) {
                break;
            }
        }
        if (i == 0) {
            return null;
        }
        return new String(out.toByteArray(), Charsets.UTF_8);
    }
 
Example 15
Project: tools   File: FileUtil.java   View source code 6 votes vote down vote up
static String readLocalAuth(String filePath) {
    final Path path = Paths.get(filePath);
    try {
        if (!Files.exists(path)) {
            LOGGER.info("authFile not found, creating it");
            final UUID uuid = UUID.randomUUID();
            final Path temp = path.getParent().resolve(path.getFileName().toString() + ".temp");
            createFileWithPermissions(temp);
            final String written = uuid.toString();
            Files.write(temp, written.getBytes(Charsets.UTF_8));
            Files.move(temp, path, StandardCopyOption.ATOMIC_MOVE);
            return written;
        } else {
            final List<String> lines = Files.readAllLines(path);
            Preconditions.checkState(lines.size() == 1);
            final String ret = lines.get(0);
            final UUID ignored = UUID.fromString(ret);//check if this throws an exception, that might mean that
            return ret;
        }
    } catch (IOException e) {
        throw new RuntimeException("unable to read password file at " + filePath, e);
    }
}
 
Example 16
Project: hadoop-oss   File: ZKUtil.java   View source code 6 votes vote down vote up
/**
 * Parse a comma-separated list of authentication mechanisms. Each
 * such mechanism should be of the form 'scheme:auth' -- the same
 * syntax used for the 'addAuth' command in the ZK CLI.
 * 
 * @param authString the comma-separated auth mechanisms
 * @return a list of parsed authentications
 * @throws {@link BadAuthFormatException} if the auth format is invalid
 */
public static List<ZKAuthInfo> parseAuth(String authString) throws
    BadAuthFormatException{
  List<ZKAuthInfo> ret = Lists.newArrayList();
  if (authString == null) {
    return ret;
  }
  
  List<String> authComps = Lists.newArrayList(
      Splitter.on(',').omitEmptyStrings().trimResults()
      .split(authString));
  
  for (String comp : authComps) {
    String parts[] = comp.split(":", 2);
    if (parts.length != 2) {
      throw new BadAuthFormatException(
          "Auth '" + comp + "' not of expected form scheme:auth");
    }
    ret.add(new ZKAuthInfo(parts[0],
        parts[1].getBytes(Charsets.UTF_8)));
  }
  return ret;
}
 
Example 17
Project: ditb   File: MemoryBoundedLogMessageBuffer.java   View source code 6 votes vote down vote up
/**
 * Dump the contents of the buffer to the given stream.
 */
public synchronized void dumpTo(PrintWriter out) {
  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

  for (LogMessage msg : messages) {
    out.write(df.format(new Date(msg.timestamp)));
    out.write(" ");
    out.println(new String(msg.message, Charsets.UTF_8));
  }
}
 
Example 18
Project: javaide   File: AvdManager.java   View source code 6 votes vote down vote up
/**
 * Writes a .ini file from a set of properties, using UTF-8 encoding.
 * The keys are sorted.
 * The file should be read back later by {@link #parseIniFile(IAbstractFile, ILogger)}.
 *
 * @param iniFile The file to generate.
 * @param values The properties to place in the ini file.
 * @param addEncoding When true, add a property {@link #AVD_INI_ENCODING} indicating the
 *                    encoding used to write the file.
 * @throws IOException if {@link FileWriter} fails to open, write or close the file.
 */
private static void writeIniFile(File iniFile, Map<String, String> values, boolean addEncoding)
        throws IOException {

    Charset charset = Charsets.ISO_8859_1;
    OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(iniFile), charset);

    if (addEncoding) {
        // Write down the charset used in case we want to use it later.
        writer.write(String.format("%1$s=%2$s\n", AVD_INI_ENCODING, charset.name()));
    }

    ArrayList<String> keys = new ArrayList<String>(values.keySet());
    Collections.sort(keys);

    for (String key : keys) {
        String value = values.get(key);
        writer.write(String.format("%1$s=%2$s\n", key, value));
    }
    writer.close();
}
 
Example 19
Project: sonar-css-plugin   File: LessMetricsVisitorTest.java   View source code 6 votes vote down vote up
private void setUp(String fileName) {
  File moduleBaseDir = new File("src/test/resources/metrics/");
  context = SensorContextTester.create(moduleBaseDir);

  DefaultInputFile inputFile = new DefaultInputFile("moduleKey", fileName)
    .setModuleBaseDir(moduleBaseDir.toPath())
    .setLanguage("less")
    .setType(InputFile.Type.MAIN);

  context.fileSystem().add(inputFile);
  context.fileSystem().setEncoding(Charsets.UTF_8);

  LessMetricsVisitor metricsVisitor = new LessMetricsVisitor(context, mock(NoSonarFilter.class));

  TreeVisitorContext treeVisitorContext = mock(TreeVisitorContext.class);
  when(treeVisitorContext.getFile()).thenReturn(inputFile.file());
  when(treeVisitorContext.getTopTree()).thenReturn(LessParser.createParser(Charsets.UTF_8).parse(inputFile.file()));

  metricsVisitor.scanTree(treeVisitorContext);
}
 
Example 20
Project: flume-release-1.7.0   File: TestResettableFileInputStream.java   View source code 6 votes vote down vote up
/**
 * Ensure a reset() brings us back to the default mark (beginning of file)
 * @throws IOException
 */
@Test
public void testReset() throws IOException {
  String output = singleLineFileInit(file, Charsets.UTF_8);

  PositionTracker tracker = new DurablePositionTracker(meta, file.getPath());
  ResettableInputStream in = new ResettableFileInputStream(file, tracker);

  String result1 = readLine(in, output.length());
  assertEquals(output, result1);

  in.reset();
  String result2 = readLine(in, output.length());
  assertEquals(output, result2);

  String result3 = readLine(in, output.length());
  assertNull("Should be null: " + result3, result3);

  in.close();
}
 
Example 21
Project: tac-kbp-eal   File: GoldArgumentCountsInspector.java   View source code 6 votes vote down vote up
@Override
public void finish() throws IOException {
  outputDirectory.mkdirs();
  log.info("Writing gold counts to {}", outputDirectory);
  Files.asCharSink(new File(outputDirectory, "README.goldCounts.txt"), Charsets.UTF_8).write(
      Resources.asCharSource(
          Resources.getResource(GoldArgumentCountsInspector.class, "/README.scorer.txt"),
          Charsets.UTF_8)
          .read());
  writeTsv(eventTypeCounts.build(), new File(outputDirectory, "eventTypeCounts.tsv"));
  writeTsv(eventArgumentCounts.build(), new File(outputDirectory, "eventArgumentTypeCounts.tsv"));
  writeTsv(genreCounts.build(), new File(outputDirectory, "genreCounts.tsv"));
  writeTsv(argumentMentionTypeCounts.build(), new File(outputDirectory, "mentionTypeCounts.tsv"));
  genreExtractor.logDocIdsWithUndeterminedGenre(
      Files.asCharSink(new File(outputDirectory, "undetermined_genre.txt"), Charsets.UTF_8));
}
 
Example 22
Project: SurvivalMMO   File: Packet.java   View source code 5 votes vote down vote up
public static String readUTF8(ByteBuf in) {
    ByteBuf buffer = in.alloc().buffer();
    byte b;
    while (in.readableBytes() > 0 && (b = in.readByte()) != 0) {
        buffer.writeByte(b);
    }

    return buffer.toString(Charsets.UTF_8);
}
 
Example 23
Project: googles-monorepo-demo   File: PackageSanityTests.java   View source code 5 votes vote down vote up
public PackageSanityTests() {
  setDefault(BaseEncoding.class, BaseEncoding.base64());
  setDefault(int.class, 32);
  setDefault(String.class, "abcd");
  setDefault(Method.class, AbstractPackageSanityTests.class.getDeclaredMethods()[0]);
  setDefault(MapMode.class, MapMode.READ_ONLY);
  setDefault(CharsetEncoder.class, Charsets.UTF_8.newEncoder());
}
 
Example 24
Project: googles-monorepo-demo   File: SourceSinkFactories.java   View source code 5 votes vote down vote up
public static CharSinkFactory asCharSinkFactory(final ByteSinkFactory factory) {
  checkNotNull(factory);
  return new CharSinkFactory() {
    @Override
    public CharSink createSink() throws IOException {
      return factory.createSink().asCharSink(Charsets.UTF_8);
    }

    @Override
    public String getSinkContents() throws IOException {
      return new String(factory.getSinkContents(), Charsets.UTF_8);
    }

    @Override
    public String getExpected(String data) {
      /*
       * Get what the byte sink factory would expect for no written bytes, then append expected
       * string to that.
       */
      byte[] factoryExpectedForNothing = factory.getExpected(new byte[0]);
      return new String(factoryExpectedForNothing, Charsets.UTF_8) + checkNotNull(data);
    }

    @Override
    public void tearDown() throws IOException {
      factory.tearDown();
    }
  };
}
 
Example 25
Project: flume-release-1.7.0   File: TestTaildirEventReader.java   View source code 5 votes vote down vote up
@Test
// Test a basic case where a commit is missed.
public void testBasicCommitFailure() throws IOException {
  File f1 = new File(tmpDir, "file1");
  Files.write("file1line1\nfile1line2\nfile1line3\nfile1line4\n" +
              "file1line5\nfile1line6\nfile1line7\nfile1line8\n" +
              "file1line9\nfile1line10\nfile1line11\nfile1line12\n",
              f1, Charsets.UTF_8);

  ReliableTaildirEventReader reader = getReader();
  List<String> out1 = null;
  for (TailFile tf : reader.getTailFiles().values()) {
    out1 = bodiesAsStrings(reader.readEvents(tf, 4));
  }
  assertTrue(out1.contains("file1line1"));
  assertTrue(out1.contains("file1line2"));
  assertTrue(out1.contains("file1line3"));
  assertTrue(out1.contains("file1line4"));

  List<String> out2 = bodiesAsStrings(reader.readEvents(4));
  assertTrue(out2.contains("file1line1"));
  assertTrue(out2.contains("file1line2"));
  assertTrue(out2.contains("file1line3"));
  assertTrue(out2.contains("file1line4"));

  reader.commit();
  List<String> out3 = bodiesAsStrings(reader.readEvents(4));
  assertTrue(out3.contains("file1line5"));
  assertTrue(out3.contains("file1line6"));
  assertTrue(out3.contains("file1line7"));
  assertTrue(out3.contains("file1line8"));

  reader.commit();
  List<String> out4 = bodiesAsStrings(reader.readEvents(4));
  assertEquals(4, out4.size());
  assertTrue(out4.contains("file1line9"));
  assertTrue(out4.contains("file1line10"));
  assertTrue(out4.contains("file1line11"));
  assertTrue(out4.contains("file1line12"));
}
 
Example 26
Project: googles-monorepo-demo   File: SourceSinkFactories.java   View source code 5 votes vote down vote up
@Override
public String getSinkContents() throws IOException {
  Path file = getPath();
  try (Reader reader = java.nio.file.Files.newBufferedReader(file, Charsets.UTF_8)) {
    StringBuilder builder = new StringBuilder();
    for (int c = reader.read(); c != -1; c = reader.read()) {
      builder.append((char) c);
    }
    return builder.toString();
  }
}
 
Example 27
Project: googles-monorepo-demo   File: FunnelsTest.java   View source code 5 votes vote down vote up
public void testEquals() {
   new EqualsTester()
     .addEqualityGroup(Funnels.byteArrayFunnel())
     .addEqualityGroup(Funnels.integerFunnel())
     .addEqualityGroup(Funnels.longFunnel())
     .addEqualityGroup(Funnels.unencodedCharsFunnel())
     .addEqualityGroup(Funnels.stringFunnel(Charsets.UTF_8))
     .addEqualityGroup(Funnels.stringFunnel(Charsets.US_ASCII))
     .addEqualityGroup(Funnels.sequentialFunnel(Funnels.integerFunnel()),
         SerializableTester.reserialize(Funnels.sequentialFunnel(
             Funnels.integerFunnel())))
     .addEqualityGroup(Funnels.sequentialFunnel(Funnels.longFunnel()))
     .testEquals();
}
 
Example 28
Project: guava-mock   File: PackageSanityTests.java   View source code 5 votes vote down vote up
public PackageSanityTests() {
  setDefault(BaseEncoding.class, BaseEncoding.base64());
  setDefault(int.class, 32);
  setDefault(String.class, "abcd");
  setDefault(Method.class, AbstractPackageSanityTests.class.getDeclaredMethods()[0]);
  setDefault(MapMode.class, MapMode.READ_ONLY);
  setDefault(CharsetEncoder.class, Charsets.UTF_8.newEncoder());
}
 
Example 29
Project: dremio-oss   File: Distributions.java   View source code 5 votes vote down vote up
public static synchronized Distributions getDefaultDistributions()
{
    if (DEFAULT_DISTRIBUTIONS == null) {
        try {
            URL resource = Resources.getResource("dists.dss");
            checkState(resource != null, "Distribution file 'dists.dss' not found");
            DEFAULT_DISTRIBUTIONS = new Distributions(loadDistribution(Resources.asCharSource(resource, Charsets.UTF_8)));
        }
        catch (IOException e) {
            throw Throwables.propagate(e);
        }
    }
    return DEFAULT_DISTRIBUTIONS;
}
 
Example 30
Project: okwallet   File: WalletUtils.java   View source code 5 votes vote down vote up
public static Wallet restorePrivateKeysFromBase58(final InputStream is,
        final NetworkParameters expectedNetworkParameters) throws IOException {
    final BufferedReader keyReader = new BufferedReader(new InputStreamReader(is, Charsets.UTF_8));

    // create non-HD wallet
    final KeyChainGroup group = new KeyChainGroup(expectedNetworkParameters);
    group.importKeys(WalletUtils.readKeys(keyReader, expectedNetworkParameters));
    return new Wallet(expectedNetworkParameters, group);
}
 
Example 31
Project: MooProject   File: NetworkBus.java   View source code 5 votes vote down vote up
/**
 * Processes the packet (last step before sending)
 *
 * @param channel   The channel to send the packet to
 * @param packet    The packet to be sent
 * @param callbacks Callback after receiving a respond
 */
public synchronized void processOut(Channel channel, AbstractPacket packet, Consumer<AbstractPacket>... callbacks) {
    if(channel == null) {
        return;
    }

    // send time and identifier
    packet.setStamp(System.currentTimeMillis());
    if(packet.getQueryUid() == null) {
        packet.setQueryUid(UUID.nameUUIDFromBytes(("Time:" + System.nanoTime()).getBytes(Charsets.UTF_8)));
    }
    channel.writeAndFlush(packet);

    // releases the packetbuf
    try {
        packet.getBuf().release();
    }
    catch(Exception e) {
        //
    }

    // callbacks
    if(callbacks.length != 0) {
        handle.getCallbacks().put(packet.getQueryUid(), new ArrayList<>(Arrays.asList(callbacks)));
    }

    // call handler event
    handle.callEvent(adapter -> adapter.onPacketSend(packet));

    // packet content
    /*String content = (packet instanceof PacketRespond ? " {" + packet.toString().split("\"payload\": ")[1] : "");
    String id = (packet.getQueryUid() + "").substring(0, 2);
    id = ConsoleColor.translateLowSpectrum('&', "&" + (id.substring(0, 1))) + id + ConsoleColor.RESET;

    handle.getLogger().info("[Outgoing " + id + "] '" + packet.getName() + "'" + content);*/
}
 
Example 32
Project: open-kilda   File: FileUtil.java   View source code 5 votes vote down vote up
/**
 * @return the actual number, or -1 if there was an exception.
 */
public int numLines() {
    int result = -1;

    try {
        result = Files.readLines(getFile(), Charsets.UTF_8).size();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return result;
}
 
Example 33
Project: okwallet   File: CryptoTest.java   View source code 5 votes vote down vote up
private String readBackupFromResource(final String filename) throws IOException {
    final BufferedReader reader = new BufferedReader(
            new InputStreamReader(getClass().getResourceAsStream(filename), Charsets.UTF_8));
    final StringBuilder backup = new StringBuilder();
    Io.copy(reader, backup);
    reader.close();

    return backup.toString();
}
 
Example 34
Project: flume-release-1.7.0   File: TestSpoolingFileLineReader.java   View source code 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testDestinationExistsAndDifferentFile() throws IOException {
  File f1 = new File(tmpDir.getAbsolutePath() + "/file1");
  File f1Completed =
      new File(tmpDir.getAbsolutePath() + "/file1" + completedSuffix);

  Files.write("file1line1\nfile1line2\n", f1, Charsets.UTF_8);
  Files.write("file1line1\nfile1XXXe2\n", f1Completed, Charsets.UTF_8);

  ReliableSpoolingFileEventReader parser = getParser();

  List<String> out = Lists.newArrayList();

  for (int i = 0; i < 2; i++) {
    out.add(bodyAsString(parser.readEvent()));
    parser.commit();
  }

  File f2 = new File(tmpDir.getAbsolutePath() + "/file2");
  Files.write("file2line1\nfile2line2\n", f2, Charsets.UTF_8);

  for (int i = 0; i < 2; i++) {
    out.add(bodyAsString(parser.readEvent()));
    parser.commit();
  }
  // Not reached
}
 
Example 35
Project: guava-mock   File: FilesTest.java   View source code 5 votes vote down vote up
public void testReadFile_withCorrectSize() throws IOException {
  File asciiFile = getTestFile("ascii.txt");

  Closer closer = Closer.create();
  try {
    InputStream in = closer.register(new FileInputStream(asciiFile));
    byte[] bytes = Files.readFile(in, asciiFile.length());
    assertTrue(Arrays.equals(ASCII.getBytes(Charsets.US_ASCII), bytes));
  } catch (Throwable e) {
    throw closer.rethrow(e);
  } finally {
    closer.close();
  }
}
 
Example 36
Project: flume-release-1.7.0   File: TestSearchAndReplaceInterceptor.java   View source code 5 votes vote down vote up
private void testSearchReplace(Context context, String input, String output)
    throws Exception {
  Interceptor.Builder builder = InterceptorBuilderFactory.newInstance(
      InterceptorType.SEARCH_REPLACE.toString());
  builder.configure(context);
  Interceptor interceptor = builder.build();

  Event event = EventBuilder.withBody(input, Charsets.UTF_8);
  event = interceptor.intercept(event);
  String val = new String(event.getBody(), Charsets.UTF_8);
  assertEquals(output, val);
  logger.info(val);
}
 
Example 37
Project: DecompiledMinecraft   File: PacketBuffer.java   View source code 5 votes vote down vote up
public PacketBuffer writeString(String string)
{
    byte[] abyte = string.getBytes(Charsets.UTF_8);

    if (abyte.length > 32767)
    {
        throw new EncoderException("String too big (was " + string.length() + " bytes encoded, max " + 32767 + ")");
    }
    else
    {
        this.writeVarIntToBuffer(abyte.length);
        this.writeBytes(abyte);
        return this;
    }
}
 
Example 38
Project: n4js   File: FileBasedProjectModelSetup.java   View source code 5 votes vote down vote up
private void createArchive(URI baseDir) throws IOException {
	File directory = new File(java.net.URI.create(baseDir.toString()));
	File lib = new File(directory, "lib");
	assertTrue(lib.mkdir());
	File nfar = new File(lib, host.archiveProjectId + ".nfar");
	final ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(nfar));
	zipOutputStream.putNextEntry(new ZipEntry("src/A.js"));
	zipOutputStream.putNextEntry(new ZipEntry("src/B.js"));
	zipOutputStream.putNextEntry(new ZipEntry("src/sub/B.js"));
	zipOutputStream.putNextEntry(new ZipEntry("src/sub/C.js"));
	zipOutputStream.putNextEntry(new ZipEntry("src/sub/leaf/D.js"));

	zipOutputStream.putNextEntry(new ZipEntry(IN4JSProject.N4MF_MANIFEST));
	// this will close the stream
	CharStreams.write("ProjectId: " + host.archiveProjectId + "\n" +
			"ProjectType: library\n" +
			"ProjectVersion: 0.0.1-SNAPSHOT\n" +
			"VendorId: org.eclipse.n4js\n" +
			"VendorName: \"Eclipse N4JS Project\"\n" +
			"Output: \"src-gen\"\n" +
			"Sources {\n" +
			"	source {" +
			"		\"src\"\n" +
			"	}\n" +
			"}",
			CharStreams.newWriterSupplier(new OutputSupplier<ZipOutputStream>() {
				@Override
				public ZipOutputStream getOutput() throws IOException {
					return zipOutputStream;
				}
			}, Charsets.UTF_8));
	host.setArchiveFileURI(URI.createURI(nfar.toURI().toString()));
}
 
Example 39
Project: QDrill   File: TestReverseImplicitCast.java   View source code 5 votes vote down vote up
@Test
public void twoWayCast(@Injectable final DrillbitContext bitContext,
                       @Injectable UserServer.UserClientConnection connection) throws Throwable {

  // Function checks for casting from Float, Double to Decimal data types
  try (RemoteServiceSet serviceSet = RemoteServiceSet.getLocalServiceSet();
       Drillbit bit = new Drillbit(CONFIG, serviceSet);
       DrillClient client = new DrillClient(CONFIG, serviceSet.getCoordinator())) {

    // run query.
    bit.run();
    client.connect();
    List<QueryDataBatch> results = client.runQuery(org.apache.drill.exec.proto.UserBitShared.QueryType.PHYSICAL,
        Files.toString(FileUtils.getResourceAsFile("/functions/cast/two_way_implicit_cast.json"), Charsets.UTF_8));

    RecordBatchLoader batchLoader = new RecordBatchLoader(bit.getContext().getAllocator());

    QueryDataBatch batch = results.get(0);
    assertTrue(batchLoader.load(batch.getHeader().getDef(), batch.getData()));

    Iterator<VectorWrapper<?>> itr = batchLoader.iterator();

    ValueVector.Accessor intAccessor1 = itr.next().getValueVector().getAccessor();
    ValueVector.Accessor varcharAccessor1 = itr.next().getValueVector().getAccessor();

    for (int i = 0; i < intAccessor1.getValueCount(); i++) {
      System.out.println(intAccessor1.getObject(i));
      assertEquals(intAccessor1.getObject(i), 10);
      System.out.println(varcharAccessor1.getObject(i));
      assertEquals(varcharAccessor1.getObject(i).toString(), "101");
    }

    batchLoader.clear();
    for (QueryDataBatch result : results) {
      result.release();
    }
  }
}
 
Example 40
Project: QDrill   File: ValueHolderHelper.java   View source code 5 votes vote down vote up
public static VarCharHolder getVarCharHolder(BufferAllocator a, String s){
  VarCharHolder vch = new VarCharHolder();

  byte[] b = s.getBytes(Charsets.UTF_8);
  vch.start = 0;
  vch.end = b.length;
  vch.buffer = a.buffer(b.length); //
  vch.buffer.setBytes(0, b);
  return vch;
}