java.nio.charset.Charset Java Examples

The following examples show how to use java.nio.charset.Charset. 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 Project: Tomcat7.0.67   Author: tryandcatch   File: TestUtf8.java    License: Apache License 2.0 7 votes vote down vote up
@Test
public void testJvmDecoder() {
    CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
    int testCount = 0;
    try {
        for (Utf8TestCase testCase : TEST_CASES) {
            doTest(decoder, testCase, testCase.flagsJvm);
            testCount++;
        }
    } finally {
        System.err.println("Workarounds added to " + workAroundCount +
                " tests to account for known JVM bugs");
        if (testCount < TEST_CASES.size()) {
            System.err.println("Executed " + testCount + " of " +
                    TEST_CASES.size() + " UTF-8 tests before " +
                    "encountering a failure");
        }
    }
}
 
Example #2
Source Project: astor   Author: SpoonLabs   File: DocumentTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testShiftJisRoundtrip() throws Exception {
    String input =
            "<html>"
                    +   "<head>"
                    +     "<meta http-equiv=\"content-type\" content=\"text/html; charset=Shift_JIS\" />"
                    +   "</head>"
                    +   "<body>"
                    +     "before&nbsp;after"
                    +   "</body>"
                    + "</html>";
    InputStream is = new ByteArrayInputStream(input.getBytes(Charset.forName("ASCII")));

    Document doc = Jsoup.parse(is, null, "http://example.com");
    doc.outputSettings().escapeMode(Entities.EscapeMode.xhtml);

    String output = new String(doc.html().getBytes(doc.outputSettings().charset()), doc.outputSettings().charset());

    assertFalse("Should not have contained a '?'.", output.contains("?"));
    assertTrue("Should have contained a '&#xa0;' or a '&nbsp;'.",
            output.contains("&#xa0;") || output.contains("&nbsp;"));
}
 
Example #3
Source Project: flink-learning   Author: zhisheng17   File: NiFiSourceMain.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

    SiteToSiteClientConfig clientConfig = new SiteToSiteClient.Builder()
            .url("http://localhost:8080/nifi")
            .portName("Data for Flink")
            .requestBatchCount(5)
            .buildConfig();

    SourceFunction<NiFiDataPacket> nifiSource = new NiFiSource(clientConfig);
    DataStream<NiFiDataPacket> streamSource = env.addSource(nifiSource).setParallelism(2);

    DataStream<String> dataStream = streamSource.map(new MapFunction<NiFiDataPacket, String>() {
        @Override
        public String map(NiFiDataPacket value) throws Exception {
            return new String(value.getContent(), Charset.defaultCharset());
        }
    });

    dataStream.print();
    env.execute();
}
 
Example #4
Source Project: incubator-atlas   Author: apache   File: ClassType.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void updateSignatureHash(MessageDigest digester, Object val) throws AtlasException {
    if( !(val instanceof  ITypedReferenceableInstance)) {
        throw new IllegalArgumentException("Unexpected value type " + val.getClass().getSimpleName() + ". Expected instance of ITypedStruct");
    }
    digester.update(getName().getBytes(Charset.forName("UTF-8")));

    if(fieldMapping.fields != null && val != null) {
        IReferenceableInstance typedValue = (IReferenceableInstance) val;
        if(fieldMapping.fields.values() != null) {
            for (AttributeInfo aInfo : fieldMapping.fields.values()) {
                Object attrVal = typedValue.get(aInfo.name);
                if (attrVal != null) {
                    aInfo.dataType().updateSignatureHash(digester, attrVal);
                }
            }
        }
    }
}
 
Example #5
Source Project: ground-android   Author: google   File: GeoJsonParser.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns the immutable list of tiles specified in {@param geojson} that intersect {@param
 * bounds}.
 */
public ImmutableList<Tile> intersectingTiles(LatLngBounds bounds, File file) {
  try {
    String fileContents = FileUtils.readFileToString(file, Charset.forName(JSON_SOURCE_CHARSET));
    // TODO: Separate parsing and intersection checks, make asyc (single, completable).
    JSONObject geoJson = new JSONObject(fileContents);
    // TODO: Make features constant.
    JSONArray features = geoJson.getJSONArray(FEATURES_KEY);

    return stream(toArrayList(features))
        .map(GeoJsonTile::new)
        .filter(tile -> tile.boundsIntersect(bounds))
        .map(this::jsonToTile)
        .collect(toImmutableList());

  } catch (JSONException | IOException e) {
    Log.e(TAG, "Unable to parse JSON", e);
  }

  return ImmutableList.of();
}
 
Example #6
Source Project: deeplearning4j   Author: eclipse   File: FileRecordReader.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public List<Record> loadFromMetaData(List<RecordMetaData> recordMetaDatas) throws IOException {
    List<Record> out = new ArrayList<>();

    for (RecordMetaData meta : recordMetaDatas) {
        URI uri = meta.getURI();

        List<Writable> list;
        try(InputStream s = streamCreatorFn.apply(uri)) {
            list = loadFromStream(uri, s, Charset.forName(charset));
        } catch (IOException e){
            throw new RuntimeException("Error reading from stream for URI: " + uri);
        }

        out.add(new org.datavec.api.records.impl.Record(list, meta));
    }

    return out;
}
 
Example #7
Source Project: joyqueue   Author: chubaostream   File: ArchiveSerializer.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 计算消费日志占用的长度
 * <br>
 * (byte[]:messageId, int:brokerId, byte[16]:clientIP, long:consumeTime, String:app(变长), )
 *
 * @param consumeLog
 * @return
 */
private static int consumeLogSize(ConsumeLog consumeLog) {
    int size = 0;
    // messageId
    size += consumeLog.getBytesMessageId().length;
    // brokerId
    size += 4;
    // clientIp
    size += 16;
    // consumeTime
    size += 8;
    // app长度
    size += 2;
    // app
    size += consumeLog.getApp().getBytes(Charset.forName("utf-8")).length;
    return size;
}
 
Example #8
Source Project: hivemq-community-edition   Author: hivemq   File: PluginLoaderImplTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void test_load_plugins_folder_contains_jar_file() throws Exception {
    final File pluginsFolder = temporaryFolder.newFolder("extension");
    final File pluginFolder = temporaryFolder.newFolder("extension", "plugin1");
    final File file = new File(pluginFolder, "extension.jar");
    FileUtils.writeStringToFile(pluginFolder.toPath().resolve("hivemq-extension.xml").toFile(),
            validPluginXML1,
            Charset.defaultCharset());
    final JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class).
            addAsServiceProviderAndClasses(ExtensionMain.class, TestExtensionMainImpl.class);

    javaArchive.as(ZipExporter.class).exportTo(file);

    when(classServiceLoader.load(eq(ExtensionMain.class), any(ClassLoader.class))).thenReturn(new ArrayList<>());

    pluginLoader.loadPlugins(pluginsFolder.toPath(), false, ExtensionMain.class);

    //Let's verify that the extension was loaded
    verify(classServiceLoader).load(any(Class.class), any(ClassLoader.class));
}
 
Example #9
Source Project: spring-boot-starter-netty   Author: Leibnizhu   File: NettyHttpServletResponse.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setContentType(String type) {
    if (isCommitted()) {
        return;
    }
    if (hasWriter()) {
        return;
    }
    if (null == type) {
        contentType = null;
        return;
    }
    MediaType mediaType = MediaType.parse(type);
    Optional<Charset> charset = mediaType.charset();
    if (charset.isPresent()) {
        setCharacterEncoding(charset.get().name());
    }
    contentType = mediaType.type() + '/' + mediaType.subtype();
}
 
Example #10
Source Project: cyberduck   Author: iterate-ch   File: DefaultUploadFeatureTest.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testUpload() throws Exception {
    final DefaultUploadFeature<VersionId> m = new DefaultUploadFeature<VersionId>(new GoogleStorageWriteFeature(session));
    final Path container = new Path("test.cyberduck.ch", EnumSet.of(Path.Type.directory, Path.Type.volume));
    final Path test = new Path(container, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
    final Local local = new Local(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    final String random = new RandomStringGenerator.Builder().build().generate(1000);
    final OutputStream out = local.getOutputStream(false);
    IOUtils.write(random, out, Charset.defaultCharset());
    out.close();
    final TransferStatus status = new TransferStatus();
    status.setLength(random.getBytes().length);
    m.upload(test, local, new BandwidthThrottle(BandwidthThrottle.UNLIMITED),
        new DisabledStreamListener(), status, new DisabledLoginCallback());
    assertTrue(new GoogleStorageFindFeature(session).find(test));
    final PathAttributes attributes = new GoogleStorageListService(session).list(container,
        new DisabledListProgressListener()).get(test).attributes();
    assertEquals(random.getBytes().length, attributes.getSize());
    new GoogleStorageDeleteFeature(session).delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback());
    local.delete();
}
 
Example #11
Source Project: nifi   Author: apache   File: PutCassandraQL.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Determines the CQL statement that should be executed for the given FlowFile
 *
 * @param session  the session that can be used to access the given FlowFile
 * @param flowFile the FlowFile whose CQL statement should be executed
 * @return the CQL that is associated with the given FlowFile
 */

private String getCQL(final ProcessSession session, final FlowFile flowFile, final Charset charset) {
    // Read the CQL from the FlowFile's content
    final byte[] buffer = new byte[(int) flowFile.getSize()];
    session.read(flowFile, new InputStreamCallback() {
        @Override
        public void process(final InputStream in) throws IOException {
            StreamUtils.fillBuffer(in, buffer);
        }
    });

    // Create the PreparedStatement string to use for this FlowFile.
    return new String(buffer, charset);
}
 
Example #12
Source Project: teiid-spring-boot   Author: teiid   File: TeiidRSProvider.java    License: Apache License 2.0 5 votes vote down vote up
public ResponseEntity<InputStreamResource> executeQuery(final String sql, boolean json, final boolean passthroughAuth)
        throws SQLException {
    Connection conn = null;
    try {
        conn = getConnection();
        Statement statement = conn.createStatement();
        final boolean hasResultSet = statement.execute(sql);
        Object result = null;
        if (hasResultSet) {
            ResultSet rs = statement.getResultSet();
            if (rs.next()) {
                result = rs.getObject(1);
            } else {
                throw new ResponseStatusException(HttpStatus.BAD_REQUEST,"Only result producing procedures are allowed");
            }
        }
        InputStream is = handleResult(Charset.defaultCharset().name(), result);
        InputStreamResource inputStreamResource = new InputStreamResource(is);
        HttpHeaders httpHeaders = new HttpHeaders();
        return new ResponseEntity<InputStreamResource>(inputStreamResource, httpHeaders, HttpStatus.OK);
    } finally {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
        }
    }
}
 
Example #13
Source Project: hottub   Author: dsrg-uoft   File: StreamTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void testConstructException() {
    try (Stream<String> s = Files.lines(testFolder.resolve("notExist"), Charset.forName("UTF-8"))) {
        s.forEach(l -> fail("File is not even exist!"));
    } catch (IOException ioe) {
        assertTrue(ioe instanceof NoSuchFileException);
    }
}
 
Example #14
Source Project: msf4j   Author: wso2   File: EchoService.java    License: Apache License 2.0 5 votes vote down vote up
@POST
@Path("fileecho")
@Consumes("text/plain")
@Produces("text/plain")
public String fileWrite(String body) throws InterruptedException, IOException {
    String returnStr = "";
    java.nio.file.Path tempfile = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
    Files.write(tempfile, body.getBytes(Charset.defaultCharset()),
                StandardOpenOption.CREATE,
                StandardOpenOption.TRUNCATE_EXISTING);
    returnStr = new String(Files.readAllBytes(tempfile), Charset.defaultCharset());
    Files.delete(tempfile);
    return returnStr;
}
 
Example #15
Source Project: OpenYOLO-Android   Author: openid   File: LoginViewModel.java    License: Apache License 2.0 5 votes vote down vote up
private String base64Sha256Hash(byte[] data) {
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] sha256Bytes = digest.digest(data);
        return new String(
                Base64.encode(sha256Bytes, Base64.NO_WRAP | Base64.URL_SAFE),
                Charset.forName("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("SHA-256 not supported on this platform!");
    }
}
 
Example #16
Source Project: netty4.0.27Learn   Author: wuyinxian124   File: HttpRequestEncoderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testQueryStringPath() throws Exception {
    HttpRequestEncoder encoder = new HttpRequestEncoder();
    ByteBuf buffer = Unpooled.buffer(64);
    encoder.encodeInitialLine(buffer, new DefaultHttpRequest(HttpVersion.HTTP_1_1,
            HttpMethod.GET, "/?url=http://example.com"));
    String req = buffer.toString(Charset.forName("US-ASCII"));
    assertEquals("GET /?url=http://example.com HTTP/1.1\r\n", req);
}
 
Example #17
Source Project: ReactiveLab   Author: Netflix   File: ClientServer.java    License: Apache License 2.0 5 votes vote down vote up
public static Observable<String> createRequest(String host, int port) {

        /**
         * Creates an HTTP client bound to the provided host and port.
         */
        return RxNetty.createHttpClient(host, port)
                /* Submit an HTTP GET request with uri "/hello" */
                .submit(HttpClientRequest.createGet("/hello"))
                /* Print the HTTP initial line and headers. Return the content.*/
                .flatMap(response -> {
                    /**
                     * Printing the HTTP initial line.
                     */
                    System.out.println(response.getHttpVersion().text() + ' ' + response.getStatus().code()
                                       + ' ' + response.getStatus().reasonPhrase());
                    /**
                     * Printing HTTP headers.
                     */
                    for (Map.Entry<String, String> header : response.getHeaders().entries()) {
                        System.out.println(header.getKey() + ": " + header.getValue());
                    }

                    // Line break after the headers.
                    System.out.println();

                    return response.getContent();
                })
                /* Convert the ByteBuf for each content chunk into a string. */
                .map(byteBuf -> byteBuf.toString(Charset.defaultCharset()));

    }
 
Example #18
Source Project: openjdk-8   Author: bpupadhyaya   File: ToolBox.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static void compareLines(Path path, List<String> strings,
        String encoding, boolean trim)
        throws FileNotFoundException, IOException, ResourcesNotEqualException {
    Charset charset = encoding != null ?
            Charset.forName(encoding) :
            defaultCharset;
    List<String> list = Files.readAllLines(path, charset);
    compareLines(list, strings, trim);
}
 
Example #19
Source Project: Asqatasun   Author: Asqatasun   File: HttpRequestHandler.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
public String getHttpContent (String url) throws URISyntaxException, UnknownHostException, IOException, IllegalCharsetNameException {
    if (StringUtils.isEmpty(url)){
        return "";
    }
    String encodedUrl = getEncodedUrl(url);
    CloseableHttpClient httpClient = getHttpClient(encodedUrl);
    HttpGet get = new HttpGet(encodedUrl);
    try {
        LOGGER.debug("executing request to retrieve content on " + get.getURI());
        HttpResponse response = httpClient.execute(get);
        LOGGER.debug("received " + response.getStatusLine().getStatusCode() + " from get request");
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            LOGGER.debug("status == HttpStatus.SC_OK " );
            return EntityUtils.toString(response.getEntity(), Charset.defaultCharset());
        } else {
            LOGGER.debug("status != HttpStatus.SC_OK " );
            return "";
        }
        
    } catch (NullPointerException ioe) {
        LOGGER.debug("NullPointerException");
        return "";
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        get.releaseConnection();
        LOGGER.debug("finally");
        httpClient.close();
    }
}
 
Example #20
Source Project: che   Author: eclipse   File: CheBootstrapTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
private void writePropertiesFile(File parent, String name, Properties properties)
    throws IOException {
  File propertiesFile = new File(parent, name);
  try (Writer writer = Files.newWriter(propertiesFile, Charset.forName("UTF-8"))) {
    properties.store(writer, null);
  }
}
 
Example #21
Source Project: vespa   Author: vespa-engine   File: ErrorCodesTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Always meant to be run against HEAD.
 */
@Test
public void errorCodesMatchCppDefinitions() throws Exception {
    final NamedErrorCodes codes = new NamedErrorCodes();
    enumerateAllDocumentProtocolErrorCodes(codes);

    final String javaGoldenFile = TestFileUtil.getPath("HEAD-java-golden-error-codes.txt");
    final String javaGoldenData = codes.toSortedKeyValueString();
    TestFileUtil.writeToFile(javaGoldenFile, javaGoldenData);

    final String cppGoldenFile = TestFileUtil.getPath("HEAD-cpp-golden-error-codes.txt");
    final String cppGoldenData = new String(TestFileUtil.readFile(cppGoldenFile), Charset.forName("UTF-8"));
    assertEquals(javaGoldenData, cppGoldenData);
}
 
Example #22
Source Project: scheduling   Author: ow2-proactive   File: TestUnauthorizedScripts.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
@BeforeClass
public static void before() throws Throwable {
    // initialize the scheduler & rm with a custom authorized script folder

    scriptsFolder = folder.newFolder("scripts");
    File schedulerConfigFile = generateConfigFile(originalSchedulerConfigFile.toURI(), "schedulerConfig");
    File rmConfigFile = generateConfigFile(originalRMConfigFile.toURI(), "rmConfig");

    // create authorized and unauthorized scripts
    String tempFolderPathEscaped = folder.getRoot().getAbsolutePath().replace("\\", "\\\\");
    authorizedForkScriptContent = "new File('" + tempFolderPathEscaped + "','fork_auth.out').write('ok')";
    authorizedCleanScriptContent = "new File('" + tempFolderPathEscaped + "','clean_auth.out').write('ok')";
    authorizedSelectionScriptContent = "new File('" + tempFolderPathEscaped +
                                       "','selection_auth.out').write('ok'); selected=true";

    File forkScript = new File(scriptsFolder, "forkScript");
    FileUtils.write(forkScript, authorizedForkScriptContent, Charset.defaultCharset(), false);
    File cleanScript = new File(scriptsFolder, "cleanScript");
    FileUtils.write(cleanScript, authorizedCleanScriptContent, Charset.defaultCharset(), false);
    File selectionScript = new File(scriptsFolder, "selectionScript");
    FileUtils.write(selectionScript, authorizedSelectionScriptContent, Charset.defaultCharset(), false);

    unauthorizedForkScriptContent = "new File('" + tempFolderPathEscaped + "','fork.out').write('ko')";
    unauthorizedCleanScriptContent = "new File('" + tempFolderPathEscaped + "','clean.out').write('ko')";
    unauthorizedSelectionScriptContent = "new File('" + tempFolderPathEscaped +
                                         "','selection.out').write('ko'); selected=true";

    // start the configured scheduler
    schedulerHelper = new SchedulerTHelper(true,
                                           schedulerConfigFile.getAbsolutePath(),
                                           rmConfigFile.getAbsolutePath(),
                                           null);
}
 
Example #23
Source Project: swellrt   Author: SwellRT   File: AccountServiceTest.java    License: Apache License 2.0 5 votes vote down vote up
protected ServletInputStream asServletInputStream(final String data) {
  return new ServletInputStream() {

    ByteArrayInputStream byteInputStream =
        new ByteArrayInputStream(data.getBytes(Charset.forName("UTF-8")));

    int b;

    @Override
    public boolean isFinished() {
      return b == -1;
    }

    @Override
    public boolean isReady() {
      return true;
    }

    @Override
    public void setReadListener(ReadListener readListener) {
    }

    @Override
    public int read() throws IOException {
      this.b = byteInputStream.read();
      return b;
    }

  };
}
 
Example #24
Source Project: log4j2-elasticsearch   Author: rfoltyns   File: AdminOperationsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void passesIndexTemplateToClient() {

    //given
    HCHttp factory = Mockito.spy(HCHttpTest.createDefaultHttpObjectFactoryBuilder().build());

    HttpClient httpClient = mockedHttpClient(factory);
    AtomicReference<ByteBuf> argCaptor = new AtomicReference<>();
    mockedResult(httpClient, true, argCaptor);

    IndexTemplate indexTemplate = spy(IndexTemplate.newBuilder()
            .withPath("classpath:indexTemplate.json")
            .withName("testName")
            .build());

    String expectedPayload = indexTemplate.getSource();

    // when
    factory.execute(indexTemplate);

    // then
    ArgumentCaptor<IndexTemplateRequest> requestArgumentCaptor = ArgumentCaptor.forClass(IndexTemplateRequest.class);
    verify(httpClient).execute(requestArgumentCaptor.capture(), any());

    assertEquals(argCaptor.get().toString(Charset.forName("UTF-8")), expectedPayload);

}
 
Example #25
Source Project: jdk8u-dev-jdk   Author: frohoff   File: ISO2022_CN_CNS.java    License: GNU General Public License v2.0 5 votes vote down vote up
public Encoder(Charset cs)
{
    super(cs);
    SODesig = "$)G";
    SS2Desig = "$*H";
    SS3Desig = "$+I";

    try {
        Charset cset = Charset.forName("EUC_TW"); // CNS11643
        ISOEncoder = cset.newEncoder();
    } catch (Exception e) { }
}
 
Example #26
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: CheckEncodingPropertiesFile.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static String checkConvertMime2Java(String xml) {
    final String jName = Encodings.convertMime2JavaEncoding(xml);
    final String jCharsetName;
    try {
        jCharsetName = Charset.forName(jName).name();
    } catch (Exception x) {
        throw new Error("Unrecognized charset returned by Encodings.convertMime2JavaEncoding(\""+xml+"\")", x);
    }
    System.out.println("Encodings.convertMime2JavaEncoding(\""+xml+"\") = \""+jName+"\" ("+jCharsetName+")");
    return jName;
}
 
Example #27
Source Project: nifi   Author: apache   File: TestGetSchema.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@Ignore("Does not work on windows")
public void testSchemaFromFileSystem() throws IOException {
    File schemaFile = temp.newFile("schema.avsc");
    FileOutputStream out = new FileOutputStream(schemaFile);
    out.write(bytesFor(SCHEMA.toString(), Charset.forName("utf8")));
    out.close();

    Schema schema = AbstractKiteProcessor.getSchema(
            schemaFile.toString(), DefaultConfiguration.get());

    Assert.assertEquals("Schema from file should match", SCHEMA, schema);
}
 
Example #28
Source Project: j2objc   Author: google   File: ChannelsTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testNewWriterWritableByteChannelString() throws IOException {
    this.fouts = new FileOutputStream(tmpFile);
    WritableByteChannel wbChannel = Channels.newChannel(this.fouts);
    Writer testWriter = Channels.newWriter(wbChannel, CODE_SET); //$NON-NLS-1$
    Writer testWriter_s = Channels.newWriter(wbChannel, Charset.forName(
            CODE_SET).newEncoder(), //$NON-NLS-1$
            -1);

    String writebuf = ""; //$NON-NLS-1$
    for (int val = 0; val < this.writebufSize / 2; val++) {
        writebuf = writebuf + ((char) (val + 64));
    }
    byte[] bit = new byte[1];
    bit[0] = 80;
    this.fouts.write(bit);
    this.assertFileSizeSame(tmpFile, 1);

    // writer continues to write after '1',what the fouts write
    testWriter.write(writebuf);
    testWriter.flush();
    this.assertFileSizeSame(tmpFile, this.writebufSize / 2 + 1);
    // testwriter_s does not know if testwrite writes
    testWriter_s.write(writebuf);
    testWriter.flush();
    this.assertFileSizeSame(tmpFile, this.writebufSize / 2 + 1);
    // testwriter_s even does not know if himself writes?
    testWriter_s.write(writebuf);
    testWriter.flush();
    this.assertFileSizeSame(tmpFile, this.writebufSize / 2 + 1);

    // close the fouts, no longer writable for testWriter
    for (int val = 0; val < this.writebufSize; val++) {
        writebuf = writebuf + ((char) (val + 64));
    }
    this.fouts.close();
    testWriter_s.write(writebuf);
    testWriter.flush();
    this.assertFileSizeSame(tmpFile, this.writebufSize / 2 + 1);
}
 
Example #29
Source Project: osmdroid   Author: osmdroid   File: DefaultShapeMetaSetter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void set(DbfRecord metadata, Polygon polygon) throws ParseException {
    if (metadata != null) {
        metadata.setStringCharset(Charset.defaultCharset());
        polygon.setSnippet(metadata.toMap().toString());
        polygon.setTitle(getSensibleTitle(polygon.getSnippet()));
    }
    final BoundingBox boundingBox = polygon.getBounds();
    polygon.setSubDescription(boundingBox.toString());
}
 
Example #30
Source Project: kafka-statsd-reporter   Author: myfitnesspal   File: StatsdReporter.java    License: MIT License 5 votes vote down vote up
private void flush() {
	try {
		int sizeOfBuffer = sendBuffer.position();
		if (sizeOfBuffer <= 0) {
			// empty buffer
			return;
		}

		// send and reset the buffer
		sendBuffer.flip();
		int nbSentBytes = channel.send(sendBuffer, address);
		LOG.debug("Send entirely stat :" + new String(sendBuffer.array(), Charset.forName("UTF-8")) + " to host "
				+ address.getHostName() + " : " + address.getPort());
		if (sizeOfBuffer != nbSentBytes) {
			LOG.error("Could not send entirely stat (" + sendBuffer.toString() + ") : "
					+  new String(sendBuffer.array(), Charset.forName("UTF-8")) + " to host "
					+ address.getHostName() + " : " + address.getPort() + ". Only sent " + nbSentBytes
					+ " out of " + sizeOfBuffer);
		}
		sendBuffer.limit(sendBuffer.capacity());
		sendBuffer.rewind();
	} catch (IOException e) {
		LOG.error("Could not send entirely stat (" + sendBuffer.toString() + ") : "
				+  new String(sendBuffer.array(), Charset.forName("UTF-8")) + " to host "
				+ address.getHostName() + " : "	+ address.getPort(), e);
	}
}