List of usage examples for java.nio CharBuffer toString
public String toString()
From source file:org.codehaus.groovy.grails.web.util.StreamByteBuffer.java
public String readAsString(Charset charset) throws CharacterCodingException { int unreadSize = totalBytesUnread(); if (unreadSize > 0) { CharsetDecoder decoder = charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE); CharBuffer charbuffer = CharBuffer.allocate(unreadSize); ByteBuffer buf = null;/*from w w w. ja v a 2 s . c o m*/ while (prepareRead() != -1) { buf = currentReadChunk.readToNioBuffer(); boolean endOfInput = (prepareRead() == -1); CoderResult result = decoder.decode(buf, charbuffer, endOfInput); if (endOfInput) { if (!result.isUnderflow()) { result.throwException(); } } } CoderResult result = decoder.flush(charbuffer); if (buf.hasRemaining()) { throw new IllegalStateException("There's a bug here, buffer wasn't read fully."); } if (!result.isUnderflow()) { result.throwException(); } charbuffer.flip(); String str; if (charbuffer.hasArray()) { int len = charbuffer.remaining(); char[] ch = charbuffer.array(); if (len != ch.length) { ch = ArrayUtils.subarray(ch, 0, len); } str = StringCharArrayAccessor.createString(ch); } else { str = charbuffer.toString(); } return str; } return null; }
From source file:com.servoy.j2db.util.Utils.java
public static String getTXTFileContent(File f, Charset charset) { if (f != null /* && f.exists() */) { if (Thread.currentThread().isInterrupted()) { Thread.interrupted(); // reset interrupted flag of current thread, FileChannel.read() will throw an exception for it. }/*from w ww .ja va 2s.c o m*/ FileInputStream fis = null; try { int length = (int) f.length(); if (f.exists()) { fis = new FileInputStream(f); FileChannel fc = fis.getChannel(); ByteBuffer bb = ByteBuffer.allocate(length); fc.read(bb); bb.rewind(); CharBuffer cb = charset.decode(bb); return cb.toString(); } } catch (Exception e) { Debug.error("Error reading txt file: " + f, e); //$NON-NLS-1$ } finally { closeInputStream(fis); } } return null; }
From source file:com.zhengde163.netguard.ServiceSinkhole.java
/** * ByteBuffer ? String/* w w w . ja va 2s.c o m*/ * * @param buffer * @return */ public static String getString(ByteBuffer buffer) { Charset charset = null; CharsetDecoder decoder = null; CharBuffer charBuffer = null; try { charset = Charset.forName("UTF-8"); decoder = charset.newDecoder(); // charBuffer = decoder.decode(buffer);//??? charBuffer = decoder.decode(buffer.asReadOnlyBuffer()); return charBuffer.toString(); } catch (Exception ex) { ex.printStackTrace(); return ""; } }
From source file:org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java
public static Object isCodepage(Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext) { boolean bRC = false; if (ArgList.length == 2) { try {//from w ww .j a va 2 s . c o m if (isNull(ArgList, new int[] { 0, 1 })) { return null; } else if (isUndefined(ArgList, new int[] { 0, 1 })) { return Context.getUndefinedValue(); } String strValueToCheck = Context.toString(ArgList[0]); String strCodePage = Context.toString(ArgList[1]); byte[] bytearray = strValueToCheck.getBytes(); CharsetDecoder d = Charset.forName(strCodePage).newDecoder(); CharBuffer r = d.decode(ByteBuffer.wrap(bytearray)); r.toString(); bRC = true; } catch (Exception e) { bRC = false; } } else { throw Context.reportRuntimeError("The function call isCodepage requires 2 arguments."); } return Boolean.valueOf(bRC); }
From source file:jackpal.androidterm.Term.java
private void copyFileToClipboard(String filename) { if (filename == null) return;//from www. j av a 2s .c om FileInputStream fis; try { fis = new FileInputStream(filename); FileChannel fc = fis.getChannel(); try { ByteBuffer bbuf; bbuf = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int) fc.size()); // Create a read-only CharBuffer on the file CharBuffer cbuf = Charset.forName("UTF-8").newDecoder().decode(bbuf); String str = cbuf.toString(); ClipboardManagerCompat clip = ClipboardManagerCompatFactory.getManager(getApplicationContext()); clip.setText(str); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:org.bimserver.ifc.step.deserializer.IfcStepDeserializer.java
private String readString(String value) throws DeserializeException { String result = value.substring(1, value.length() - 1); // Replace all '' with ' while (result.contains("''")) { int index = result.indexOf("''"); result = result.substring(0, index) + "'" + result.substring(index + 2); }/*from w ww . ja v a 2s . co m*/ while (result.contains("\\S\\")) { int index = result.indexOf("\\S\\"); char x = result.charAt(index + 3); ByteBuffer b = ByteBuffer.wrap(new byte[] { (byte) (x + 128) }); CharBuffer decode = Charsets.ISO_8859_1.decode(b); result = result.substring(0, index) + decode.get() + result.substring(index + 4); } while (result.contains("\\X\\")) { int index = result.indexOf("\\X\\"); int code = Integer.parseInt(result.substring(index + 3, index + 5), 16); ByteBuffer b = ByteBuffer.wrap(new byte[] { (byte) (code) }); CharBuffer decode = Charsets.ISO_8859_1.decode(b); result = result.substring(0, index) + decode.get() + result.substring(index + 5); } while (result.contains("\\X2\\")) { int index = result.indexOf("\\X2\\"); int indexOfEnd = result.indexOf("\\X0\\"); if (indexOfEnd == -1) { throw new DeserializeException("\\X2\\ not closed with \\X0\\"); } if ((indexOfEnd - index) % 4 != 0) { throw new DeserializeException("Number of hex chars in \\X2\\ definition not divisible by 4"); } try { ByteBuffer buffer = ByteBuffer .wrap(Hex.decodeHex(result.substring(index + 4, indexOfEnd).toCharArray())); CharBuffer decode = Charsets.UTF_16BE.decode(buffer); result = result.substring(0, index) + decode.toString() + result.substring(indexOfEnd + 4); } catch (DecoderException e) { throw new DeserializeException(e); } } while (result.contains("\\X4\\")) { int index = result.indexOf("\\X4\\"); int indexOfEnd = result.indexOf("\\X0\\"); if (indexOfEnd == -1) { throw new DeserializeException("\\X4\\ not closed with \\X0\\"); } if ((indexOfEnd - index) % 8 != 0) { throw new DeserializeException("Number of hex chars in \\X4\\ definition not divisible by 8"); } try { ByteBuffer buffer = ByteBuffer .wrap(Hex.decodeHex(result.substring(index + 4, indexOfEnd).toCharArray())); CharBuffer decode = Charset.forName("UTF-32").decode(buffer); result = result.substring(0, index) + decode.toString() + result.substring(indexOfEnd + 4); } catch (DecoderException e) { throw new DeserializeException(e); } catch (UnsupportedCharsetException e) { throw new DeserializeException("UTF-32 is not supported on your system", e); } } // Replace all \\ with \ while (result.contains("\\\\")) { int index = result.indexOf("\\\\"); result = result.substring(0, index) + "\\" + result.substring(index + 2); } return result; }
From source file:com.arksoft.epamms.ZGlobal1_Operation.java
int AddAttributeToCSV(CharBuffer cb, int nLth, View lLibPers, String entityName, String attributeName, boolean bNumeric) { String s = null;//from w w w . j ava 2 s .co m cb.put(0, '"'); // opening quote // if ( bNumeric ) // { s = GetStringFromAttribute(s, lLibPers, entityName, attributeName); nLth = zstrcpy(cb, 1, s); // } // else // { // String stringAttrib; // // GetAddrForAttribute( &stringAttrib, lLibPers, entityName, stringAttribute ); // zstrcpy( stringBuffer, stringAttrib ); // } s = cb.toString(); if (s.indexOf('"', 1) > 0) { s = s.replace("\"", "\"\" "); // double any quotes ... s = s.substring(1); // except the first one } s += "\","; // terminating quote plus comma nLth = zstrcpy(cb, 0, s); return nLth; }
From source file:com.arksoft.epamms.ZGlobal1_Operation.java
public int StartEmailClientForListReus(View vResult, String entityName, String attributeName, String contextName, String stringScope, int bUseOnlySelectedEntities, int bUseParentSelectedEntities, String stringSubject, String stringCopyTo, // comma separated list String stringBlindCopy, // comma separated list String stringBody, String stringAttachment, String stringEmailClient, int lFlags, String stringBlindCopyFlag) // reserved { String stringParentEntity = null; String s = null;/* w w w . ja v a2 s. co m*/ int lEntityCnt; int ulAttributeLth = 0; int lTotalSize; int lLth = 0; int lRC; if (bUseParentSelectedEntities != 0) stringParentEntity = MiGetParentEntityNameForView(stringParentEntity, vResult, entityName); lEntityCnt = CountEntitiesForView(vResult, entityName); ulAttributeLth = GetAttributeDisplayLength(ulAttributeLth, vResult, entityName, attributeName, contextName); lTotalSize = lEntityCnt * (int) ulAttributeLth; // a starting point CharBuffer cbMemory = CharBuffer.allocate(lTotalSize + 1); // DrAllocTaskMemory( cbMemory, lTotalSize + 1 ); // For each entity, append the specified data to the list. // lRC = SetCursorFirstEntity( vResult, entityName, stringScope ); lRC = SetEntityCursor(vResult, entityName, "", zPOS_FIRST, "", "", "", 0, stringScope, ""); while (lRC > zCURSOR_UNCHANGED) { if (bUseOnlySelectedEntities == 0 || ((bUseOnlySelectedEntities != 0) && GetSelectStateOfEntity(vResult, entityName) != 0) || ((bUseParentSelectedEntities != 0) && GetSelectStateOfEntity(vResult, stringParentEntity) != 0)) { s = GetVariableFromAttribute(s, 0, zTYPE_STRING, lTotalSize - lLth - 1, vResult, entityName, attributeName, contextName, contextName != null && contextName.isEmpty() == false ? 0 : zUSE_DEFAULT_CONTEXT); lLth = zstrcpy(cbMemory, lLth, s); while (lLth > 0 && cbMemory.charAt(lLth - 1) == ' ') { lLth--; cbMemory.put(lLth, '\0'); } } // lRC = SetCursorNextEntity( vResult, entityName, stringScope ); lRC = SetEntityCursor(vResult, entityName, "", zPOS_NEXT, "", "", "", 0, stringScope, ""); if (lRC > zCURSOR_UNCHANGED) { // lLth = zstrlen( stringMemory ); if (lTotalSize - lLth < (int) ulAttributeLth) { s = cbMemory.toString(); lEntityCnt *= 2; lTotalSize = lEntityCnt * (int) ulAttributeLth; cbMemory = CharBuffer.allocate(lTotalSize + 1); zstrcpy(cbMemory, 0, s); } if (lLth > 0 && cbMemory.charAt(lLth - 1) != ',') { cbMemory.put(lLth++, ','); cbMemory.put(lLth, '\0'); } } } if (stringBlindCopyFlag.charAt(0) == 'Y') { // Email Addresses are to be put in Blind Copy parameter. TraceLineS("Blind Copies: ", cbMemory.toString()); lRC = m_ZDRVROPR.StartEmailClient(stringBlindCopy, // Regular send parameter stringSubject, stringCopyTo, // comma separated list cbMemory.toString(), // Blind Copy parameter stringBody, stringAttachment, "", lFlags); // reserved } else { // Email Addresses are to be put in regular Send parameter. TraceLineS("Regular Copies: ", cbMemory.toString()); lRC = m_ZDRVROPR.StartEmailClient(cbMemory.toString(), // comma separated list stringSubject, stringCopyTo, // comma separated list stringBlindCopy, // comma separated list stringBody, stringAttachment, stringEmailClient, lFlags); // reserved } // DrFreeTaskMemory( (String) cbMemory ); return lRC; }
From source file:com.quinsoft.swauopencuas.ZGLOBAL1_Operation.java
public int StartEmailClientForListReus(View vResult, String entityName, String attributeName, String contextName, String stringScope, int bUseOnlySelectedEntities, int bUseParentSelectedEntities, String stringSubject, String stringCopyTo, // comma separated list String stringBlindCopy, // comma separated list String stringBody, String stringAttachment, String stringEmailClient, int lFlags, String stringBlindCopyFlag) // reserved { String stringParentEntity = null; String s = null;// w w w . ja v a2 s . c o m int lEntityCnt; int ulAttributeLth = 0; int lTotalSize; int lLth = 0; int lRC; if (bUseParentSelectedEntities != 0) stringParentEntity = MiGetParentEntityNameForView(stringParentEntity, vResult, entityName); lEntityCnt = CountEntitiesForView(vResult, entityName); ulAttributeLth = GetAttributeDisplayLength(ulAttributeLth, vResult, entityName, attributeName, contextName); lTotalSize = lEntityCnt * ulAttributeLth; // a starting point CharBuffer cbMemory = CharBuffer.allocate(lTotalSize + 1); // DrAllocTaskMemory( cbMemory, lTotalSize + 1 ); // For each entity, append the specified data to the list. // lRC = SetCursorFirstEntity( vResult, entityName, stringScope ); lRC = SetEntityCursor(vResult, entityName, "", zPOS_FIRST, "", "", "", 0, stringScope, ""); while (lRC > zCURSOR_UNCHANGED) { if (bUseOnlySelectedEntities == 0 || ((bUseOnlySelectedEntities != 0) && GetSelectStateOfEntity(vResult, entityName) != 0) || ((bUseParentSelectedEntities != 0) && GetSelectStateOfEntity(vResult, stringParentEntity) != 0)) { s = GetVariableFromAttribute(s, 0, zTYPE_STRING, lTotalSize - lLth - 1, vResult, entityName, attributeName, contextName, contextName != null && contextName.isEmpty() == false ? 0 : zUSE_DEFAULT_CONTEXT); lLth = zstrcpy(cbMemory, lLth, s); while (lLth > 0 && cbMemory.charAt(lLth - 1) == ' ') { lLth--; cbMemory.put(lLth, '\0'); } } // lRC = SetCursorNextEntity( vResult, entityName, stringScope ); lRC = SetEntityCursor(vResult, entityName, "", zPOS_NEXT, "", "", "", 0, stringScope, ""); if (lRC > zCURSOR_UNCHANGED) { // lLth = zstrlen( stringMemory ); if (lTotalSize - lLth < ulAttributeLth) { s = cbMemory.toString(); lEntityCnt *= 2; lTotalSize = lEntityCnt * ulAttributeLth; cbMemory = CharBuffer.allocate(lTotalSize + 1); zstrcpy(cbMemory, 0, s); } if (lLth > 0 && cbMemory.charAt(lLth - 1) != ',') { cbMemory.put(lLth++, ','); cbMemory.put(lLth, '\0'); } } } if (stringBlindCopyFlag.charAt(0) == 'Y') { // Email Addresses are to be put in Blind Copy parameter. TraceLineS("Blind Copies: ", cbMemory.toString()); lRC = m_ZDRVROPR.StartEmailClient(stringBlindCopy, // Regular send parameter stringSubject, stringCopyTo, // comma separated list cbMemory.toString(), // Blind Copy parameter stringBody, stringAttachment, "", lFlags); // reserved } else { // Email Addresses are to be put in regular Send parameter. TraceLineS("Regular Copies: ", cbMemory.toString()); lRC = m_ZDRVROPR.StartEmailClient(cbMemory.toString(), // comma separated list stringSubject, stringCopyTo, // comma separated list stringBlindCopy, // comma separated list stringBody, stringAttachment, stringEmailClient, lFlags); // reserved } // DrFreeTaskMemory( (String) cbMemory ); return lRC; }
From source file:com.arksoft.epamms.ZGlobal1_Operation.java
public int SendEmailForFiles(View ViewToWindow, View ResultSet, String stringSmtpServer, String stringRecipientEMailAddress, String stringSenderEMailAddress, String stringEMailUserName, String stringEMailPassword, String stringSubjectLine, String stringBodyFileName, String stringAltTextFileName, String stringEmbeddedImages, String stringAttachmentFileName, int nMimeType, // 1-Text, 2-HTML int lConnection) { // String stringBodyMemoryStart; CharBuffer cbAtBodyFileName = CharBuffer.allocate(256); CharBuffer cbAtAltTextFileName = CharBuffer.allocate(256); // int selBodyMemory; // int lFileLth; zVIEW zqMDocOLST = null;/*w w w . j av a2s. c om*/ zVIEW wXferO = null; int nRC; // TraceLine( "SendEmailForFiles Server: %s Sender: %s Recipient: %s" // " Subject: %s Mime Type: %d" // " User: %s Password %s", // stringSmtpServer, stringSenderEMailAddress, stringRecipientEMailAddress, // stringSubjectLine, nMimeType, stringEMailUserName, stringEMailPassword ); // First make sure the email address is valid. If not exit with return code of 2. if (IsEmailAddressValid(stringRecipientEMailAddress) == false) return 2; GetViewByName(zqMDocOLST, "zqMDocOLST", ResultSet, zLEVEL_TASK); GetViewByName(wXferO, "wXferO", ViewToWindow, zLEVEL_TASK); if (stringBodyFileName != null) { if (stringBodyFileName.isEmpty() == false && stringBodyFileName.charAt(0) != '@') { cbAtBodyFileName.put(0, '@'); zstrcpy(cbAtBodyFileName, 1, stringBodyFileName); } else zstrcpy(cbAtBodyFileName, 0, stringBodyFileName); } else cbAtBodyFileName.put(0, '\0'); if (stringAltTextFileName != null) { if (stringAltTextFileName.isEmpty() == false && stringAltTextFileName.charAt(0) != '@') { cbAtAltTextFileName.put(0, '@'); zstrcpy(cbAtAltTextFileName, 1, stringAltTextFileName); } else zstrcpy(cbAtAltTextFileName, 0, stringAltTextFileName); } else cbAtAltTextFileName.put(0, '\0'); // Read the data from the Body and Attachment files into memory and call // StartEmailClient with those values. // Read the Body into memory. // lFileLth = ReadFileDataIntoMemory( ResultSet, stringBodyFileName, // &selBodyMemory, &stringBodyMemoryStart ); // Exit if the file is empty or if there is an error opening it. // if ( lFileLth <= 0 ) // { // The memory allocated to hold the body has been freed. // IssueError( ResultSet, 0, 0, "Can't open Email file." ); // return -1; // } if (stringSubjectLine == null || stringSubjectLine.isEmpty()) stringSubjectLine = " "; // TraceLine( "SendEmailForFiles2 Server: %s Sender: %s Recipient: %s" // " Subject: %s Mime Type: %d" // " User: %s Password %s", // stringSmtpServer, stringSenderEMailAddress, stringRecipientEMailAddress, // stringSubjectLine, nMimeType, stringEMailUserName, stringEMailPassword ); // If there is an attachment file, also read it into memory. // Then call CreateSeeMessage with or without an attachment. if (stringAttachmentFileName.isEmpty() == false) { nRC = m_ZDRVROPR.CreateSeeMessage(lConnection, stringSmtpServer, stringSenderEMailAddress, stringRecipientEMailAddress, "", "", stringSubjectLine, nMimeType, cbAtBodyFileName.toString(), cbAtAltTextFileName.toString(), stringEmbeddedImages, 1, // has attachment stringAttachmentFileName, stringEMailUserName, stringEMailPassword); } else { nRC = m_ZDRVROPR.CreateSeeMessage(lConnection, stringSmtpServer, stringSenderEMailAddress, stringRecipientEMailAddress, "", "", stringSubjectLine, nMimeType, cbAtBodyFileName.toString(), cbAtAltTextFileName.toString(), stringEmbeddedImages, 0, // no attachment "", // blank attachment file name stringEMailUserName, stringEMailPassword); } // SysFreeMemory( selBodyMemory ); // DrFreeTaskMemory( stringBodyMemoryStart ); return nRC; }