下面列出了org.apache.commons.lang3.StringUtils#left ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(String[] args) {
// Read the specified file from arg[0]
String filename = (args.length > 0) ? args[0] : StringUtils.EMPTY;
if (StringUtils.isEmpty(filename)) {
throw new IllegalArgumentException("Bad syntax. Usage:\n\t" + SqlGenerator.class.getSimpleName() + " filename");
}
File inputFile = new File(filename);
try (CSVReader csvReader = new CSVReader(new BufferedReader(new FileReader(inputFile)))) {
// Read the file and generate SQL from it
String outputDirectory = StringUtils.left(inputFile.getPath(),
StringUtils.lastIndexOf(inputFile.getPath(), File.separatorChar));
SqlGenerator sqlGenerator = new SqlGenerator(outputDirectory);
sqlGenerator.processFile(csvReader);
csvReader.close();
log.info("Done.");
} catch (IOException e) {
log.error("IOException occurred while reading the input file => " + inputFile, e);
}
}
public static void main(String[] args) {
// Read the specified file from arg[0]
String filename = (args.length > 0) ? args[0] : StringUtils.EMPTY;
String year = (args.length > 1) ? args[1] : StringUtils.EMPTY;
if (StringUtils.isEmpty(filename) || StringUtils.isEmpty(year)) {
throw new IllegalArgumentException("Bad syntax. Usage:\n\t" + SqlGenerator.class.getSimpleName() + " filename");
}
File inputFile = new File(filename);
try (CSVReader csvReader = new CSVReader(new BufferedReader(new FileReader(inputFile)))) {
// Read the file and generate SQL from it
String outputDirectory = StringUtils.left(inputFile.getPath(),
StringUtils.lastIndexOf(inputFile.getPath(), File.separatorChar));
TournamentParticipantSqlGenerator sqlGenerator = new TournamentParticipantSqlGenerator(outputDirectory, year);
sqlGenerator.processFile(csvReader);
csvReader.close();
log.info("Done.");
} catch (IOException e) {
log.error("IOException occurred while reading the input file => " + inputFile, e);
}
}
/**
* Add a numerically increasing counter onto the and of a random string.
* Incremented counter should be thread safe.
*
* @param column {@link org.apache.phoenix.pherf.configuration.Column}
* @return {@link org.apache.phoenix.pherf.rules.DataValue}
*/
private DataValue getSequentialVarcharDataValue(Column column) {
DataValue data = null;
long inc = COUNTER.getAndIncrement();
String strInc = String.valueOf(inc);
int paddedLength = column.getLengthExcludingPrefix();
String strInc1 = StringUtils.leftPad(strInc, paddedLength, "0");
String strInc2 = StringUtils.right(strInc1, column.getLengthExcludingPrefix());
String varchar = (column.getPrefix() != null) ? column.getPrefix() + strInc2:
strInc2;
// Truncate string back down if it exceeds length
varchar = StringUtils.left(varchar,column.getLength());
data = new DataValue(column.getType(), varchar);
return data;
}
/**
* Generates a password based on a random UUID. Provider-specific limitations, which are needed
* for proper admin / root password generation:
*
* <ul>
* <li>AWS: password may be up to 30 characters</li>
* <li>Azure: password must be 8 - 128 characters, with chars from three of:
* uppercase, lowercase, digits, and non-alphanumeric</li>
* </ul>
*
* If no cloud platform is passed, then this method follows arbitrary rules to create a
* password.
*
* @param cloudPlatform cloud provider whose rules must be followed, if known
* @return random password
*/
public String generatePassword(Optional<CloudPlatform> cloudPlatform) {
if (cloudPlatform.isPresent()) {
switch (cloudPlatform.get()) {
case AWS:
case MOCK:
return uuidGeneratorService.uuidVariableParts(AWS_MAX_LENGTH);
case AZURE:
String candidatePassword;
do {
candidatePassword = StringUtils.left(uuidGeneratorService.randomUuid(), AZURE_MAX_LENGTH);
} while (!passesAzureCharacterRules(candidatePassword));
return candidatePassword;
default:
throw new UnsupportedOperationException("Password generation for " + cloudPlatform.get()
+ " not yet implemented");
}
} else {
return uuidGeneratorService.randomUuid();
}
}
/**
* 【中文姓名】只显示第一个汉字,其他隐藏为2个星号
* <p>
* DesensitizedUtil.password("李六子"); = "李**"
* </p>
*/
public static String chineseName(String fullName) {
if (StringUtils.isBlank(fullName)) {
return "";
}
String name = StringUtils.left(fullName, 1);
return StringUtils.rightPad(name, StringUtils.length(fullName), "*");
}
/**
* [中文姓名] 只显示第一个汉字,其他隐藏为2个星号<例子:李**>
*/
public static String chineseName(final String fullName) {
if (StringUtils.isBlank(fullName)) {
return "";
}
final String name = StringUtils.left(fullName, 1);
return StringUtils.rightPad(name, StringUtils.length(fullName), "*");
}
/**
* 【中文姓名】只显示第一个汉字,其他隐藏为2个星号
* <p>
* DesensitizedUtil.password("李六子"); = "李**"
* </p>
*/
public static String chineseName(String fullName) {
if (StringUtils.isBlank(fullName)) {
return "";
}
String name = StringUtils.left(fullName, 1);
return StringUtils.rightPad(name, StringUtils.length(fullName), "*");
}
/**
* Desensitizes the plain text.
*
* @param plainText plain text must not be null
* @param leftSize left size
* @param rightSize right size
* @return desensitization
*/
public static String desensitize(@NonNull String plainText, int leftSize, int rightSize) {
Assert.hasText(plainText, "Plain text must not be blank");
if (leftSize < 0) {
leftSize = 0;
}
if (leftSize > plainText.length()) {
leftSize = plainText.length();
}
if (rightSize < 0) {
rightSize = 0;
}
if (rightSize > plainText.length()) {
rightSize = plainText.length();
}
if (plainText.length() < leftSize + rightSize) {
rightSize = plainText.length() - leftSize;
}
int remainSize = plainText.length() - rightSize - leftSize;
String left = StringUtils.left(plainText, leftSize);
String right = StringUtils.right(plainText, rightSize);
return StringUtils.rightPad(left, remainSize + leftSize, '*') + right;
}
@SuppressWarnings("Duplicates")
public static String printString(String value, int maxLength) {
value = StringUtils.trimToNull(value);
if (maxLength < 1)
return value;
else if (maxLength < 4)
return StringUtils.left(value, maxLength);
else
return StringUtils.abbreviate(value, maxLength);
}
public static String printString(String value, int maxLength) {
value = StringUtils.trimToNull(value);
//noinspection Duplicates
if (maxLength < 1)
return value;
else if (maxLength < 4)
return StringUtils.left(value, maxLength);
else
return StringUtils.abbreviate(value, maxLength);
}
private DataValue getRandomDataValue(Column column) {
String varchar = RandomStringUtils.randomAlphanumeric(column.getLength());
varchar = (column.getPrefix() != null) ? column.getPrefix() + varchar : varchar;
// Truncate string back down if it exceeds length
varchar = StringUtils.left(varchar, column.getLength());
return new DataValue(column.getType(), varchar);
}
private String keyToString(byte[] rowKey) {
if (rowKey == null) {
return "null";
}
String str = BytesType.instance.getString(ByteBuffer.wrap(rowKey));
return StringUtils.left(str, 32);
}
private void readLog(long lastTimeFileSize, DataOutputStream dos) throws Exception{
try {
File logFile = new File(Config.base(), "logs/" + DateTools.format(new Date(), "yyyy_MM_dd") + ".out.log");
if(logFile.exists()){
List<Map<String, String>> list = new ArrayList<>();
try(RandomAccessFile randomFile = new RandomAccessFile(logFile,"r")) {
long curFileSize = randomFile.length();
if (lastTimeFileSize <= 0 || lastTimeFileSize > curFileSize) {
lastTimeFileSize = (curFileSize > LOG_MAX_READ_SIZE) ? (curFileSize - LOG_MAX_READ_SIZE) : 0;
}
randomFile.seek(lastTimeFileSize);
int curReadSize = 0;
String tmp = "";
String curTime = "";
while ((tmp = randomFile.readLine()) != null) {
byte[] bytes = tmp.getBytes("ISO8859-1");
curReadSize = curReadSize + bytes.length + 1;
String lineStr = new String(bytes);
String time = curTime;
String logLevel = "";
if (lineStr.length() > 23) {
time = StringUtils.left(lineStr, 19);
if (DateTools.isDateTime(time)) {
time = StringUtils.left(lineStr, 23);
curTime = time;
if(lineStr.length() > 29){
logLevel = StringUtils.right(StringUtils.left(lineStr, 29),5).trim();
}
} else {
if (StringUtils.isEmpty(curTime)) {
time = "2020-01-01 00:00:01.001";
} else {
time = curTime;
}
}
} else {
if (StringUtils.isEmpty(curTime)) {
continue;
} else {
time = curTime;
}
}
Map<String, String> map = new HashMap<>();
map.put("logTime",time+"#"+Config.node());
map.put("node", Config.node());
map.put("logLevel", logLevel);
map.put("lineLog", lineStr);
list.add(map);
if (curReadSize > LOG_MAX_READ_SIZE){
break;
}
}
if(curReadSize>0) {
lastTimeFileSize = lastTimeFileSize + curReadSize - 1;
}
}
dos.writeUTF(XGsonBuilder.toJson(list));
dos.flush();
dos.writeLong(lastTimeFileSize);
dos.flush();
return;
}
} catch (Exception e) {
logger.print("readLog error:{}", e.getMessage());
}
dos.writeUTF("failure");
dos.flush();
}
public static void main(String[] args) throws Exception{
//File logFile = new File(Config.base(), "logs/" + DateTools.format(new Date(), "yyyy_MM_dd") + ".out.log");
File logFile = new File("/Users/chengjian/Desktop/temp/temp/2020_03_12.out.log");
RandomAccessFile randomFile = new RandomAccessFile(logFile,"r");
long lastTimeFileSize = randomFile.length()-10*1024;
long tempSize = lastTimeFileSize;
randomFile.seek(lastTimeFileSize);
String tmp = "";
String curTime = "";
while( (tmp = randomFile.readLine())!= null) {
byte[] bytes = tmp.getBytes("ISO8859-1");
String lineStr = new String(bytes);
tempSize = tempSize + bytes.length+1;
String time = curTime;
if(lineStr.length()>23){
time = StringUtils.left(lineStr, 19);
if(DateTools.isDateTime(time)){
time = StringUtils.left(lineStr, 23);
curTime = time;
//System.out.println(lineStr);
}else{
if(StringUtils.isEmpty(curTime)){
continue;
}else {
time = curTime;
//System.out.println(lineStr);
}
}
}else{
if(StringUtils.isEmpty(curTime)){
continue;
}else{
time = curTime;
//System.out.println(lineStr);
}
}
}
lastTimeFileSize = randomFile.length();
tempSize = tempSize - 1;
System.out.println(lastTimeFileSize);
System.out.println(tempSize);
}
private static String truncateToMaxLength(String body) {
return StringUtils.left(body, MAX_LENGTH);
}
/**
* 保留前面几位
* <p>
* DesensitizedUtil.left("张虎子",1); = "张**"
* DesensitizedUtil.left("17667198751",3); = "176********"
* </p>
* @param str fullName
* @param index index
* @return String
*/
public static String left(String str,int index) {
if (StringUtils.isBlank(str)) {
return "";
}
String name = StringUtils.left(str, index);
return StringUtils.rightPad(name, StringUtils.length(str), "*");
}
/**
* 保留前面几位
* <p>
* DesensitizedUtil.left("张虎子",1); = "张**"
* DesensitizedUtil.left("17667198751",3); = "176********"
* </p>
* @param str fullName
* @param index index
* @return String
*/
public static String left(String str,int index) {
if (StringUtils.isBlank(str)) {
return "";
}
String name = StringUtils.left(str, index);
return StringUtils.rightPad(name, StringUtils.length(str), "*");
}
/**
* Truncates the description field to a configurable value thereby producing a 'short description'
*
* @param description the specified description
* @param shortDescMaxLength the short description maximum length
*
* @return truncated (short) description
*/
public static String getShortDescription(String description, Integer shortDescMaxLength)
{
// Parse out only html tags, truncate and return
// Do a partial HTML parse just in case there are some elements that don't have ending tags or the like
String toParse = description != null ? description : "";
return StringUtils.left(stripHtml(toParse), shortDescMaxLength);
}