List of usage examples for java.lang Character isDigit
public static boolean isDigit(int codePoint)
From source file:RandomStringUtils.java
/** * Creates a random string based on a variety of options, using * supplied source of randomness./*w w w . j av a2 s. co m*/ * * If start and end are both <code>0</code>, start and end are set * to <code>' '</code> and <code>'z'</code>, the ASCII printable * characters, will be used, unless letters and numbers are both * <code>false</code>, in which case, start and end are set to * <code>0</code> and <code>Integer.MAX_VALUE</code>. * * If set is not <code>null</code>, characters between start and * end are chosen. * * This method accepts a user-supplied {@link Random} * instance to use as a source of randomness. By seeding a single * {@link Random} instance with a fixed seed and using it for each call, * the same random sequence of strings can be generated repeatedly * and predictably. * * @param count the length of random string to create * @param start the position in set of chars to start at * @param end the position in set of chars to end before * @param letters only allow letters? * @param numbers only allow numbers? * @param chars the set of chars to choose randoms from. * If <code>null</code>, then it will use the set of all chars. * @param random a source of randomness. * @return the random string * @throws ArrayIndexOutOfBoundsException if there are not * <code>(end - start) + 1</code> characters in the set array. * @throws IllegalArgumentException if <code>count</code> < 0. * @since 2.0 */ public static String random(int count, int start, int end, boolean letters, boolean numbers, char[] chars, Random random) { if (count == 0) { return ""; } else if (count < 0) { throw new IllegalArgumentException("Requested random string length " + count + " is less than 0."); } if ((start == 0) && (end == 0)) { end = 'z' + 1; start = ' '; if (!letters && !numbers) { start = 0; end = Integer.MAX_VALUE; } } char[] buffer = new char[count]; int gap = end - start; while (count-- != 0) { char ch; if (chars == null) { ch = (char) (random.nextInt(gap) + start); } else { ch = chars[random.nextInt(gap) + start]; } if ((letters && Character.isLetter(ch)) || (numbers && Character.isDigit(ch)) || (!letters && !numbers)) { if (ch >= 56320 && ch <= 57343) { if (count == 0) { count++; } else { // low surrogate, insert high surrogate after putting it in buffer[count] = ch; count--; buffer[count] = (char) (55296 + random.nextInt(128)); } } else if (ch >= 55296 && ch <= 56191) { if (count == 0) { count++; } else { // high surrogate, insert low surrogate before putting it in buffer[count] = (char) (56320 + random.nextInt(128)); count--; buffer[count] = ch; } } else if (ch >= 56192 && ch <= 56319) { // private high surrogate, no effing clue, so skip it count++; } else { buffer[count] = ch; } } else { count++; } } return new String(buffer); }
From source file:com.dgtlrepublic.anitomyj.ParserNumber.java
/** * Attempts to find an episode/season inside a {@code word}/ * * @param word the word// w w w.j a v a 2s . com * @param token the token * @return true if the word was matched to an episode/season number */ public boolean matchEpisodePatterns(String word, Token token) { if (StringHelper.isNumericString(word)) return false; word = StringHelper.trimAny(word, " -"); boolean numericFront = Character.isDigit(word.charAt(0)); boolean numericBack = Character.isDigit(word.charAt(word.length() - 1)); // e.g. "01v2" if (numericFront && numericBack) if (matchSingleEpisodePattern(word, token)) return true; // e.g. "01-02", "03-05v2" if (numericFront && numericBack) if (matchMultiEpisodePattern(word, token)) return true; // e.g. "2x01", "S01E03", "S01-02xE001-150" if (numericBack) if (matchSeasonAndEpisodePattern(word, token)) return true; // e.g. "ED1", "OP4a", "OVA2" if (!numericFront) if (matchTypeAndEpisodePattern(word, token)) return true; // e.g. "07.5" if (numericFront && numericBack) if (matchFractionalEpisodePattern(word, token)) return true; // e.g. "4a", "111C" if (numericFront && !numericBack) if (matchPartialEpisodePattern(word, token)) return true; // e.g. "#01", "#02-03v2" if (numericBack) if (matchNumberSignPattern(word, token)) return true; // U+8A71 is used as counter for stories, episodes of TV series, etc. if (numericFront) if (matchJapaneseCounterPattern(word, token)) return true; return false; }
From source file:com.krawler.portal.util.GetterUtil.java
private static String _trim(String value) { if (value != null) { value = value.trim();//from w w w. ja v a2 s . c o m StringBuilder sb = new StringBuilder(); char[] charArray = value.toCharArray(); for (int i = 0; i < charArray.length; i++) { if ((Character.isDigit(charArray[i])) || ((charArray[i] == CharPool.DASH) && (i == 0)) || (charArray[i] == CharPool.PERIOD) || (charArray[i] == CharPool.UPPER_CASE_E) || (charArray[i] == CharPool.LOWER_CASE_E)) { sb.append(charArray[i]); } } value = sb.toString(); } return value; }
From source file:edu.stanford.muse.index.IndexUtils.java
/** replaces all tokens in the given text that are not in any of the entities in the given doc. * all other tokens are replaced with REDACTION_CHAR. * token is defined as a consecutive sequence of letters or digits * Note: all other characters (incl. punctuation, special symbols) are blindly copied through * anything not captured in a token is considered non-sensitive and is passed through *//*from w w w. j av a 2 s. c o m*/ public static String retainOnlyNames(String text, org.apache.lucene.document.Document doc) { StringBuilder result = new StringBuilder(); Set<String> allowedTokens = new LinkedHashSet<>(); // assemble all the allowed tokens (lower cased) from these 3 types of entities { List<String> allEntities = Arrays.asList(Archive.getAllNamesInLuceneDoc(doc, true)).stream() .map(Span::getText).collect(Collectors.toList()); for (String e : allEntities) allowedTokens.addAll(Util.tokenize(e.toLowerCase())); // names may sometimes still have punctuation; strip it. e.g. a name like "Rep. Duncan" should lead to the tokens "rep" and "duncan" allowedTokens = allowedTokens.stream().map(s -> Util.stripPunctuation(s)).collect(Collectors.toSet()); } final char REDACTION_CHAR = '.'; int idx = 0; boolean previousTokenAllowed = false; outer: while (true) { StringBuilder token = new StringBuilder(); // go through all the chars one by one, either passing them through or assembling them in a token that can be looked up in allowedTokens { // skip until start of next token, passing through chars to result // the letter pointed to by idx has not yet been processed while (true) { if (idx >= text.length()) break outer; char ch = text.charAt(idx++); if (Character.isLetter(ch) || Character.isDigit(ch)) { // if other chars are judged sensitive in the future, this condition should be updated token.append(ch); break; } else result.append(ch); } } Character ch; { // now, idx is just past the start of a token (with the first letter stored in token), // keep reading letters until we find a non-letter, adding it to the token // the letter pointed to by idx has not yet been processed while (true) { ch = null; if (idx >= text.length()) break; // only break out of inner loop here, not the outer. this might be the last token, and token may have some residual content, so it has to be processed ch = text.charAt(idx++); if (!Character.isLetter(ch) && !Character.isDigit(ch)) break; token.append(ch); } } // ch contains the first char beyond the token (if it is not null). If it is null, it means we have reached the end of the string // look up the token and allow it only if allowedTokens contains it // use lower case token for comparison, but when appending to result, use the original string with the original case // worried about "A" grade, we should disallow it although it could easily be a token in a name somewhere String lowerCaseToken = token.toString().toLowerCase(); // ctoken = canonicalized token boolean allowToken = allowedTokens.contains(lowerCaseToken); // however, if this token is a stop word, only allow if previous token was allowed because we don't want to start from a stop word. // note: this will still allow the stop word if it is at the beginning of a sentence, and the prev. sentence ended in an allowed token if (allowToken && DictUtils.isJoinWord(lowerCaseToken)) allowToken = previousTokenAllowed; if (allowToken) result.append(token); else for (int j = 0; j < token.length(); j++) result.append(REDACTION_CHAR); previousTokenAllowed = allowToken; if (ch != null) result.append(ch); } return result.toString(); }
From source file:com.microsoft.windowsazure.management.mediaservices.AccountOperationsImpl.java
/** * The Create Media Services Account operation creates a new media services * account in Windows Azure. (see//from w ww . ja v a2 s . c o m * http://msdn.microsoft.com/en-us/library/windowsazure/dn194267.aspx for * more information) * * @param parameters Required. Parameters supplied to the Create Media * Services Account operation. * @throws ParserConfigurationException Thrown if there was an error * configuring the parser for the response body. * @throws SAXException Thrown if there was an error parsing the response * body. * @throws TransformerException Thrown if there was an error creating the * DOM transformer. * @throws IOException Signals that an I/O exception of some sort has * occurred. This class is the general class of exceptions produced by * failed or interrupted I/O operations. * @throws ServiceException Thrown if an unexpected response is found. * @return The Create Media Services Account operation response. */ @Override public MediaServicesAccountCreateResponse create(MediaServicesAccountCreateParameters parameters) throws ParserConfigurationException, SAXException, TransformerException, IOException, ServiceException { // Validate if (parameters == null) { throw new NullPointerException("parameters"); } if (parameters.getAccountName() == null) { throw new NullPointerException("parameters.AccountName"); } if (parameters.getAccountName().length() < 3) { throw new IllegalArgumentException("parameters.AccountName"); } if (parameters.getAccountName().length() > 24) { throw new IllegalArgumentException("parameters.AccountName"); } if (parameters.getBlobStorageEndpointUri() == null) { throw new NullPointerException("parameters.BlobStorageEndpointUri"); } if (parameters.getRegion() == null) { throw new NullPointerException("parameters.Region"); } if (parameters.getRegion().length() < 3) { throw new IllegalArgumentException("parameters.Region"); } if (parameters.getRegion().length() > 256) { throw new IllegalArgumentException("parameters.Region"); } if (parameters.getStorageAccountKey() == null) { throw new NullPointerException("parameters.StorageAccountKey"); } if (parameters.getStorageAccountKey().length() < 14) { throw new IllegalArgumentException("parameters.StorageAccountKey"); } if (parameters.getStorageAccountKey().length() > 256) { throw new IllegalArgumentException("parameters.StorageAccountKey"); } if (parameters.getStorageAccountName() == null) { throw new NullPointerException("parameters.StorageAccountName"); } if (parameters.getStorageAccountName().length() < 3) { throw new IllegalArgumentException("parameters.StorageAccountName"); } if (parameters.getStorageAccountName().length() > 24) { throw new IllegalArgumentException("parameters.StorageAccountName"); } for (char storageAccountNameChar : parameters.getStorageAccountName().toCharArray()) { if (Character.isLowerCase(storageAccountNameChar) == false && Character.isDigit(storageAccountNameChar) == false) { throw new IllegalArgumentException("parameters.StorageAccountName"); } } // Tracing boolean shouldTrace = CloudTracing.getIsEnabled(); String invocationId = null; if (shouldTrace) { invocationId = Long.toString(CloudTracing.getNextInvocationId()); HashMap<String, Object> tracingParameters = new HashMap<String, Object>(); tracingParameters.put("parameters", parameters); CloudTracing.enter(invocationId, this, "createAsync", tracingParameters); } // Construct URL String url = ""; url = url + "/"; if (this.getClient().getCredentials().getSubscriptionId() != null) { url = url + URLEncoder.encode(this.getClient().getCredentials().getSubscriptionId(), "UTF-8"); } url = url + "/services/mediaservices/Accounts"; String baseUrl = this.getClient().getBaseUri().toString(); // Trim '/' character from the end of baseUrl and beginning of url. if (baseUrl.charAt(baseUrl.length() - 1) == '/') { baseUrl = baseUrl.substring(0, (baseUrl.length() - 1) + 0); } if (url.charAt(0) == '/') { url = url.substring(1); } url = baseUrl + "/" + url; url = url.replace(" ", "%20"); // Create HTTP transport objects HttpPost httpRequest = new HttpPost(url); // Set Headers httpRequest.setHeader("Content-Type", "application/xml"); httpRequest.setHeader("x-ms-version", "2011-10-01"); // Serialize Request String requestContent = null; DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document requestDoc = documentBuilder.newDocument(); Element accountCreationRequestElement = requestDoc.createElementNS( "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models", "AccountCreationRequest"); requestDoc.appendChild(accountCreationRequestElement); Element accountNameElement = requestDoc.createElementNS( "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models", "AccountName"); accountNameElement.appendChild(requestDoc.createTextNode(parameters.getAccountName())); accountCreationRequestElement.appendChild(accountNameElement); Element blobStorageEndpointUriElement = requestDoc.createElementNS( "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models", "BlobStorageEndpointUri"); blobStorageEndpointUriElement .appendChild(requestDoc.createTextNode(parameters.getBlobStorageEndpointUri().toString())); accountCreationRequestElement.appendChild(blobStorageEndpointUriElement); Element regionElement = requestDoc.createElementNS( "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models", "Region"); regionElement.appendChild(requestDoc.createTextNode(parameters.getRegion())); accountCreationRequestElement.appendChild(regionElement); Element storageAccountKeyElement = requestDoc.createElementNS( "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models", "StorageAccountKey"); storageAccountKeyElement.appendChild(requestDoc.createTextNode(parameters.getStorageAccountKey())); accountCreationRequestElement.appendChild(storageAccountKeyElement); Element storageAccountNameElement = requestDoc.createElementNS( "http://schemas.datacontract.org/2004/07/Microsoft.Cloud.Media.Management.ResourceProvider.Models", "StorageAccountName"); storageAccountNameElement.appendChild(requestDoc.createTextNode(parameters.getStorageAccountName())); accountCreationRequestElement.appendChild(storageAccountNameElement); DOMSource domSource = new DOMSource(requestDoc); StringWriter stringWriter = new StringWriter(); StreamResult streamResult = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.transform(domSource, streamResult); requestContent = stringWriter.toString(); StringEntity entity = new StringEntity(requestContent); httpRequest.setEntity(entity); httpRequest.setHeader("Content-Type", "application/xml"); // Send Request HttpResponse httpResponse = null; try { if (shouldTrace) { CloudTracing.sendRequest(invocationId, httpRequest); } httpResponse = this.getClient().getHttpClient().execute(httpRequest); if (shouldTrace) { CloudTracing.receiveResponse(invocationId, httpResponse); } int statusCode = httpResponse.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_CREATED) { ServiceException ex = ServiceException.createFromJson(httpRequest, requestContent, httpResponse, httpResponse.getEntity()); if (shouldTrace) { CloudTracing.error(invocationId, ex); } throw ex; } // Create Result MediaServicesAccountCreateResponse result = null; // Deserialize Response if (statusCode == HttpStatus.SC_CREATED) { InputStream responseContent = httpResponse.getEntity().getContent(); result = new MediaServicesAccountCreateResponse(); ObjectMapper objectMapper = new ObjectMapper(); JsonNode responseDoc = null; String responseDocContent = IOUtils.toString(responseContent); if (responseDocContent == null == false && responseDocContent.length() > 0) { responseDoc = objectMapper.readTree(responseDocContent); } if (responseDoc != null && responseDoc instanceof NullNode == false) { MediaServicesCreatedAccount accountInstance = new MediaServicesCreatedAccount(); result.setAccount(accountInstance); JsonNode accountIdValue = responseDoc.get("AccountId"); if (accountIdValue != null && accountIdValue instanceof NullNode == false) { String accountIdInstance; accountIdInstance = accountIdValue.getTextValue(); accountInstance.setAccountId(accountIdInstance); } JsonNode accountNameValue = responseDoc.get("AccountName"); if (accountNameValue != null && accountNameValue instanceof NullNode == false) { String accountNameInstance; accountNameInstance = accountNameValue.getTextValue(); accountInstance.setAccountName(accountNameInstance); } JsonNode subscriptionValue = responseDoc.get("Subscription"); if (subscriptionValue != null && subscriptionValue instanceof NullNode == false) { String subscriptionInstance; subscriptionInstance = subscriptionValue.getTextValue(); accountInstance.setSubscriptionId(subscriptionInstance); } } } result.setStatusCode(statusCode); if (httpResponse.getHeaders("x-ms-request-id").length > 0) { result.setRequestId(httpResponse.getFirstHeader("x-ms-request-id").getValue()); } if (shouldTrace) { CloudTracing.exit(invocationId, result); } return result; } finally { if (httpResponse != null && httpResponse.getEntity() != null) { httpResponse.getEntity().getContent().close(); } } }
From source file:DateParser.java
/** * Check if a given date is an iso8601 date. * /*from www . j av a2s .co m*/ * @param iso8601Date The date to be checked. * @return <code>true</code> if the date is an iso8601 date. * @throws InvalidDateException */ private Calendar getCalendar(String iso8601Date) throws InvalidDateException { // YYYY-MM-DDThh:mm:ss.sTZD StringTokenizer st = new StringTokenizer(iso8601Date, "-T:.+Z", true); if (!st.hasMoreTokens()) { throw new InvalidDateException("Empty Date"); } Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); calendar.clear(); try { // Year if (st.hasMoreTokens()) { int year = Integer.parseInt(st.nextToken()); calendar.set(Calendar.YEAR, year); } else { return calendar; } // Month if (checkValueAndNext(st, "-")) { int month = Integer.parseInt(st.nextToken()) - 1; calendar.set(Calendar.MONTH, month); } else { return calendar; } // Day if (checkValueAndNext(st, "-")) { int day = Integer.parseInt(st.nextToken()); calendar.set(Calendar.DAY_OF_MONTH, day); } else { return calendar; } // Hour if (checkValueAndNext(st, "T")) { int hour = Integer.parseInt(st.nextToken()); calendar.set(Calendar.HOUR_OF_DAY, hour); } else { calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar; } // Minutes if (checkValueAndNext(st, ":")) { int minutes = Integer.parseInt(st.nextToken()); calendar.set(Calendar.MINUTE, minutes); } else { calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar; } if (!st.hasMoreTokens()) { return calendar; } // Not mandatory now // Seconds String tok = st.nextToken(); if (tok.equals(":")) { // seconds if (st.hasMoreTokens()) { int secondes = Integer.parseInt(st.nextToken()); calendar.set(Calendar.SECOND, secondes); if (!st.hasMoreTokens()) { return calendar; } // decimal fraction of a second tok = st.nextToken(); if (tok.equals(".")) { String nt = st.nextToken(); while (nt.length() < 3) { nt += "0"; } if (nt.length() > 3) { // check the other part from the decimal fraction to be formed only from digits for (int i = 3; i < nt.length(); i++) { if (!Character.isDigit(nt.charAt(i))) { throw new InvalidDateException( "Invalid digit in the decimal fraction of a second: " + nt.charAt(i)); } } } nt = nt.substring(0, 3); //Cut trailing chars.. int millisec = Integer.parseInt(nt); calendar.set(Calendar.MILLISECOND, millisec); if (!st.hasMoreTokens()) { return calendar; } tok = st.nextToken(); } else { calendar.set(Calendar.MILLISECOND, 0); } } else { throw new InvalidDateException("No secondes specified"); } } else { calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); } // Time zone if (!tok.equals("Z")) { // UTC if (!(tok.equals("+") || tok.equals("-"))) { throw new InvalidDateException("only Z, + or - allowed"); } boolean plus = tok.equals("+"); if (!st.hasMoreTokens()) { throw new InvalidDateException("Missing hour field"); } int tzhour = Integer.parseInt(st.nextToken()); int tzmin = 0; if (checkValueAndNext(st, ":")) { tzmin = Integer.parseInt(st.nextToken()); } else { throw new InvalidDateException("Missing minute field"); } if (plus) { calendar.add(Calendar.HOUR, -tzhour); calendar.add(Calendar.MINUTE, -tzmin); } else { calendar.add(Calendar.HOUR, tzhour); calendar.add(Calendar.MINUTE, tzmin); } } else { if (st.hasMoreTokens()) { throw new InvalidDateException( "Unexpected field at the end of the date field: " + st.nextToken()); } } } catch (NumberFormatException ex) { throw new InvalidDateException("[" + ex.getMessage() + "] is not an integer"); } return calendar; }
From source file:herudi.controller.microMarketController.java
@FXML private void keyAreaLength(KeyEvent event) { char[] data = txtAreaLength.getText().toCharArray(); boolean valid = true; for (char c : data) { if (!Character.isDigit(c)) { valid = false;// ww w .j a v a2 s. c o m break; } } if (!valid) { config2.dialog(Alert.AlertType.ERROR, "Please, Fill With Number"); txtAreaLength.clear(); txtAreaLength.requestFocus(); } }
From source file:UTFEncodingUtil.java
/** * Decodes the given string using the encoding UTF-8. * * @param s the string that should be encoded. * @return the encoded string.//www . j av a 2 s .com */ public static String decodeUTF(final String s) { final StringBuffer sbuf = new StringBuffer(); final char[] chars = s.toCharArray(); final int l = chars.length; int sumb = 0; for (int i = 0, more = -1; i < l; i++) { /* Get next byte b from URL segment s */ final int ch = chars[i]; final int b; switch (ch) { case '%': final char lch = s.charAt(++i); final int hb = (Character.isDigit(lch) ? lch - '0' : 10 + Character.toLowerCase(lch) - 'a') & 0xF; final char hch = s.charAt(++i); final int lb = (Character.isDigit(hch) ? hch - '0' : 10 + Character.toLowerCase(hch) - 'a') & 0xF; b = (hb << 4) | lb; break; case '+': b = ' '; break; default: b = ch; } /* Decode byte b as UTF-8, sumb collects incomplete chars */ if ((b & 0xc0) == 0x80) { // 10xxxxxx (continuation byte) sumb = (sumb << 6) | (b & 0x3f); // Add 6 bits to sumb if (--more == 0) { sbuf.append((char) sumb); // Add char to sbuf } } else if ((b & 0x80) == 0x00) { // 0xxxxxxx (yields 7 bits) sbuf.append((char) b); // Store in sbuf } else if ((b & 0xe0) == 0xc0) { // 110xxxxx (yields 5 bits) sumb = b & 0x1f; more = 1; // Expect 1 more byte } else if ((b & 0xf0) == 0xe0) { // 1110xxxx (yields 4 bits) sumb = b & 0x0f; more = 2; // Expect 2 more bytes } else if ((b & 0xf8) == 0xf0) { // 11110xxx (yields 3 bits) sumb = b & 0x07; more = 3; // Expect 3 more bytes } else if ((b & 0xfc) == 0xf8) { // 111110xx (yields 2 bits) sumb = b & 0x03; more = 4; // Expect 4 more bytes } else /*if ((b & 0xfe) == 0xfc)*/ { // 1111110x (yields 1 bit) sumb = b & 0x01; more = 5; // Expect 5 more bytes } /* We don't test if the UTF-8 encoding is well-formed */ } return sbuf.toString(); }
From source file:com.microsoft.tfs.core.util.FileEncodingDetector.java
/** * Tests whether the given bytes declare an Adobe PDF file. * * See the PDF file spec at/*w ww . jav a 2 s . co m*/ * http://partners.adobe.com/public/developer/pdf/index_reference.html for * details on file structure. * * @param bytes * the bytes to test, where the byte at index 0 is the first byte of * the file. * @param size * the maximum number of bytes to evaluate in the given byte array. * @return true if the bytes denote a PDF file of any version, false if they * do not. */ private static boolean startsWithPDFHeader(final byte[] bytes, final int size) { /* * PDF files start with the header "%PDF-X.Y" where X is the major * version number and Y is the minor. See the PDF specification for * notes on this header and the version scheme. */ // The header is 8 bytes, so smaller files can't be PDF. if (size < 8) { return false; } // Make sure the first five bytes are an exact match. byte[] firstFive; try { firstFive = "%PDF-".getBytes("US-ASCII"); //$NON-NLS-1$ //$NON-NLS-2$ } catch (final UnsupportedEncodingException e) { /* * Should never happen as all JVMs are required to support US-ASCII. */ throw new RuntimeException(e); } for (int i = 0; i < firstFive.length; i++) { if (firstFive[i] != bytes[i]) { return false; } } // Finally, it should end with "X.Y". if (Character.isDigit((char) bytes[5]) && bytes[6] == '.' && Character.isDigit((char) bytes[7])) { return true; } return false; }
From source file:com.aurel.track.exchange.docx.exporter.PreprocessImage.java
private static Integer parseNumber(String imgSrc, String tag) { int workItemIndex = imgSrc.indexOf(tag); if (workItemIndex != -1) { String workItemString = imgSrc.substring(workItemIndex); StringBuilder stringBuilder = new StringBuilder(workItemString); stringBuilder.replace(0, tag.length(), ""); int i = 0; char charValue; StringBuilder numberString = new StringBuilder(); do {/*from www . j a va2 s.c o m*/ charValue = stringBuilder.charAt(i++); if (Character.isDigit(charValue)) { numberString.append(charValue); } } while (stringBuilder.length() > i && Character.isDigit(charValue)); return Integer.decode(numberString.toString()); } return null; }