List of usage examples for java.lang StringBuffer toString
@Override @HotSpotIntrinsicCandidate public synchronized String toString()
From source file:Main.java
public static void main(String args[]) { Pattern p = Pattern.compile("(another) (test)"); StringBuffer sb = new StringBuffer(); String candidateString = "This is another test."; String replacement = "$1 AAA $2"; Matcher matcher = p.matcher(candidateString); matcher.find();//from w w w . j av a 2 s . c o m matcher.appendReplacement(sb, replacement); String msg = sb.toString(); System.out.println(msg); }
From source file:MatcherAppendReplacementGroupExample.java
public static void main(String args[]) { Pattern p = Pattern.compile("(James) (Bond)"); StringBuffer sb = new StringBuffer(); String candidateString = "My name is Bond. James Bond."; String replacement = "$1 Waldo $2"; Matcher matcher = p.matcher(candidateString); matcher.find();/*w w w . ja v a 2 s . c o m*/ matcher.appendReplacement(sb, replacement); String msg = sb.toString(); System.out.println(msg); }
From source file:Main.java
public static void main(String[] argv) throws Exception { CharSequence inputStr = "ab12 cd efg34 123"; String patternStr = "([a-zA-Z]+[0-9]+)"; Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(inputStr); StringBuffer buf = new StringBuffer(); boolean found = false; while ((found = matcher.find())) { String replaceStr = matcher.group(); matcher.appendReplacement(buf, "found<" + replaceStr + ">"); }// w w w . ja va2 s. c o m matcher.appendTail(buf); String result = buf.toString(); System.out.println(result); }
From source file:com.gemini.httpclienttest.HttpClientTestMain.java
public static void main(String[] args) { //authenticate with the server URL url;// ww w .j av a2s . c o m try { url = new URL("http://198.11.209.34:5000/v2.0/tokens"); } catch (MalformedURLException ex) { System.out.printf("Invalid Endpoint - not a valid URL {}", "http://198.11.209.34:5000/v2.0"); return; } CloseableHttpClient httpclient = HttpClientBuilder.create().build(); try { HttpPost httpPost = new HttpPost(url.toString()); httpPost.setHeader("Content-Type", "application/json"); StringEntity strEntity = new StringEntity( "{\"auth\":{\"tenantName\":\"Gemini-network-prj\",\"passwordCredentials\":{\"username\":\"sri\",\"password\":\"srikumar12\"}}}"); httpPost.setEntity(strEntity); //System.out.println("Executing request " + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpPost); try { //get the response status code String respStatus = response.getStatusLine().getReasonPhrase(); //get the response body int bytes = response.getEntity().getContent().available(); InputStream body = response.getEntity().getContent(); BufferedReader in = new BufferedReader(new InputStreamReader(body)); String line; StringBuffer sbJSON = new StringBuffer(); while ((line = in.readLine()) != null) { sbJSON.append(line); } String json = sbJSON.toString(); EntityUtils.consume(response.getEntity()); GsonBuilder gsonBuilder = new GsonBuilder(); Object parsedJson = gsonBuilder.create().fromJson(json, Object.class); System.out.printf("Parsed json is of type %s\n\n", parsedJson.getClass().toString()); if (parsedJson instanceof com.google.gson.internal.LinkedTreeMap) { com.google.gson.internal.LinkedTreeMap treeJson = (com.google.gson.internal.LinkedTreeMap) parsedJson; if (treeJson.containsKey("id")) { String s = (String) treeJson.getOrDefault("id", "no key provided"); System.out.printf("\n\ntree contained id %s\n", s); } else { System.out.printf("\n\ntree does not contain key id\n"); } } } catch (IOException ex) { System.out.print(ex); } finally { response.close(); } } catch (IOException ex) { System.out.print(ex); } finally { //lots of exceptions, just the connection and exit try { httpclient.close(); } catch (IOException | NoSuchMethodError ex) { System.out.print(ex); return; } } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Document document = new Document(PageSize.A6); PdfWriter.getInstance(document, new FileOutputStream("2.pdf")); document.open();/*from w ww . j a va 2 s . com*/ document.add(new Paragraph("Hello World")); document.add(new Paragraph("Hello People")); document.close(); PdfReader reader = new PdfReader("2.pdf"); PdfDictionary page = reader.getPageN(1); PRIndirectReference objectReference = (PRIndirectReference) page.get(PdfName.CONTENTS); PRStream stream = (PRStream) PdfReader.getPdfObject(objectReference); byte[] streamBytes = PdfReader.getStreamBytes(stream); String contentStream = new String(streamBytes); System.out.println(contentStream); PRTokeniser tokenizer = new PRTokeniser(streamBytes); while (tokenizer.nextToken()) { if (tokenizer.getTokenType() == PRTokeniser.TK_STRING) { System.out.println(tokenizer.getStringValue()); } } StringBuffer buf = new StringBuffer(); int pos = contentStream.indexOf("Hello World") + 11; buf.append(contentStream.substring(0, pos)); buf.append("Hello"); buf.append(contentStream.substring(pos)); String hackedContentStream = buf.toString(); document = new Document(PageSize.A6); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("HelloWorldStreamHacked.pdf")); document.open(); PdfContentByte cb = writer.getDirectContent(); cb.setLiteral(hackedContentStream); document.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { // Create a manifest from a file InputStream fis = new FileInputStream("manifestfile"); Manifest manifest = new Manifest(fis); // Construct a string version of a manifest StringBuffer sbuf = new StringBuffer(); sbuf.append("Manifest-Version: 1.0\n"); sbuf.append("\n"); sbuf.append("Name: javax/swing/JScrollPane.class\n"); sbuf.append("Java-Bean: True\n"); // Convert the string to a input stream InputStream is = new ByteArrayInputStream(sbuf.toString().getBytes("UTF-8")); // Create the manifest manifest = new Manifest(is); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Document document = new Document(PageSize.A4); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf")); document.open();/*from w w w .jav a 2 s. c o m*/ PdfContentByte cb = writer.getDirectContent(); StringBuffer sb = new StringBuffer(1024); BufferedReader reader = new BufferedReader(new FileReader("a.txt")); int c; while ((c = reader.read()) > -1) { sb.append((char) c); } reader.close(); ColumnText ct = new ColumnText(cb); ct.setSimpleColumn(new Phrase(sb.toString()), 36, 36, PageSize.A4.width() - 36, PageSize.A4.height() - 36, 18, Element.ALIGN_JUSTIFIED); int status = ColumnText.START_COLUMN; while (ColumnText.hasMoreText(status)) { status = ct.go(); ct.setYLine(PageSize.A4.height() - 36); document.newPage(); } document.close(); }
From source file:MainClass.java
public static void main(String[] a) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setSize(250, 200);//from www . j av a2 s. c o m shell.setLayout(new FillLayout()); Table table = new Table(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); Transfer[] types = new Transfer[] { TextTransfer.getInstance() }; DragSource source = new DragSource(table, DND.DROP_MOVE | DND.DROP_COPY); source.setTransfer(types); source.addDragListener(new DragSourceAdapter() { public void dragSetData(DragSourceEvent event) { DragSource ds = (DragSource) event.widget; Table table = (Table) ds.getControl(); TableItem[] selection = table.getSelection(); StringBuffer buff = new StringBuffer(); for (int i = 0, n = selection.length; i < n; i++) { buff.append(selection[i].getText()); } event.data = buff.toString(); } }); DropTarget target = new DropTarget(table, DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT); target.setTransfer(types); target.addDropListener(new DropTargetAdapter() { public void dragEnter(DropTargetEvent event) { if (event.detail == DND.DROP_DEFAULT) { event.detail = (event.operations & DND.DROP_COPY) != 0 ? DND.DROP_COPY : DND.DROP_NONE; } for (int i = 0, n = event.dataTypes.length; i < n; i++) { if (TextTransfer.getInstance().isSupportedType(event.dataTypes[i])) { event.currentDataType = event.dataTypes[i]; } } } public void dragOver(DropTargetEvent event) { event.feedback = DND.FEEDBACK_SELECT | DND.FEEDBACK_SCROLL; } public void drop(DropTargetEvent event) { if (TextTransfer.getInstance().isSupportedType(event.currentDataType)) { DropTarget target = (DropTarget) event.widget; Table table = (Table) target.getControl(); String data = (String) event.data; TableItem item = new TableItem(table, SWT.NONE); item.setText(new String[] { data }); table.redraw(); } } }); TableColumn column = new TableColumn(table, SWT.NONE); TableItem item = new TableItem(table, SWT.NONE); item.setText(new String[] { "A" }); item = new TableItem(table, SWT.NONE); item.setText(new String[] { "B" }); item = new TableItem(table, SWT.BORDER); item.setText(new String[] { "C" }); column.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:com.bright.utils.rmDuplicateLines.java
public static void main(String args) { File monfile = new File(args); Set<String> userIdSet = new LinkedHashSet<String>(); if (monfile.isFile() && monfile.getName().endsWith(".txt")) { try {// w w w . ja v a2 s . c o m List<String> content = FileUtils.readLines(monfile, Charset.forName("UTF-8")); userIdSet.addAll(content); Iterator<String> itr = userIdSet.iterator(); StringBuffer output = new StringBuffer(); while (itr.hasNext()) { output.append(itr.next() + System.getProperty("line.separator")); } BufferedWriter out = new BufferedWriter(new FileWriter(monfile)); String outText = output.toString(); out.write(outText); out.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:DragDropTable.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); Table table = new Table(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); Transfer[] types = new Transfer[] { TextTransfer.getInstance() }; DragSource source = new DragSource(table, DND.DROP_MOVE | DND.DROP_COPY); source.setTransfer(types);//ww w.j a v a 2 s .c o m source.addDragListener(new DragSourceAdapter() { public void dragSetData(DragSourceEvent event) { // Get the selected items in the drag source DragSource ds = (DragSource) event.widget; Table table = (Table) ds.getControl(); TableItem[] selection = table.getSelection(); StringBuffer buff = new StringBuffer(); for (int i = 0, n = selection.length; i < n; i++) { buff.append(selection[i].getText()); } event.data = buff.toString(); } }); // Create the drop target DropTarget target = new DropTarget(table, DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT); target.setTransfer(types); target.addDropListener(new DropTargetAdapter() { public void dragEnter(DropTargetEvent event) { if (event.detail == DND.DROP_DEFAULT) { event.detail = (event.operations & DND.DROP_COPY) != 0 ? DND.DROP_COPY : DND.DROP_NONE; } // Allow dropping text only for (int i = 0, n = event.dataTypes.length; i < n; i++) { if (TextTransfer.getInstance().isSupportedType(event.dataTypes[i])) { event.currentDataType = event.dataTypes[i]; } } } public void dragOver(DropTargetEvent event) { event.feedback = DND.FEEDBACK_SELECT | DND.FEEDBACK_SCROLL; } public void drop(DropTargetEvent event) { if (TextTransfer.getInstance().isSupportedType(event.currentDataType)) { // Get the dropped data DropTarget target = (DropTarget) event.widget; Table table = (Table) target.getControl(); String data = (String) event.data; // Create a new item in the table to hold the dropped data TableItem item = new TableItem(table, SWT.NONE); item.setText(new String[] { data }); table.redraw(); } } }); TableColumn column = new TableColumn(table, SWT.NONE); // Seed the table TableItem item = new TableItem(table, SWT.NONE); item.setText(new String[] { "A" }); item = new TableItem(table, SWT.NONE); item.setText(new String[] { "B" }); item = new TableItem(table, SWT.BORDER); item.setText(new String[] { "C" }); column.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }