Java Code Examples for java.nio.charset.StandardCharsets#UTF_8

The following examples show how to use java.nio.charset.StandardCharsets#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: PartitionGroupStoreManager.java    From joyqueue with Apache License 2.0 6 votes vote down vote up
private Checkpoint recoverCheckpoint() {
    try {
        File checkpointFile =  new File(base, CHECKPOINT_FILE);
        if(checkpointFile.isFile()) {
            byte[] serializedData = new byte[(int) checkpointFile.length()];
            try (FileInputStream fis = new FileInputStream(checkpointFile)) {
                if (serializedData.length != fis.read(serializedData)) {
                    throw new IOException("File length not match!");
                }
            }
            String jsonString = new String(serializedData, StandardCharsets.UTF_8);
            Checkpoint checkpoint = JSON.parseObject(jsonString, Checkpoint.class);
            if(null != checkpoint) {
                logger.info("Checkpoint file recovered: {}.", checkpoint);
            }
            return checkpoint;
        } else {
            logger.warn("Checkpoint file is NOT found, continue recover...");
        }
    } catch (Throwable t) {
        logger.warn("Recover checkpoint exception, continue recover...", t);
    }
    return null;
}
 
Example 2
Source File: StringBuilderEncoderTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncodeText_JapaneseTextUtf8DoesntFitCharBuff_BytesDontFitByteBuff() throws Exception {
    final StringBuilderEncoder helper = new StringBuilderEncoder(StandardCharsets.UTF_8, 4, 8 * 1024);
    final StringBuilder text = new StringBuilder( // 日本語テスト文章
            "\u65e5\u672c\u8a9e\u30c6\u30b9\u30c8\u6587\u7ae0");
    final SpyByteBufferDestination destination = new SpyByteBufferDestination(3, 50);
    helper.encode(text, destination);

    assertEquals("drained", 7, destination.drainPoints.size());
    destination.drain(destination.getByteBuffer());

    final byte[] utf8 = text.toString().getBytes(StandardCharsets.UTF_8);
    for (int i = 0; i < utf8.length; i++) {
        assertEquals("byte at " + i, utf8[i], destination.drained.get(i));
    }
}
 
Example 3
Source File: ModelDataGenerator.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
private boolean generateTable(TableDesc table) throws IOException {
    TableGenConfig config = new TableGenConfig(table, this);
    if (!config.needGen)
        return false;

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    PrintWriter pout = new PrintWriter(new OutputStreamWriter(bout, StandardCharsets.UTF_8));

    generateTableInternal(table, config, pout);

    pout.close();
    bout.close();

    saveResource(bout.toByteArray(), path(table));
    return true;
}
 
Example 4
Source File: AuthTime.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Reads an LC style string from a channel, which is a int32 length
 * plus a UTF-8 encoded string possibly ends with \0.
 * @throws IOException if there is a format error
 * @throws BufferUnderflowException if goes beyond the end
 */
private static String readStringWithLength(SeekableByteChannel chan)
        throws IOException {
    ByteBuffer bb = ByteBuffer.allocate(4);
    bb.order(ByteOrder.nativeOrder());
    chan.read(bb);
    bb.flip();
    int len = bb.getInt();
    if (len > 1024) {
        // Memory attack? The string should be fairly short.
        throw new IOException("Invalid string length");
    }
    bb = ByteBuffer.allocate(len);
    if (chan.read(bb) != len) {
        throw new IOException("Not enough string");
    }
    byte[] data = bb.array();
    return (data[len-1] == 0)?
            new String(data, 0, len-1, StandardCharsets.UTF_8):
            new String(data, StandardCharsets.UTF_8);
}
 
Example 5
Source File: WmfOnlineStandardDumpFile.java    From Wikidata-Toolkit with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean fetchIsDone() {
	boolean found = false;
	try (InputStream in = this.webResourceFetcher
			.getInputStreamForUrl(getBaseUrl() + this.projectName + "-"
					+ dateStamp + "-md5sums.txt")) {
		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(in, StandardCharsets.UTF_8));
		String inputLine;
		String filePostfix = WmfDumpFile
				.getDumpFilePostfix(this.dumpContentType);
		while (!found && (inputLine = bufferedReader.readLine()) != null) {
			if (inputLine.endsWith(filePostfix)) {
				found = true;
			}
		}
		bufferedReader.close();
	} catch (IOException e) {
		// file not found or not readable; just return false
	}
	return found;
}
 
Example 6
Source File: TableParser.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public List<Record> readAllRecords(InputStream ios, int maxLines) throws IOException, NumberFormatException {

    List<Record> records = new ArrayList<>();

    BufferedReader dataIS = new BufferedReader(new InputStreamReader(ios, StandardCharsets.UTF_8));
    int count = 0;
    while ((maxLines < 0) || (count < maxLines)) {
      String line = dataIS.readLine();
      if (line == null)
        break;
      if (line.startsWith(comment))
        continue;
      if (line.trim().isEmpty())
        continue;
      if (debug)
        System.out.printf("%s%n", line);
      Record r = Record.make(line, fields);
      if (r != null)
        records.add(r);
      count++;
    }

    return records;
  }
 
Example 7
Source File: PassTicketTestController.java    From api-layer with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Validates the PassTicket in authorization header.
 */
@GetMapping(value = "/api/v1/passticketTest")
@ApiOperation(value = "Validate that the PassTicket in Authorization header is valid", tags = { "Test Operations" })
public void passticketTest(@RequestHeader("authorization") String authorization,
        @RequestParam(value = "applId", defaultValue = "", required = false) String applId)
        throws IRRPassTicketEvaluationException
{
    if (authorization != null && authorization.toLowerCase().startsWith("basic")) {
        String base64Credentials = authorization.substring("Basic".length()).trim();
        String credentials = new String(Base64.getDecoder().decode(base64Credentials), StandardCharsets.UTF_8);
        String[] values = credentials.split(":", 2);
        String userId = values[0];
        String passTicket = values[1];
        if (applId.isEmpty()) {
             applId = defaultApplId;
        }
        passTicketService.evaluate(userId, applId, passTicket);
    } else {
        throw new IllegalArgumentException("Missing Basic authorization header");
    }
}
 
Example 8
Source File: LauncherMeta.java    From fabric-installer with Apache License 2.0 5 votes vote down vote up
public VersionMeta getVersionMeta() throws IOException {
	if(versionMeta == null){
		URL url = new URL(this.url);
		URLConnection conn = url.openConnection();
		try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
			versionMeta = Utils.GSON.fromJson(reader, VersionMeta.class);
		}
	}
	return versionMeta;
}
 
Example 9
Source File: TestSelectorList.java    From buck with Apache License 2.0 5 votes vote down vote up
public Builder addBase64EncodedTestSelector(String b64Selector) {
  String[] selectorParts = b64Selector.split(",");
  if (selectorParts.length != 2) {
    throw new IllegalArgumentException();
  }
  String className = selectorParts[0];
  String methodName = selectorParts[1];
  Base64.Decoder decoder = Base64.getDecoder();
  String decodedClassName = new String(decoder.decode(className), StandardCharsets.UTF_8);
  String decodedMethodName = new String(decoder.decode(methodName), StandardCharsets.UTF_8);
  this.testSelectors.add(new SimpleTestSelector(decodedClassName, decodedMethodName));
  return this;
}
 
Example 10
Source File: MDSUrlService.java    From fido2 with GNU Lesser General Public License v2.1 5 votes vote down vote up
private MetadataStatement retrieveMetadataStatement(URI uri, String entryHash, MessageDigest digest, boolean forceUpdate) throws Exception {
    String filename = filenameFromURI(uri);
    String data = null;

    if (!forceUpdate) {
        data = storage.loadData(namespace, filename);
    }

    if (data == null) {
        String url = URLDecoder.decode(uri.toString(), StandardCharsets.UTF_8.name());
        url = addToken(url);
        url = url.replaceAll("#", "%23");
        URI realURI = URI.create(url);
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(realURI, String.class);
        data = responseEntity.getBody();

        if(data == null){
            logger.severe("Null hash.");
            throw new Exception("Null hash");
        }

        String hash = Base64.getUrlEncoder().withoutPadding().encodeToString(digest.digest(data.getBytes()));
        if (!hash.equals(entryHash)) {
            logger.log(Level.SEVERE, "Bad hash. {0} != {1}  Skipping ", new Object[]{hash, entryHash});
            throw new Exception("Bad hash");
        }
        storage.saveData(namespace, filename, data);
    }

    String decoded;
    try {
        decoded = new String(Base64Utils.decodeFromUrlSafeString(data), StandardCharsets.UTF_8);
    } catch (Exception e) {
        // TODO: known bug: test server does not base64 this info
        decoded = data;
    }
    return objectMapper.readValue(decoded, MetadataStatement.class);

}
 
Example 11
Source File: ActivityTrackingControllerTest.java    From syndesis with Apache License 2.0 5 votes vote down vote up
private static String resource(String file) throws IOException {
    try (InputStream is = requireNonNull(ActivityTrackingControllerTest.class.getClassLoader().getResourceAsStream(file)) ) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        copy(is, os);
        return new String(os.toByteArray(), StandardCharsets.UTF_8);
    }
}
 
Example 12
Source File: CommonG.java    From bdt with Apache License 2.0 5 votes vote down vote up
/**
 * Method to convert one json to yaml file - backup&restore functionality
 * <p>
 * File will be placed on path /target/test-classes
 */
public String asYaml(String jsonStringFile) throws IOException {

    InputStream stream = getClass().getClassLoader().getResourceAsStream(jsonStringFile);

    Writer writer = new StringWriter();
    char[] buffer = new char[1024];
    Reader reader;

    if (stream == null) {
        this.getLogger().error("File does not exist: {}", jsonStringFile);
        throw new FileNotFoundException("ERR! File not found: " + jsonStringFile);
    }

    try {
        reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }
    } catch (Exception readerexception) {
        this.getLogger().error(readerexception.getMessage());
    } finally {
        try {
            stream.close();
        } catch (Exception closeException) {
            this.getLogger().error(closeException.getMessage());
        }
    }
    String text = writer.toString();

    String std = text.replace("\r", "").replace("\n", ""); // make sure we have unix style text regardless of the input

    // parse JSON
    JsonNode jsonNodeTree = new ObjectMapper().readTree(std);
    // save it as YAML
    String jsonAsYaml = new YAMLMapper().writeValueAsString(jsonNodeTree);
    return jsonAsYaml;
}
 
Example 13
Source File: StackdriverLogging.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void accept(PubSubMessage message, Context context) {
  String name = "World";

  if (!message.getData().isEmpty()) {
    name = new String(Base64.getDecoder().decode(
        message.getData().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
  }
  String res = String.format("Hello, %s", name);
  logger.info(res);
}
 
Example 14
Source File: RedisConfig.java    From yshopmall with Apache License 2.0 5 votes vote down vote up
@Override
public T deserialize(byte[] bytes) {
    if (bytes == null || bytes.length <= 0) {
        return null;
    }
    String str = new String(bytes, StandardCharsets.UTF_8);
    return JSON.parseObject(str, clazz);
}
 
Example 15
Source File: ClientConnection.java    From yacy_grid_mcp with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static JSONObject loadJSONObject(String source_url) throws IOException {
    byte[] b = load(source_url);
    return new JSONObject(new JSONTokener(new String(b, StandardCharsets.UTF_8)));
}
 
Example 16
Source File: RestDifferEndpoint.java    From EDDI with Apache License 2.0 4 votes vote down vote up
private DeliverCallback conversationCreatedCallback() {
    return (consumerTag, delivery) -> {
        final long deliveryTag = delivery.getEnvelope().getDeliveryTag();
        String receivedMessage = new String(delivery.getBody(), StandardCharsets.UTF_8);
        log.trace("Received Raw Conversation Created Message: {}", receivedMessage);

        try {
            var conversationCreatedEvent = jsonSerialization.deserialize(receivedMessage, ConversationCreatedEvent.class);

            if (conversationCreatedEvent.getPayload().getError() != null) {
                log.error("ConversationCreated event contained error '{}'", receivedMessage);
                differPublisher.positiveDeliveryAck(deliveryTag);
                return;
            }

            var payload = conversationCreatedEvent.getPayload();
            var conversationId = payload.getConversation().getId();
            var participantIds = payload.getParticipantIds();
            // in case there are multiple bots part of the conversation, we need to send this data to all of them
            var botUserParticipantIds = filterBotUserParticipantIds(participantIds);

            if (botUserParticipantIds.isEmpty()) {
                //No bot involved in this conversation
                log.debug(logStatementIgnoredEvent, receivedMessage);
                differPublisher.positiveDeliveryAck(deliveryTag);
                return;
            }

            log.info(" [x] Received and accepted amqp event: {}", receivedMessage);

            var conversationInfo = createDifferConversation(conversationId, participantIds, botUserParticipantIds);
            log.debug("Differ Conversation created. {}", conversationInfo);

            botUserParticipantIds.forEach(botUserId ->
                    startConversationWithUser(delivery, botUserId, conversationId, conversationCreatedEvent, conversationInfo));
        } catch (Exception e) {
            log.error("Error processing delivery {} of conversation.created.eddi with body {}", deliveryTag, receivedMessage);
            log.error(e.getLocalizedMessage(), e);
            differPublisher.negativeDeliveryAck(delivery);
        }
    };
}
 
Example 17
Source File: TestBootstrap.java    From cdep with Apache License 2.0 4 votes vote down vote up
private String main(String... args) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos);
    new Bootstrap(getDownloadFolder(), ps).go(args);
    return new String(baos.toByteArray(), StandardCharsets.UTF_8);
}
 
Example 18
Source File: IndexController.java    From alf.io with GNU General Public License v3.0 4 votes vote down vote up
/**
 <pre>
 { path: '', component: EventListComponent, canActivate: [LanguageGuard] },
 { path: 'event/:eventShortName', component: EventDisplayComponent, canActivate: [EventGuard, LanguageGuard] },
 { path: 'event/:eventShortName/reservation/:reservationId', children: [
 { path: 'book', component: BookingComponent, canActivate: reservationsGuard },
 { path: 'overview', component: OverviewComponent, canActivate: reservationsGuard },
 { path: 'waitingPayment', redirectTo: 'waiting-payment'},
 { path: 'waiting-payment', component: OfflinePaymentComponent, canActivate: reservationsGuard },
 { path: 'processing-payment', component: ProcessingPaymentComponent, canActivate: reservationsGuard },
 { path: 'success', component: SuccessComponent, canActivate: reservationsGuard },
 { path: 'not-found', component: NotFoundComponent, canActivate: reservationsGuard },
 { path: 'error', component: ErrorComponent, canActivate: reservationsGuard }
 ]},
 { path: 'event/:eventShortName/ticket/:ticketId/view', component: ViewTicketComponent, canActivate: [EventGuard, LanguageGuard] }
 </pre>

 */
@GetMapping({
    "/",
    "/event/{eventShortName}",
    "/event/{eventShortName}/reservation/{reservationId}/book",
    "/event/{eventShortName}/reservation/{reservationId}/overview",
    "/event/{eventShortName}/reservation/{reservationId}/waitingPayment",
    "/event/{eventShortName}/reservation/{reservationId}/waiting-payment",
    "/event/{eventShortName}/reservation/{reservationId}/deferred-payment",
    "/event/{eventShortName}/reservation/{reservationId}/processing-payment",
    "/event/{eventShortName}/reservation/{reservationId}/success",
    "/event/{eventShortName}/reservation/{reservationId}/not-found",
    "/event/{eventShortName}/reservation/{reservationId}/error",
    "/event/{eventShortName}/ticket/{ticketId}/view",
    "/event/{eventShortName}/ticket/{ticketId}/update"
})
public void replyToIndex(@PathVariable(value = "eventShortName", required = false) String eventShortName,
                         @RequestHeader(value = "User-Agent", required = false) String userAgent,
                         @RequestParam(value = "lang", required = false) String lang,
                         ServletWebRequest request,
                         HttpServletResponse response,
                         HttpSession session) throws IOException {

    response.setContentType(TEXT_HTML_CHARSET_UTF_8);
    response.setCharacterEncoding(UTF_8);
    var nonce = addCspHeader(response);

    if (eventShortName != null && RequestUtils.isSocialMediaShareUA(userAgent) && eventRepository.existsByShortName(eventShortName)) {
        try (var os = response.getOutputStream(); var osw = new OutputStreamWriter(os, StandardCharsets.UTF_8)) {
            var res = getOpenGraphPage((Document) OPEN_GRAPH_PAGE.cloneNode(true), eventShortName, request, lang);
            JFiveParse.serialize(res, osw);
        }
    } else {
        try (var os = response.getOutputStream(); var osw = new OutputStreamWriter(os, StandardCharsets.UTF_8)) {
            var baseCustomCss = configurationManager.getForSystem(BASE_CUSTOM_CSS).getValueOrDefault(null);
            var idx = INDEX_PAGE.cloneNode(true);
            idx.getElementsByTagName("script").forEach(element -> element.setAttribute("nonce", nonce));
            var head = idx.getElementsByTagName("head").get(0);
            head.appendChild(buildScripTag(Json.toJson(configurationManager.getInfo(session)), "application/json", "preload-info", null));
            head.appendChild(buildScripTag(Json.toJson(messageSourceManager.getBundleAsMap("alfio.i18n.public", true, "en")), "application/json", "preload-bundle", "en"));
            if (baseCustomCss != null) {
                var style = new Element("style");
                style.setAttribute("type", "text/css");
                style.appendChild(new Text(baseCustomCss));
                head.appendChild(style);
            }
            if (eventShortName != null) {
                eventLoader.loadEventInfo(eventShortName, session).ifPresent(ev -> {
                    head.appendChild(buildScripTag(Json.toJson(ev), "application/json", "preload-event", eventShortName));
                });
            }
            JFiveParse.serialize(idx, osw);
        }
    }
}
 
Example 19
Source File: HCSMAPISamplerResult.java    From hedera-mirror-node with Apache License 2.0 4 votes vote down vote up
@Override
String getMessage(MirrorConsensusTopicResponse response) {
    return new String(response.message, StandardCharsets.UTF_8);
}
 
Example 20
Source File: ZipFile.java    From hottub with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Opens a new <code>ZipFile</code> to read from the specified
 * <code>File</code> object in the specified mode.  The mode argument
 * must be either <tt>OPEN_READ</tt> or <tt>OPEN_READ | OPEN_DELETE</tt>.
 *
 * <p>First, if there is a security manager, its <code>checkRead</code>
 * method is called with the <code>name</code> argument as its argument to
 * ensure the read is allowed.
 *
 * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
 * decode the entry names and comments
 *
 * @param file the ZIP file to be opened for reading
 * @param mode the mode in which the file is to be opened
 * @throws ZipException if a ZIP format error has occurred
 * @throws IOException if an I/O error has occurred
 * @throws SecurityException if a security manager exists and
 *         its <code>checkRead</code> method
 *         doesn't allow read access to the file,
 *         or its <code>checkDelete</code> method doesn't allow deleting
 *         the file when the <tt>OPEN_DELETE</tt> flag is set.
 * @throws IllegalArgumentException if the <tt>mode</tt> argument is invalid
 * @see SecurityManager#checkRead(java.lang.String)
 * @since 1.3
 */
public ZipFile(File file, int mode) throws IOException {
    this(file, mode, StandardCharsets.UTF_8);
}