Java Code Examples for com.jstarcraft.core.utility.StringUtility#CHARSET

The following examples show how to use com.jstarcraft.core.utility.StringUtility#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 File: DetectionPattern.java    From jstarcraft-nlp with Apache License 2.0 6 votes vote down vote up
public static final Map<String, DetectionPattern> loadPatterns(InputStream stream) {
    try (DataInputStream buffer = new DataInputStream(stream)) {
        Map<String, DetectionPattern> detections = new HashMap<>();
        byte[] data = new byte[buffer.available()];
        buffer.readFully(data);
        String json = new String(data, StringUtility.CHARSET);
        // 兼容\x转义ASCII字符
        json = StringUtility.unescapeJava(json);
        Type type = TypeUtility.parameterize(LinkedHashMap.class, String.class, String.class);
        LinkedHashMap<String, String> regulations = JsonUtility.string2Object(json, type);

        for (Entry<String, String> scriptTerm : regulations.entrySet()) {
            String script = scriptTerm.getKey();
            String regulation = scriptTerm.getValue();
            Pattern pattern = Pattern.compile(regulation, Pattern.MULTILINE);
            DetectionPattern detection = new DetectionPattern(script, pattern);
            detections.put(script, detection);
        }
        return Collections.unmodifiableMap(detections);
    } catch (Exception exception) {
        throw new IllegalArgumentException(exception);
    }
}
 
Example 2
Source File: FileMemorandum.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
public FileMemorandum(int cacheSize, File environmentDirectory, File memorandumDirectory) {
    this.cacheSize = cacheSize;
    this.environmentDirectory = environmentDirectory;
    this.memorandumDirectory = memorandumDirectory;
    this.markFile = new File(memorandumDirectory, MARK_FILE);
    try {
        if (markFile.createNewFile()) {
            // 标记文件不存在
            markNumber = -1L;
        } else {
            try (FileInputStream input = new FileInputStream(markFile); InputStreamReader reader = new InputStreamReader(input, StringUtility.CHARSET); BufferedReader buffer = new BufferedReader(reader)) {
                markNumber = Long.valueOf(buffer.readLine());
            }
        }
    } catch (Exception exception) {
        throw new BerkeleyMemorandumException(exception);
    }

}
 
Example 3
Source File: MovieDataConfigurer.java    From jstarcraft-example with Apache License 2.0 5 votes vote down vote up
@Bean("movieItems")
List<MovieItem> getItems(DataSpace movieDataSpace) throws Exception {
    File movieItemFile = new File("data/ml-100k/u.item");
    List<MovieItem> items = new LinkedList<>();

    QualityAttribute<Integer> itemAttribute = movieDataSpace.getQualityAttribute("item");
    try (InputStream stream = new FileInputStream(movieItemFile); InputStreamReader reader = new InputStreamReader(stream, StringUtility.CHARSET); BufferedReader buffer = new BufferedReader(reader)) {
        try (CSVParser parser = new CSVParser(buffer, CSVFormat.newFormat('|'))) {
            Iterator<CSVRecord> iterator = parser.iterator();
            while (iterator.hasNext()) {
                CSVRecord datas = iterator.next();
                // 物品标识
                int id = Integer.parseInt(datas.get(0));
                // 物品索引
                int index = itemAttribute.convertData(id);
                // 物品标题
                String title = datas.get(1);
                // 物品日期
                LocalDate date = StringUtility.isEmpty(datas.get(2)) ? LocalDate.of(1970, 1, 1) : LocalDate.parse(datas.get(2), formatter);
                MovieItem item = new MovieItem(index, title, date);
                items.add(item);
            }
        }
    }

    items = new ArrayList<>(items);
    return items;
}
 
Example 4
Source File: DetectionTrie.java    From jstarcraft-nlp with Apache License 2.0 5 votes vote down vote up
public static final Map<String, Set<DetectionTrie>> loadTries(InputStream stream) {
    try (DataInputStream buffer = new DataInputStream(stream)) {
        Map<String, Set<DetectionTrie>> detections = new HashMap<>();
        byte[] data = new byte[buffer.available()];
        buffer.readFully(data);
        String json = new String(data, StringUtility.CHARSET);
        // 兼容\x转义ASCII字符
        json = StringUtility.unescapeJava(json);
        Type type = TypeUtility.parameterize(LinkedHashMap.class, String.class, String.class);
        type = TypeUtility.parameterize(LinkedHashMap.class, String.class, LinkedHashMap.class);
        LinkedHashMap<String, LinkedHashMap<String, String>> dictionaries = JsonUtility.string2Object(json, type);

        for (Entry<String, LinkedHashMap<String, String>> scriptTerm : dictionaries.entrySet()) {
            Set<DetectionTrie> tries = new HashSet<>();
            String script = scriptTerm.getKey();
            LinkedHashMap<String, String> languages = scriptTerm.getValue();
            for (Entry<String, String> languageTerm : languages.entrySet()) {
                String language = languageTerm.getKey();
                String[] dictionary = languageTerm.getValue().split("\\|");
                int weight = 0;
                TreeMap<String, Integer> tree = new TreeMap<>();
                for (String word : dictionary) {
                    tree.put(word, weight++);
                }
                DoubleArrayTrie<Integer> trie = new DoubleArrayTrie<>(tree);
                DetectionTrie detection = new DetectionTrie(language, trie);
                tries.add(detection);
            }
            detections.put(script, Collections.unmodifiableSet(tries));
        }
        return Collections.unmodifiableMap(detections);
    } catch (Exception exception) {
        throw new IllegalArgumentException(exception);
    }
}
 
Example 5
Source File: StreamConverter.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
public int convert(DataModule module, InputStream stream) {
    try {
        try (InputStreamReader iterator = new InputStreamReader(stream, StringUtility.CHARSET); BufferedReader buffer = new BufferedReader(iterator)) {
            return parseData(module, buffer);
        }
    } catch (Exception exception) {
        // TODO 处理日志.
        throw new DataException(exception);
    }
}
 
Example 6
Source File: CsvFormatAdapter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public <E> Iterator<E> iterator(Class<E> clazz, InputStream stream) {
    try {
        List<E> list = new LinkedList<>();
        try (InputStreamReader reader = new InputStreamReader(stream, StringUtility.CHARSET); BufferedReader buffer = new BufferedReader(reader)) {
            for (String line = buffer.readLine(); line != null; line = buffer.readLine()) {
                E instance = CsvUtility.string2Object(line, clazz);
                list.add(instance);
            }
        }
        return list.iterator();
    } catch (Exception exception) {
        throw new StorageException("遍历CSV异常", exception);
    }
}
 
Example 7
Source File: CsvReader.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
public CsvReader(InputStream inputStream, CodecDefinition definition) {
    super(definition);
    InputStreamReader buffer = new InputStreamReader(inputStream, StringUtility.CHARSET);
    try (CSVParser input = new CSVParser(buffer, FORMAT)) {
        Iterator<CSVRecord> iterator = input.iterator();
        if (iterator.hasNext()) {
            CSVRecord values = iterator.next();
            this.inputStream = values.iterator();
        }
    } catch (Exception exception) {
        throw new RuntimeException(exception);
    }
}
 
Example 8
Source File: CsvWriter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
public CsvWriter(OutputStream outputStream, CodecDefinition definition) {
    super(definition);
    try {
        OutputStreamWriter buffer = new OutputStreamWriter(outputStream, StringUtility.CHARSET);
        this.outputStream = new CSVPrinter(buffer, FORMAT);
    } catch (Exception exception) {
        throw new RuntimeException(exception);
    }
}
 
Example 9
Source File: RockMqTestCase.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> messages, ConsumeConcurrentlyContext context) {
    for (MessageExt message : messages) {
        String body = new String(message.getBody(), StringUtility.CHARSET);
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "消费响应:msgId : " + message.getMsgId() + ",  msgBody : " + body);// 输出消息内容
        semaphore.release(1);
    }

    // 返回消费状态
    // CONSUME_SUCCESS 消费成功
    // RECONSUME_LATER 消费失败,需要重新消费
    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
 
Example 10
Source File: FileMemorandum.java    From jstarcraft-core with Apache License 2.0 4 votes vote down vote up
@Override
public void checkOut(Instant from, Instant to) {
    // 清空目标目录
    try {
        FileUtils.deleteDirectory(environmentDirectory);
    } catch (IOException ioException) {
    }

    environmentDirectory.mkdirs();

    // 按照日期排序的Map,保证新文件会覆盖旧文件
    final TreeMap<Instant, File> restoreDirectoryMap = new TreeMap<>();
    for (File dayDirectory : memorandumDirectory.listFiles()) {
        if (dayDirectory.isDirectory()) {
            for (File timeDirectory : dayDirectory.listFiles()) {
                if (timeDirectory.isDirectory()) {
                    Instant date = Instant.from(formatter.parse(dayDirectory.getName() + File.separator + timeDirectory.getName()));
                    if (!date.isBefore(from) && !date.isAfter(to)) {
                        restoreDirectoryMap.put(date, timeDirectory);
                    }
                }
            }
        }
    }

    if (restoreDirectoryMap.size() == 0) {
        throw new BerkeleyMemorandumException("不包含恢复目录");
    }

    final File lastBackUpDirectory = restoreDirectoryMap.lastEntry().getValue();
    final File listFile = new File(lastBackUpDirectory, MEMORANDUM_FILE);

    if (!listFile.exists()) {
        throw new BerkeleyMemorandumException("备忘文件不存在");
    }

    // 恢复的文件列表
    final Collection<String> names = new HashSet<String>();

    try (FileInputStream fileInputStream = new FileInputStream(listFile); InputStreamReader dataInputStream = new InputStreamReader(fileInputStream, StringUtility.CHARSET); BufferedReader bufferedReader = new BufferedReader(dataInputStream);) {
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            names.add(line);
        }

        // 恢复目标目录
        for (Entry<Instant, File> keyValue : restoreDirectoryMap.entrySet()) {
            final File restoreDirectory = keyValue.getValue();
            for (File fromFile : restoreDirectory.listFiles()) {
                if (names.contains(fromFile.getName()) && fromFile.isFile()) {
                    final File toFile = new File(environmentDirectory, fromFile.getName());
                    FileUtils.copyFile(fromFile, toFile);
                }
            }
        }
    } catch (Exception exception) {
        throw new BerkeleyMemorandumException("恢复文件失败", exception);
    }
}
 
Example 11
Source File: FileMemorandum.java    From jstarcraft-core with Apache License 2.0 4 votes vote down vote up
@Override
public void clean(Instant expire) {
    // 按照日期排序的Map,保证新文件会覆盖旧文件
    final TreeMap<Instant, File> cleanDirectoryMap = new TreeMap<>();

    for (File dayDirectory : memorandumDirectory.listFiles()) {
        if (dayDirectory.isDirectory()) {
            for (File timeDirectory : dayDirectory.listFiles()) {
                if (timeDirectory.isDirectory()) {
                    Instant date = Instant.from(formatter.parse(dayDirectory.getName() + File.separator + timeDirectory.getName()));
                    if (!date.isAfter(expire)) {
                        cleanDirectoryMap.put(date, timeDirectory);
                    }
                }
            }
        }
    }

    if (cleanDirectoryMap.size() == 0) {
        throw new BerkeleyMemorandumException("不包含清理目录");
    }

    final File lastBackUpDirectory = cleanDirectoryMap.lastEntry().getValue();
    final File listFile = new File(lastBackUpDirectory, MEMORANDUM_FILE);

    if (!listFile.exists()) {
        throw new BerkeleyMemorandumException("列表文件不存在");
    }

    // 保留的文件列表
    final Collection<String> saveList = new HashSet<String>();
    try (FileInputStream input = new FileInputStream(listFile); InputStreamReader reader = new InputStreamReader(input, StringUtility.CHARSET); BufferedReader buffer = new BufferedReader(reader);) {
        String line;
        while ((line = buffer.readLine()) != null) {
            saveList.add(line);
        }
    } catch (Exception exception) {
        throw new BerkeleyMemorandumException("清理文件失败", exception);
    }

    // 清理目标目录
    for (Entry<Instant, File> keyValue : cleanDirectoryMap.entrySet()) {
        final File cleanDirectory = keyValue.getValue();
        for (File cleanFile : cleanDirectory.listFiles()) {
            if (cleanFile.isFile()) {
                if (!listFile.equals(cleanFile) && !saveList.contains(cleanFile.getName())) {
                    cleanFile.delete();
                }
            }
        }
        if (cleanDirectory.list().length == 0) {
            cleanDirectory.delete();
        }
    }
}