Java Code Examples for java.nio.CharBuffer
The following code examples are extracted from open source projects. You can click to
vote up the examples that are useful to you.
Example 1
From project Android-Terminal-Emulator, under directory /libraries/emulatorview/src/jackpal/androidterm/emulatorview/.
Source file: TermSession.java

/** * Write the UTF-8 representation of a single Unicode code point to the terminal output. The written data will be consumed by the emulation client as input. <p> This implementation encodes the code point and then calls {@link #write(byte[],int,int)} to do the actual writing. It shouldtherefore usually be unnecessary to override this method; override {@link #write(byte[],int,int)} instead. * @param codePoint The Unicode code point to write to the terminal. */ public void write(int codePoint){ CharBuffer charBuf=mWriteCharBuffer; ByteBuffer byteBuf=mWriteByteBuffer; CharsetEncoder encoder=mUTF8Encoder; charBuf.clear(); byteBuf.clear(); Character.toChars(codePoint,charBuf.array(),0); encoder.reset(); encoder.encode(charBuf,byteBuf,true); encoder.flush(byteBuf); write(byteBuf.array(),0,byteBuf.position() - 1); }
Example 2
From project android_external_guava, under directory /src/com/google/common/io/.
Source file: CharStreams.java

/** * Copies all characters between the {@link Readable} and {@link Appendable}objects. Does not close or flush either object. * @param from the object to read from * @param to the object to write to * @return the number of characters copied * @throws IOException if an I/O error occurs */ public static long copy(Readable from,Appendable to) throws IOException { CharBuffer buf=CharBuffer.allocate(BUF_SIZE); long total=0; while (true) { int r=from.read(buf); if (r == -1) { break; } buf.flip(); to.append(buf,0,r); total+=r; } return total; }
Example 3
From project asterisk-java, under directory /src/main/java/org/asteriskjava/config/.
Source file: ConfigFileReader.java

void readFile(String configfile,BufferedReader reader) throws IOException, ConfigParseException { String line; int lineno=0; CharBuffer buffer=CharBuffer.allocate(MAX_LINE_LENGTH); reset(); while ((line=reader.readLine()) != null) { lineno++; buffer.clear(); buffer.put(line); buffer.put("\n"); buffer.flip(); processLine(configfile,lineno,buffer); } }
Example 4
From project bson4jackson, under directory /src/main/java/de/undercouch/bson4jackson/io/.
Source file: StaticBuffers.java

/** * Creates or re-uses a {@link CharBuffer} that has a minimum size. Callingthis method multiple times with the same key will always return the same buffer, as long as it has the minimum size and is marked to be re-used. Buffers that are allowed to be re-used should be released using {@link #releaseCharBuffer(Key,CharBuffer)}. * @param key the buffer's identifier * @param minSize the minimum size * @return the {@link CharBuffer} instance */ public CharBuffer charBuffer(Key key,int minSize){ minSize=Math.max(minSize,GLOBAL_MIN_SIZE); CharBuffer r=_charBuffers[key.ordinal()]; if (r == null || r.capacity() < minSize) { r=CharBuffer.allocate(minSize); } else { _charBuffers[key.ordinal()]=null; r.clear(); } return r; }
Example 5
From project cloudhopper-commons-charset, under directory /src/main/java/com/cloudhopper/commons/charset/.
Source file: JavaCharset.java

@Override public void decode(byte[] bytes,StringBuilder buffer){ if (bytes == null) { return; } ByteBuffer byteBuffer=ByteBuffer.wrap(bytes); CharBuffer charBuffer=charset.decode(byteBuffer); buffer.append(charBuffer); }
Example 6
From project cloudhopper-commons-util, under directory /src/main/java/com/cloudhopper/commons/util/codec/.
Source file: URLCodec.java

public static void encode(CharSequence in,Charset charset,Appendable out) throws IOException { CharBuffer cb; if (in instanceof CharBuffer) { cb=(CharBuffer)in; } else if (in != null) { cb=CharBuffer.wrap(in); } else { return; } encode(charset.encode(cb),out); }
Example 7
From project coala, under directory /modules/coala-communication/src/main/java/org/openehealth/coala/.
Source file: StringEncodingUtils.java

/** * Converts a UTF-8 encoded {@link String} to ISO-LATIN-1 bytes via a ByteBuffer. * @param inUTF8 Any {@link String} that was created holding UTF-8 characters. * @return A {@link String} holding <code>inUTF8</code> characters coded asISO-8859-1. * @throws CharacterCodingException */ public static String convertUTF8ToISO(String inUTF8) throws CharacterCodingException { ByteBuffer inputBuffer=utf8charset.newEncoder().encode(CharBuffer.wrap(inUTF8)); CharBuffer data=utf8charset.decode(inputBuffer); ByteBuffer outputBuffer=iso88591charset.encode(data); byte[] outputData=outputBuffer.array(); return new String(outputData); }
Example 8
From project culvert, under directory /culvert-main/src/main/java/com/bah/culvert/data/index/.
Source file: Index.java

/** * Used to set a key indicating if the string value held by another configuration key is a base64 encoded binary or not. * @param isValueBinaryEncodedSetting The key telling weather or not the otherkey (setting) is base64. * @param potentiallyEncodedSetting The actual key that might be base64encoded. * @param data The data to set as base64. * @param conf The configuration to do the setting on. */ private static void setBinaryConfSetting(String isValueBinaryEncodedSetting,String potentiallyEncodedSetting,byte[] data,Configuration conf){ CharsetDecoder decoder=UTF_8.newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPORT); try { CharBuffer colFamString=decoder.decode(ByteBuffer.wrap(data)); conf.setBoolean(isValueBinaryEncodedSetting,false); conf.set(potentiallyEncodedSetting,colFamString.toString()); } catch ( CharacterCodingException e) { conf.setBoolean(isValueBinaryEncodedSetting,true); conf.set(potentiallyEncodedSetting,new String(Base64.encodeBase64(data),UTF_8)); } }
Example 9
From project Diver, under directory /ca.uvic.chisel.javasketch/src/ca/uvic/chisel/javasketch/persistence/internal/logs/.
Source file: TraceLogEvent.java

protected String readShortString(RandomAccessFile file) throws IOException { int length=file.readUnsignedShort(); byte[] buffer=new byte[length]; file.read(buffer); Charset charset=Charset.forName("UTF-8"); CharsetDecoder decoder=charset.newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPLACE); decoder.replaceWith("]"); CharBuffer chars=decoder.decode(ByteBuffer.wrap(buffer)); return chars.toString(); }
Example 10
From project DoubleArrayTrie, under directory /src/main/org/digitalstain/datrie/mapping/.
Source file: CharacterNaturalMapping.java

/** * @see org.digitalstain.datrie.mapping.NaturalMapping#toNatural(java.lang.Object) */ @Override public int toNatural(Character object){ CharBuffer cb=CharBuffer.allocate(1); cb.put(object.charValue()); cb.flip(); ByteBuffer bb=Charset.defaultCharset().encode(cb); return bb.get(); }
Example 11
From project EasySOA, under directory /easysoa-proxy/easysoa-proxy-core/easysoa-proxy-core-api/src/main/java/org/easysoa/proxy/core/api/util/.
Source file: ContentReader.java

/** * Read a <code>Reader</code> and returns it's content as a string * @param reader The <code>Reader</code> to read * @return */ public static final String read(Reader reader) throws Exception { StringBuffer requestBody=new StringBuffer(); CharBuffer buffer=CharBuffer.allocate(512); while (reader.read(buffer) >= 0) { requestBody.append(buffer.flip()); buffer.clear(); } requestBody.trimToSize(); return requestBody.toString(); }
Example 12
From project fastjson, under directory /src/main/java/com/alibaba/fastjson/.
Source file: JSON.java

public static final Object parse(byte[] input,int off,int len,CharsetDecoder charsetDecoder,int features){ charsetDecoder.reset(); int scaleLength=(int)(len * (double)charsetDecoder.maxCharsPerByte()); char[] chars=ThreadLocalCache.getChars(scaleLength); ByteBuffer byteBuf=ByteBuffer.wrap(input,off,len); CharBuffer charBuf=CharBuffer.wrap(chars); IOUtils.decode(charsetDecoder,byteBuf,charBuf); int position=charBuf.position(); DefaultJSONParser parser=new DefaultJSONParser(chars,position,ParserConfig.getGlobalInstance(),features); Object value=parser.parse(); handleResovleTask(parser,value); parser.close(); return value; }
Example 13
From project FBReaderJ, under directory /src/org/geometerplus/zlibrary/core/html/.
Source file: ZLByteBuffer.java

public String toString(CharsetDecoder decoder){ if (myStringValue == null) { synchronized (myConverterLock) { if (myConverterBuffer.length < myLength) { myConverterBuffer=new char[myLength]; } ByteBuffer byteBuffer=ByteBuffer.wrap(myData,0,myLength); CharBuffer charBuffer=CharBuffer.wrap(myConverterBuffer); decoder.decode(byteBuffer,charBuffer,true); myStringValue=new String(myConverterBuffer,0,charBuffer.position()); } } return myStringValue; }
Example 14
From project gecko, under directory /src/main/java/com/taobao/gecko/core/core/impl/.
Source file: TextLineCodecFactory.java

public Object decode(final IoBuffer buffer,final Session session){ String result=null; final int index=SPLIT_PATTERN.matchFirst(buffer); if (index >= 0) { final int limit=buffer.limit(); buffer.limit(index); final CharBuffer charBuffer=TextLineCodecFactory.this.charset.decode(buffer.buf()); result=charBuffer.toString(); buffer.limit(limit); buffer.position(index + SPLIT.remaining()); } return result; }
Example 15
From project gravitext, under directory /gravitext-util/src/main/java/com/gravitext/jruby/.
Source file: IOUtils.java

public static ByteList toByteList(final CharSequence value){ if (value instanceof String) { return toByteList((String)value); } final CharBuffer cbuff=(value instanceof CharBuffer) ? ((CharBuffer)value).duplicate() : CharBuffer.wrap(value); final ByteBuffer out=Charsets.UTF_8.encode(cbuff); return new ByteList(out.array(),out.arrayOffset() + out.position(),out.remaining(),UTF8Encoding.INSTANCE,false); }
Example 16
From project hawtbuf, under directory /hawtbuf-protoc/src/main/java/org/fusesource/hawtbuf/proto/compiler/.
Source file: TextFormat.java

private static StringBuilder toStringBuilder(Readable input) throws IOException { StringBuilder text=new StringBuilder(); CharBuffer buffer=CharBuffer.allocate(BUFFER_SIZE); while (true) { int n=input.read(buffer); if (n == -1) { break; } buffer.flip(); text.append(buffer,0,n); } return text; }
Example 17
From project HBase-Lattice, under directory /hbl/src/main/java/com/inadco/hbl/model/.
Source file: UTF8CharDimension.java

@Override public Object getMember(byte[] buff,int offset) throws HblException { int l=len - 1; for (; l >= 0; l--) { if (buff[offset + l] != (byte)0x0) break; } CharBuffer cb=utf8.decode(ByteBuffer.wrap(buff,offset,++l)); return cb.toString(); }
Example 18
From project hs4j, under directory /src/main/java/com/google/code/hs4j/network/core/impl/.
Source file: TextLineCodecFactory.java

public Object decode(IoBuffer buffer,Session session){ String result=null; int index=SPLIT_PATTERN.matchFirst(buffer); if (index >= 0) { int limit=buffer.limit(); buffer.limit(index); CharBuffer charBuffer=TextLineCodecFactory.this.charset.decode(buffer.buf()); result=charBuffer.toString(); buffer.limit(limit); buffer.position(index + SPLIT.remaining()); } return result; }
Example 19
From project iudex_1, under directory /iudex-core/src/main/java/iudex/core/.
Source file: VisitURL.java

/** * Return a 23-character URL64 encoded hash of the complete URL. */ static CharSequence hashURL(CharSequence url){ byte[] sha=hash(url); char[] chars=URL64.encode(sha,0,18); CharBuffer cbuff=CharBuffer.wrap(chars); cbuff.limit(23); return cbuff; }
Example 20
From project james-imap, under directory /message/src/main/java/org/apache/james/imap/decode/.
Source file: ImapRequestLineReader.java

/** * Increases the size of the character buffer. */ private void upsizeCharBuffer(){ final int oldCapacity=charBuffer.capacity(); CharBuffer oldBuffer=charBuffer; charBuffer=CharBuffer.allocate(oldCapacity + QUOTED_BUFFER_INITIAL_CAPACITY); oldBuffer.flip(); charBuffer.put(oldBuffer); }
Example 21
From project jpropel-light, under directory /src/propel/core/utils/.
Source file: StreamUtils.java

/** * Blocks until the specified string of characters is encountered and all characters read until then from the UTF8-encoded byte-stream are skipped. * @throws NullPointerException An argument is null. * @throws IOException An I/O exception occurred. */ @Validate public static void skipUntil(@NotNull final InputStream stream,@NotNull final String terminator,@NotNull final Charset streamEncoding) throws IOException { val terminatorCharBuffer=ByteBuffer.wrap(terminator.getBytes(streamEncoding)).asCharBuffer(); CharBuffer tempCharBuffer=CharBuffer.allocate(terminatorCharBuffer.length()); for (int i=0; i < terminatorCharBuffer.length(); i++) tempCharBuffer.put(readCharacter(stream,streamEncoding)); while (!(StringUtils.sequenceEqual(terminatorCharBuffer.array(),tempCharBuffer.array()))) { val ch=readCharacter(stream,streamEncoding); tempCharBuffer=CharBuffer.wrap(tempCharBuffer.subSequence(1,tempCharBuffer.length())).append(ch); } }
Example 22
From project jsfunit, under directory /jboss-jsfunit-analysis/src/main/java/org/jboss/jsfunit/analysis/el/.
Source file: ELExpressionIterator.java

private static CharBuffer fileAsCharBuffer(final File f) throws IOException { final FileChannel fc=new FileInputStream(f).getChannel(); final ByteBuffer bb=fc.map(FileChannel.MapMode.READ_ONLY,0,(int)fc.size()); final CharBuffer cb=Charset.forName("UTF-8").newDecoder().decode(bb); return cb; }
Example 23
private String decodeFolderName(String name) throws CharacterCodingException { try { CharsetDecoder decoder=mModifiedUtf7Charset.newDecoder().onMalformedInput(CodingErrorAction.REPORT); CharBuffer cb=decoder.decode(ByteBuffer.wrap(name.getBytes("US-ASCII"))); return cb.toString(); } catch ( UnsupportedEncodingException uee) { throw new RuntimeException("Unable to decode folder name: " + name,uee); } }
Example 24
From project miso-lims, under directory /core/src/main/java/uk/ac/bbsrc/tgac/miso/core/util/.
Source file: LimsUtils.java

public static Matcher grep(File f,Pattern p) throws IOException { Charset charset=Charset.forName("ISO-8859-15"); CharsetDecoder decoder=charset.newDecoder(); FileInputStream fis=new FileInputStream(f); FileChannel fc=fis.getChannel(); int sz=(int)fc.size(); MappedByteBuffer bb=fc.map(FileChannel.MapMode.READ_ONLY,0,sz); CharBuffer cb=decoder.decode(bb); Matcher m=grep(cb,p); fc.close(); return m; }
Example 25
From project anarchyape, under directory /src/main/java/ape/.
Source file: NetworkDisconnectCommand.java

/** * This method writes to the Stand output */ private boolean writeSTDOut(Process p){ BufferedReader stdInput=new BufferedReader(new InputStreamReader(p.getInputStream())); int count; CharBuffer cbuf=CharBuffer.allocate(99999); try { count=stdInput.read(cbuf); if (count != -1) cbuf.array()[count]='\0'; else if (cbuf.array()[0] != '\0') count=cbuf.array().length; else count=0; for (int i=0; i < count; i++) System.out.print(cbuf.get(i)); } catch ( IOException e) { System.err.println("Writing Stdout in NetworkDisconnectCommand catches an exception, Turn on the VERBOSE flag to see the stack trace"); e.printStackTrace(); return false; } try { stdInput.close(); } catch ( IOException e) { System.err.println("Unable to close the IOStream"); e.printStackTrace(); return false; } return true; }
Example 26
From project android_packages_apps_Gallery3D, under directory /src/com/cooliris/media/.
Source file: GridQuad.java

public GridQuad(boolean generateOrientedQuads){ mOrientedQuad=generateOrientedQuads; if (mOrientedQuad) { mMatrix=new MatrixStack(); mMatrix.glLoadIdentity(); } int vertsAcross=2; int vertsDown=2; mW=vertsAcross; mH=vertsDown; int size=vertsAcross * vertsDown; final int FLOAT_SIZE=4; final int CHAR_SIZE=2; final int orientationCount=(!generateOrientedQuads) ? 1 : ORIENTATION_COUNT; mVertexBuffer=ByteBuffer.allocateDirect(FLOAT_SIZE * size * 3* orientationCount).order(ByteOrder.nativeOrder()).asFloatBuffer(); mOverlayTexCoordBuffer=ByteBuffer.allocateDirect(FLOAT_SIZE * size * 2* orientationCount).order(ByteOrder.nativeOrder()).asFloatBuffer(); mBaseTexCoordBuffer=ByteBuffer.allocateDirect(FLOAT_SIZE * size * 2* orientationCount).order(ByteOrder.nativeOrder()).asFloatBuffer(); int indexCount=INDEX_COUNT; mIndexBuffer=ByteBuffer.allocateDirect(CHAR_SIZE * indexCount * orientationCount).order(ByteOrder.nativeOrder()).asCharBuffer(); CharBuffer buffer=mIndexBuffer; for (int i=0; i < INDEX_COUNT * orientationCount; ++i) { buffer.put(i,(char)i); } mVertBufferIndex=0; }
Example 27
From project c10n, under directory /core/src/main/java/c10n/.
Source file: DefaultC10NMsgFactory.java

private static String readTextFromInputStream(InputStream is) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(is,"UTF8"),1024 * 8); CharBuffer buf=CharBuffer.allocate(64); int read; do { read=br.read(buf); if (read == 0 && !buf.hasRemaining()) { CharBuffer newBuf=CharBuffer.allocate(buf.capacity() * 2); buf.flip(); newBuf.put(buf); buf=newBuf; } } while (read != -1); buf.flip(); return buf.toString(); }
Example 28
From project capedwarf-blue, under directory /cluster-tests/src/test/java/org/jboss/test/capedwarf/cluster/infinispan/.
Source file: AbstractInfinispanClusterTest.java

private void loadData(Cache<String,String> cache) throws IOException { InputStream in=Thread.currentThread().getContextClassLoader().getResourceAsStream(textFileName); InputStreamReader reader=new InputStreamReader(in); try { BufferedReader bufferedReader=new BufferedReader(reader); int chunkSize=10; int chunkId=0; CharBuffer cbuf=CharBuffer.allocate(1024 * chunkSize); while (bufferedReader.read(cbuf) >= 0) { Buffer buffer=cbuf.flip(); String textChunk=buffer.toString(); cache.put(textFileName + (chunkId++),textChunk); cbuf.clear(); if (chunkId % 100 == 0) System.out.printf(" Inserted %s chunks from %s into grid%n",chunkId,textFileName); } } finally { Util.close(reader); Util.close(in); } }
Example 29
From project commons-compress, under directory /src/main/java/org/apache/commons/compress/archivers/zip/.
Source file: NioZipEncoding.java

/** * @see org.apache.commons.compress.archivers.zip.ZipEncoding#encode(java.lang.String) */ public ByteBuffer encode(String name){ CharsetEncoder enc=this.charset.newEncoder(); enc.onMalformedInput(CodingErrorAction.REPORT); enc.onUnmappableCharacter(CodingErrorAction.REPORT); CharBuffer cb=CharBuffer.wrap(name); ByteBuffer out=ByteBuffer.allocate(name.length() + (name.length() + 1) / 2); while (cb.remaining() > 0) { CoderResult res=enc.encode(cb,out,true); if (res.isUnmappable() || res.isMalformed()) { if (res.length() * 6 > out.remaining()) { out=ZipEncodingHelper.growBuffer(out,out.position() + res.length() * 6); } for (int i=0; i < res.length(); ++i) { ZipEncodingHelper.appendSurrogate(out,cb.get()); } } else if (res.isOverflow()) { out=ZipEncodingHelper.growBuffer(out,0); } else if (res.isUnderflow()) { enc.flush(out); break; } } out.limit(out.position()); out.rewind(); return out; }
Example 30
From project core_4, under directory /impl/src/main/java/org/richfaces/util/.
Source file: Util.java

public static String encodeURIQueryPart(String s){ StringBuilder builder=new StringBuilder(); int start=0; int idx=0; int length=s.length(); CharsetEncoder encoder=null; ByteBuffer byteBuffer=null; CharBuffer buffer=null; for (; idx < length; idx++) { char c=s.charAt(idx); if (!isLegalURIQueryChar(c)) { builder.append(s.substring(start,idx)); if (encoder == null) { encoder=Charset.forName("UTF-8").newEncoder(); } if (buffer == null) { buffer=CharBuffer.allocate(1); byteBuffer=ByteBuffer.allocate(6); } else { byteBuffer.limit(6); } buffer.put(0,c); buffer.rewind(); byteBuffer.rewind(); encoder.encode(buffer,byteBuffer,true); byteBuffer.flip(); int limit=byteBuffer.limit(); for (int i=0; i < limit; i++) { int b=(0xFF & byteBuffer.get()); builder.append(escapeURIByte(b)); } start=idx + 1; } } builder.append(s.substring(start,idx)); return builder.toString(); }
Example 31
From project dawn-common, under directory /org.dawb.common.util/src/org/dawb/common/util/io/.
Source file: Grep.java

/** * Search for occurrences of the input pattern in the given file * @param toSearch * @param regExp * @param charsetText * @return * @throws IOException */ public static List<CharSequence> grep(final FileInputStream fis,final String regExp,final String charsetText) throws IOException { final Charset charset=Charset.forName(charsetText); final CharsetDecoder decoder=charset.newDecoder(); final Pattern pattern=Pattern.compile(regExp); final FileChannel fc=fis.getChannel(); final List<CharSequence> ret=new ArrayList<CharSequence>(7); try { int sz=(int)fc.size(); MappedByteBuffer bb=fc.map(FileChannel.MapMode.READ_ONLY,0,sz); CharBuffer cb=decoder.decode(bb); grep(cb,pattern,ret); } finally { fc.close(); } return ret; }
Example 32
From project en4j, under directory /NBPlatformApp/NoteRepositoryH2/src/main/java/com/rubenlaguna/en4j/noterepository/.
Source file: NoteImpl.java

public String getContent(){ final Reader characterStream=getContentAsReader(); if (null == characterStream) { return ""; } CharBuffer cb=CharBuffer.allocate(64000); StringBuilder sb=new StringBuilder(64000); try { while (characterStream.ready()) { cb.clear(); characterStream.read(cb); cb.flip(); sb.append(cb); } } catch ( IOException e) { getLogger().log(Level.WARNING,"caught exception:",e); } finally { try { characterStream.close(); } catch ( IOException e) { } } return sb.toString(); }
Example 33
From project etherpad, under directory /infrastructure/net.appjet.common/util/.
Source file: LenientFormatter.java

/** * Writes a formatted string to the output destination of the {@code Formatter}. * @param l the {@code Locale} used in the method. If {@code locale} is{@code null}, then no localization will be applied. This parameter does not influence the {@code Locale} specified duringconstruction. * @param format a format string. * @param args the arguments list used in the {@code format()} method. If there aremore arguments than those specified by the format string, then the additional arguments are ignored. * @return this {@code Formatter}. * @throws IllegalFormatException if the format string is illegal or incompatible with the arguments, or if fewer arguments are sent than those required by the format string, or any other illegal situation. * @throws FormatterClosedException if the {@code Formatter} has been closed. */ public LenientFormatter format(Locale l,String format,Object... args){ checkClosed(); CharBuffer formatBuffer=CharBuffer.wrap(format); ParserStateMachine parser=new ParserStateMachine(formatBuffer); Transformer transformer=new Transformer(this,l); int currentObjectIndex=0; Object lastArgument=null; boolean hasLastArgumentSet=false; while (formatBuffer.hasRemaining()) { parser.reset(); FormatToken token=parser.getNextFormatToken(); String result; String plainText=token.getPlainText(); if (token.getConversionType() == (char)FormatToken.UNSET) { result=plainText; } else { plainText=plainText.substring(0,plainText.indexOf('%')); Object argument=null; if (token.requireArgument()) { int index=token.getArgIndex() == FormatToken.UNSET ? currentObjectIndex++ : token.getArgIndex(); argument=getArgument(args,index,token,lastArgument,hasLastArgumentSet); lastArgument=argument; hasLastArgumentSet=true; } result=transformer.transform(token,argument); result=(null == result ? plainText : plainText + result); } if (null != result) { try { out.append(result); } catch ( IOException e) { lastIOException=e; } } } return this; }
Example 34
From project evodroid, under directory /src/com/sonorth/evodroid/util/.
Source file: FastXmlSerializer.java

public void flush() throws IOException { if (mPos > 0) { if (mOutputStream != null) { CharBuffer charBuffer=CharBuffer.wrap(mText,0,mPos); CoderResult result=mCharset.encode(charBuffer,mBytes,true); while (true) { if (result.isError()) { throw new IOException(result.toString()); } else if (result.isOverflow()) { flushBytes(); result=mCharset.encode(charBuffer,mBytes,true); continue; } break; } flushBytes(); mOutputStream.flush(); } else { mWriter.write(mText,0,mPos); mWriter.flush(); } mPos=0; } }
Example 35
From project flume, under directory /flume-ng-core/src/main/java/org/apache/flume/source/.
Source file: NetcatSource.java

@Override public void run(){ logger.debug("Starting connection handler"); Event event=null; try { Reader reader=Channels.newReader(socketChannel,"utf-8"); Writer writer=Channels.newWriter(socketChannel,"utf-8"); CharBuffer buffer=CharBuffer.allocate(maxLineLength); buffer.flip(); while (true) { int charsRead=fill(buffer,reader); logger.debug("Chars read = {}",charsRead); int eventsProcessed=processEvents(buffer,writer); logger.debug("Events processed = {}",eventsProcessed); if (charsRead == -1) { break; } else if (charsRead == 0 && eventsProcessed == 0) { if (buffer.remaining() == buffer.capacity()) { logger.warn("Client sent event exceeding the maximum length"); counterGroup.incrementAndGet("events.failed"); writer.write("FAILED: Event exceeds the maximum length (" + buffer.capacity() + " chars, including newline)\n"); writer.flush(); break; } } } socketChannel.close(); counterGroup.incrementAndGet("sessions.completed"); } catch ( IOException e) { counterGroup.incrementAndGet("sessions.broken"); } logger.debug("Connection handler exiting"); }
Example 36
From project flumebase, under directory /src/main/java/com/odiago/flumebase/io/.
Source file: CachingTextEventParser.java

/** * Check whether the cache already contains a value for this column index. If so, put the value in the 'out' parameter. If we've cached the column text but not parsed it, we parse it here, cache the result, and then stick that in the 'out' parameter. * @param colIdx the index of the column into the list * @param expectedType the expected result type * @param out a Ref that will hold the output cached column value, if we haveone. The contents of 'out' are not changed if this method returns false. * @return true if 'out' is populated with a cached result, false if we haveno cached result to use. */ protected boolean lookupCache(int colIdx,Type expectedType,Ref<Object> out) throws ColumnParseException { if (mColumnValues.size() > colIdx) { Object cached=mColumnValues.get(colIdx); if (cached != null) { out.item=cached; return true; } if (mColumnNulls.get(colIdx)) { out.item=null; return true; } } CharBuffer cbCol=mColTexts.size() > colIdx ? mColTexts.get(colIdx) : null; if (null != cbCol) { out.item=parseAndCache(cbCol,colIdx,expectedType); return true; } return false; }
Example 37
/** * Decodes a string by trying several charsets until one does not throw a coding exception. Last resort is to interpret as UTF-8 with illegal character substitution. * @param content * @param charsets optional * @return a string */ public static String decodeString(byte[] content,String... charsets){ Set<String> sets=new LinkedHashSet<String>(); if (!ArrayUtils.isEmpty(charsets)) { sets.addAll(Arrays.asList(charsets)); } String value=null; sets.addAll(Arrays.asList("UTF-8","ISO-8859-1",Charset.defaultCharset().name())); for ( String charset : sets) { try { Charset cs=Charset.forName(charset); CharsetDecoder decoder=cs.newDecoder(); CharBuffer buffer=decoder.decode(ByteBuffer.wrap(content)); value=buffer.toString(); break; } catch ( CharacterCodingException e) { } catch ( IllegalCharsetNameException e) { } catch ( UnsupportedCharsetException e) { } } if (value.startsWith("\uFEFF")) { return value.substring(1); } return value; }
Example 38
From project ha-jdbc, under directory /src/main/java/net/sf/hajdbc/sql/.
Source file: FileSupportImpl.java

/** * @see net.sf.hajdbc.sql.FileSupport#createFile(java.io.Reader) */ @Override public File createFile(Reader reader) throws E { try { File file=this.createTempFile(); Writer writer=new FileWriter(file); try { CharBuffer buffer=CharBuffer.allocate(BUFFER_SIZE); while (reader.read(buffer) > 0) { buffer.flip(); writer.append(buffer); buffer.clear(); } return file; } finally { Resources.close(writer); } } catch ( IOException e) { throw this.exceptionFactory.createException(e); } }
Example 39
From project HeLauncher, under directory /src/mobi/intuitit/android/internal/utils/.
Source file: FastXmlSerializer.java

public void flush() throws IOException { if (mPos > 0) { if (mOutputStream != null) { CharBuffer charBuffer=CharBuffer.wrap(mText,0,mPos); CoderResult result=mCharset.encode(charBuffer,mBytes,true); while (true) { if (result.isError()) { throw new IOException(result.toString()); } else if (result.isOverflow()) { flushBytes(); result=mCharset.encode(charBuffer,mBytes,true); continue; } break; } flushBytes(); mOutputStream.flush(); } else { mWriter.write(mText,0,mPos); mWriter.flush(); } mPos=0; } }
Example 40
From project httpClient, under directory /httpclient/src/main/java/org/apache/http/client/utils/.
Source file: URLEncodedUtils.java

/** * Decode/unescape a portion of a URL, to use with the query part ensure {@code plusAsBlank} is true. * @param content the portion to decode * @param charset the charset to use * @param plusAsBlank if {@code true}, then convert '+' to space (e.g. for www-url-form-encoded content), otherwise leave as is. * @return */ private static String urldecode(final String content,final Charset charset,final boolean plusAsBlank){ if (content == null) { return null; } ByteBuffer bb=ByteBuffer.allocate(content.length()); CharBuffer cb=CharBuffer.wrap(content); while (cb.hasRemaining()) { char c=cb.get(); if (c == '%' && cb.remaining() >= 2) { char uc=cb.get(); char lc=cb.get(); int u=Character.digit(uc,16); int l=Character.digit(lc,16); if (u != -1 && l != -1) { bb.put((byte)((u << 4) + l)); } else { bb.put((byte)'%'); bb.put((byte)uc); bb.put((byte)lc); } } else if (plusAsBlank && c == '+') { bb.put((byte)' '); } else { bb.put((byte)c); } } bb.flip(); return charset.decode(bb).toString(); }
Example 41
From project httpcore, under directory /httpcore/src/main/java/org/apache/http/impl/io/.
Source file: AbstractSessionOutputBuffer.java

/** * Writes characters from the specified string followed by a line delimiter to this session buffer. <p> This method uses CR-LF as a line delimiter. * @param s the line. * @exception IOException if an I/O error occurs. */ public void writeLine(final String s) throws IOException { if (s == null) { return; } if (s.length() > 0) { if (this.ascii) { for (int i=0; i < s.length(); i++) { write(s.charAt(i)); } } else { CharBuffer cbuf=CharBuffer.wrap(s); writeEncoded(cbuf); } } write(CRLF); }
Example 42
From project james-mailbox, under directory /store/src/main/java/org/apache/james/mailbox/store/search/.
Source file: MessageSearcher.java

/** * Is {@link #getSearchContent()} found in the given input? * @param input <code>InputStream</code> containing an email * @return true if the content exists and the stream contains the content,false otherwise * @throws IOException * @throws MimeException */ public boolean isFoundIn(final InputStream input) throws IOException, MimeException { final boolean includeHeaders; final CharSequence searchContent; final boolean isCaseInsensitive; synchronized (this) { includeHeaders=this.includeHeaders; searchContent=this.searchContent; isCaseInsensitive=this.isCaseInsensitive; } final boolean result; if (searchContent == null || "".equals(searchContent)) { final Logger logger=getLogger(); logger.debug("Nothing to search for. "); result=false; } else { final CharBuffer buffer=createBuffer(searchContent,isCaseInsensitive); result=parse(input,isCaseInsensitive,includeHeaders,buffer); } return result; }
Example 43
From project Japid, under directory /src.japid/cn/bran/japid/util/.
Source file: StringUtils.java

/** * try to encode a char[] as fast as possible for later use in outputstream * @param ca * @param off * @param len * @return */ static public ByteBuffer encodeUTF8(String src){ int len=src.length(); int off=0; int en=(int)(len * ce.maxBytesPerChar()); byte[] ba=new byte[en]; if (len == 0) return null; ce.reset(); ByteBuffer bb=ByteBuffer.wrap(ba); CharBuffer cb=CharBuffer.wrap(src,off,len); try { CoderResult cr=ce.encode(cb,bb,true); if (!cr.isUnderflow()) cr.throwException(); cr=ce.flush(bb); if (!cr.isUnderflow()) cr.throwException(); return bb; } catch ( CharacterCodingException x) { throw new Error(x); } }
Example 44
/** * try to encode a char[] as fast as possible for later use in outputstream * @param ca * @param off * @param len * @return */ static public ByteBuffer encodeUTF8(String src){ int len=src.length(); int off=0; int en=(int)(len * ce.maxBytesPerChar()); byte[] ba=new byte[en]; if (len == 0) return null; ce.reset(); ByteBuffer bb=ByteBuffer.wrap(ba); CharBuffer cb=CharBuffer.wrap(src,off,len); try { CoderResult cr=ce.encode(cb,bb,true); if (!cr.isUnderflow()) cr.throwException(); cr=ce.flush(bb); if (!cr.isUnderflow()) cr.throwException(); return bb; } catch ( CharacterCodingException x) { throw new Error(x); } }
Example 45
From project jboss-rmi-api_spec, under directory /src/main/java/org/jboss/com/sun/corba/se/impl/encoding/.
Source file: CodeSetConversion.java

public char[] getChars(byte[] bytes,int offset,int numBytes){ try { ByteBuffer byteBuf=ByteBuffer.wrap(bytes,offset,numBytes); CharBuffer charBuf=btc.decode(byteBuf); resultingNumChars=charBuf.limit(); if (charBuf.limit() == charBuf.capacity()) { buffer=charBuf.array(); } else { buffer=new char[charBuf.limit()]; charBuf.get(buffer,0,charBuf.limit()).position(0); } return buffer; } catch ( IllegalStateException ile) { throw wrapper.btcConverterFailure(ile); } catch ( MalformedInputException mie) { throw wrapper.badUnicodePair(mie); } catch ( UnmappableCharacterException uce) { throw omgWrapper.charNotInCodeset(uce); } catch ( CharacterCodingException cce) { throw wrapper.btcConverterFailure(cce); } }
Example 46
From project jmxmonitor, under directory /jmxmonitor/src/main/java/uk/co/gidley/jmxmonitor/services/.
Source file: ThreadManager.java

@Override public void run(){ logger.info("Stop listener thread working"); try { boolean shutdownRunning=true; while (shutdownRunning) { SocketChannel socketChannel=serverSocketChannel.accept(); dbuf.clear(); socketChannel.read(dbuf); dbuf.flip(); CharBuffer cb=decoder.decode(dbuf); if (cb.toString().equals(stopKey)) { serverSocketChannel.close(); shutdownRunning=false; logger.info("Recieved Stop command"); } } } catch ( ClosedByInterruptException e) { logger.debug("Closing shutdown thread {}",e); } catch ( IOException e) { logger.error("{}",e); throw new RuntimeException(e); } }
Example 47
/** * Reads in file contents. <P> This method is smart and falls back to ISO-8859-1 if the input stream does not seem to be in the specified encoding. * @param input The InputStream to read from. * @param encoding The encoding to assume at first. * @return A String, interpreted in the "encoding", or, if it fails, in Latin1. * @throws IOException If the stream cannot be read or the stream cannot bedecoded (even) in Latin1 */ public static String readContents(InputStream input,String encoding) throws IOException { ByteArrayOutputStream out=new ByteArrayOutputStream(); FileUtil.copyContents(input,out); ByteBuffer bbuf=ByteBuffer.wrap(out.toByteArray()); Charset cset=Charset.forName(encoding); CharsetDecoder csetdecoder=cset.newDecoder(); csetdecoder.onMalformedInput(CodingErrorAction.REPORT); csetdecoder.onUnmappableCharacter(CodingErrorAction.REPORT); try { CharBuffer cbuf=csetdecoder.decode(bbuf); return cbuf.toString(); } catch ( CharacterCodingException e) { Charset latin1=Charset.forName("ISO-8859-1"); CharsetDecoder l1decoder=latin1.newDecoder(); l1decoder.onMalformedInput(CodingErrorAction.REPORT); l1decoder.onUnmappableCharacter(CodingErrorAction.REPORT); try { bbuf=ByteBuffer.wrap(out.toByteArray()); CharBuffer cbuf=l1decoder.decode(bbuf); return cbuf.toString(); } catch ( CharacterCodingException ex) { throw (CharacterCodingException)ex.fillInStackTrace(); } } }
Example 48
From project kuromoji, under directory /src/main/java/org/atilika/kuromoji/trie/.
Source file: DoubleArrayTrie.java

/** * Write to file * @param filename filename * @throws IOException */ public void write(String directoryname) throws IOException { String filename=directoryname + File.separator + FILENAME; baseBuffer.rewind(); checkBuffer.rewind(); tailBuffer.rewind(); File file=new File(filename); if (file.exists()) { file.delete(); } RandomAccessFile raf=new RandomAccessFile(filename,"rw"); FileChannel channel=raf.getChannel(); raf.writeInt(baseBuffer.capacity()); raf.writeInt(tailBuffer.capacity()); ByteBuffer tmpBuffer=ByteBuffer.allocate(baseBuffer.capacity() * 4); IntBuffer tmpIntBuffer=tmpBuffer.asIntBuffer(); tmpIntBuffer.put(baseBuffer); tmpBuffer.rewind(); channel.write(tmpBuffer); tmpBuffer=ByteBuffer.allocate(checkBuffer.capacity() * 4); tmpIntBuffer=tmpBuffer.asIntBuffer(); tmpIntBuffer.put(checkBuffer); tmpBuffer.rewind(); channel.write(tmpBuffer); tmpBuffer=ByteBuffer.allocate(tailBuffer.capacity() * 2); CharBuffer tmpCharBuffer=tmpBuffer.asCharBuffer(); tmpCharBuffer.put(tailBuffer); tmpBuffer.rewind(); channel.write(tmpBuffer); raf.close(); }
Example 49
From project lenya, under directory /org.apache.lenya.core.api/src/main/java/org/apache/lenya/search/.
Source file: Grep.java

/** * Check if the given file contains the pattern * @param file the file which is to be searched for the pattern * @param pattern the pattern that is being searched. * @return true if the file contains the string, false otherwise. * @throws IOException */ public static boolean containsPattern(File file,Pattern pattern) throws IOException { FileChannel fc=null; FileInputStream fis=null; boolean result=false; try { fis=new FileInputStream(file); fc=fis.getChannel(); int sz=(int)fc.size(); MappedByteBuffer bb=fc.map(FileChannel.MapMode.READ_ONLY,0,sz); CharBuffer cb=decoder.decode(bb); Matcher pm=pattern.matcher(cb); result=pm.find(); } catch ( FileNotFoundException e) { logger.error("File not found: " + e.toString()); } catch ( CharacterCodingException e) { logger.error("Problem with encoding: " + e.toString()); } catch ( IOException e) { logger.error("IO Exception: " + e.toString()); } finally { if (fc != null) fc.close(); if (fis != null) fis.close(); } return result; }
Example 50
From project leveldb, under directory /leveldb/src/main/java/org/iq80/leveldb/util/.
Source file: Slices.java

public static String decodeString(ByteBuffer src,Charset charset){ final CharsetDecoder decoder=getDecoder(charset); final CharBuffer dst=CharBuffer.allocate((int)((double)src.remaining() * decoder.maxCharsPerByte())); try { CoderResult cr=decoder.decode(src,dst,true); if (!cr.isUnderflow()) { cr.throwException(); } cr=decoder.flush(dst); if (!cr.isUnderflow()) { cr.throwException(); } } catch ( CharacterCodingException x) { throw new IllegalStateException(x); } return dst.flip().toString(); }
Example 51
From project message-wrapper, under directory /src/main/java/jp/co/worksap/message/wrapper/.
Source file: CharsetGuesser.java

private boolean canDecode(InputStream input,Charset charset) throws IOException { ReadableByteChannel channel=Channels.newChannel(input); CharsetDecoder decoder=charset.newDecoder(); ByteBuffer byteBuffer=ByteBuffer.allocate(SIZE * 2); CharBuffer charBuffer=CharBuffer.allocate(SIZE); boolean endOfInput=false; while (!endOfInput) { int n=channel.read(byteBuffer); byteBuffer.flip(); endOfInput=(n == -1); CoderResult coderResult=decoder.decode(byteBuffer,charBuffer,endOfInput); if (coderResult.isError()) { return false; } charBuffer.clear(); while (coderResult == CoderResult.OVERFLOW) { coderResult=decoder.decode(byteBuffer,charBuffer,endOfInput); charBuffer.clear(); } byteBuffer.compact(); } CoderResult coderResult; while ((coderResult=decoder.flush(charBuffer)) == CoderResult.OVERFLOW) { charBuffer.clear(); } if (coderResult.isError()) { return false; } return true; }
Example 52
/** * Create a sort key for a given unicode string. The sort key can be compared instead of the original strings and will compare based on the sorting represented by this Sort class. Using a sort key is more efficient if many comparisons are being done (for example if you are sorting a list of strings). * @param object This is saved in the sort key for later retrieval and plays no part in the sorting. * @param s The string for which the sort key is to be created. * @param second Secondary sort key. * @param cache A cache for the created keys. This is for saving memory so it is essential that thisis managed by the caller. * @return A sort key. */ public <T>SortKey<T> createSortKey(T object,String s,int second,Map<String,byte[]> cache){ byte[] key; if (cache != null) { key=cache.get(s); if (key != null) return new SrtSortKey<T>(object,key,second); } CharBuffer inb=CharBuffer.wrap(s); try { ByteBuffer out=encoder.encode(inb); byte[] bval=out.array(); key=new byte[(bval.length + 1 + 2) * 3]; try { fillCompleteKey(bval,key); } catch ( ArrayIndexOutOfBoundsException e) { key=new byte[bval.length * 3 * maxExpSize + 3]; } if (cache != null) cache.put(s,key); return new SrtSortKey<T>(object,key,second); } catch ( CharacterCodingException e) { return new SrtSortKey<T>(object,ZERO_KEY); } }
Example 53
From project alljoyn_java, under directory /src/org/alljoyn/bus/.
Source file: BusAttachment.java

/** * Convert to UTF-8 for native code. This is intended for sensitive string data (i.e. passwords). The native code must take care of scrubbing the buffer when done. This method can be called from a listener object and must therefore be MT-Safe. * @param charArray the sensitive string * @return the UTF-8 encoded version of the string */ static byte[] encode(char[] charArray){ try { Charset charset=Charset.forName("UTF-8"); CharsetEncoder encoder=charset.newEncoder(); ByteBuffer bb=encoder.encode(CharBuffer.wrap(charArray)); byte[] ba=new byte[bb.limit()]; bb.get(ba); return ba; } catch ( CharacterCodingException ex) { BusException.log(ex); return null; } }
Example 54
From project AmDroid, under directory /httpclientandroidlib/src/ch/boye/httpclientandroidlib/entity/mime/.
Source file: HttpMultipart.java

private static ByteArrayBuffer encode(final Charset charset,final String string){ ByteBuffer encoded=charset.encode(CharBuffer.wrap(string)); ByteArrayBuffer bab=new ByteArrayBuffer(encoded.remaining()); bab.append(encoded.array(),encoded.position(),encoded.remaining()); return bab; }
Example 55
From project ARCInputFormat, under directory /src/org/commoncrawl/util/shared/.
Source file: TextBytes.java

/** * Converts the provided String to bytes using the UTF-8 encoding. If <code>replace</code> is true, then malformed input is replaced with the substitution character, which is U+FFFD. Otherwise the method throws a MalformedInputException. * @return ByteBuffer: bytes stores at ByteBuffer.array() and length isByteBuffer.limit() */ public static ByteBuffer encode(String string,boolean replace) throws CharacterCodingException { CharsetEncoder encoder=ENCODER_FACTORY.get(); if (replace) { encoder.onMalformedInput(CodingErrorAction.REPLACE); encoder.onUnmappableCharacter(CodingErrorAction.REPLACE); } ByteBuffer bytes=encoder.encode(CharBuffer.wrap(string.toCharArray())); if (replace) { encoder.onMalformedInput(CodingErrorAction.REPORT); encoder.onUnmappableCharacter(CodingErrorAction.REPORT); } return bytes; }
Example 56
From project ceres, under directory /ceres-launcher/src/main/java/com/bc/ceres/util/.
Source file: TemplateReader.java

/** * Attempts to read characters into the specified character buffer. The buffer is used as a repository of characters as-is: the only changes made are the results of a put operation. No flipping or rewinding of the buffer is performed. * @param target the buffer to read characters into * @return The number of characters added to the buffer, or-1 if this source of characters is at its end * @throws java.io.IOException if an I/O error occurs * @throws NullPointerException if target is null * @throws java.nio.ReadOnlyBufferException if target is a read only buffer */ @Override public int read(CharBuffer target) throws IOException { synchronized (lock) { int len=target.remaining(); char[] cbuf=new char[len]; int n=read(cbuf,0,len); if (n > 0) { target.put(cbuf,0,n); } return n; } }
Example 57
From project ciel-java, under directory /examples/Grep/src/skywriting/examples/grep/.
Source file: Text.java

/** * Converts the provided String to bytes using the UTF-8 encoding. If <code>replace</code> is true, then malformed input is replaced with the substitution character, which is U+FFFD. Otherwise the method throws a MalformedInputException. * @return ByteBuffer: bytes stores at ByteBuffer.array() and length is ByteBuffer.limit() */ public static ByteBuffer encode(String string,boolean replace) throws CharacterCodingException { CharsetEncoder encoder=ENCODER_FACTORY.get(); if (replace) { encoder.onMalformedInput(CodingErrorAction.REPLACE); encoder.onUnmappableCharacter(CodingErrorAction.REPLACE); } ByteBuffer bytes=encoder.encode(CharBuffer.wrap(string.toCharArray())); if (replace) { encoder.onMalformedInput(CodingErrorAction.REPORT); encoder.onUnmappableCharacter(CodingErrorAction.REPORT); } return bytes; }
Example 58
From project cocos2d, under directory /cocos2d-android/src/com/badlogic/gdx/utils/.
Source file: BufferUtils.java

private static int positionInBytes(Buffer dst){ if (dst instanceof ByteBuffer) return dst.position(); else if (dst instanceof ShortBuffer) return dst.position() << 1; else if (dst instanceof CharBuffer) return dst.position() << 1; else if (dst instanceof IntBuffer) return dst.position() << 2; else if (dst instanceof LongBuffer) return dst.position() << 3; else if (dst instanceof FloatBuffer) return dst.position() << 2; else if (dst instanceof DoubleBuffer) return dst.position() << 3; else throw new GdxRuntimeException("Can't copy to a " + dst.getClass().getName() + " instance"); }
Example 59
From project Collections, under directory /src/main/java/vanilla/java/collections/model/.
Source file: Enumerated16FieldModel.java

public void set(CharBuffer array,int index,T value){ Character ordinal=map.get(value); if (ordinal == null) { final int size=map.size(); OUTER: do { for (; addPosition < map.size(); addPosition++) { if (list.get(addPosition) == null) { ordinal=addEnumValue(value,addPosition); break OUTER; } } ordinal=addEnumValue(value,size); } while (false); addPosition++; } array.put(index,ordinal); }
Example 60
From project commons-io, under directory /src/main/java/org/apache/commons/io/input/.
Source file: CharSequenceInputStream.java

/** * Constructor. * @param s the input character sequence * @param charset the character set name to use * @param bufferSize the buffer size to use. */ public CharSequenceInputStream(final CharSequence s,final Charset charset,int bufferSize){ super(); this.encoder=charset.newEncoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE); this.bbuf=ByteBuffer.allocate(bufferSize); this.bbuf.flip(); this.cbuf=CharBuffer.wrap(s); this.mark=-1; }
Example 61
From project connectbot, under directory /src/org/apache/harmony/niochar/charset/additional/.
Source file: IBM437.java

protected CoderResult decodeLoop(ByteBuffer bb,CharBuffer cb){ int cbRemaining=cb.remaining(); if (bb.hasArray() && cb.hasArray()) { int rem=bb.remaining(); rem=cbRemaining >= rem ? rem : cbRemaining; byte[] bArr=bb.array(); char[] cArr=cb.array(); int bStart=bb.position(); int cStart=cb.position(); int i; for (i=bStart; i < bStart + rem; i++) { char in=(char)(bArr[i] & 0xFF); if (in >= 26) { int index=(int)in - 26; cArr[cStart++]=(char)arr[index]; } else { cArr[cStart++]=(char)(in & 0xFF); } } bb.position(i); cb.position(cStart); if (rem == cbRemaining && bb.hasRemaining()) return CoderResult.OVERFLOW; } else { while (bb.hasRemaining()) { if (cbRemaining == 0) return CoderResult.OVERFLOW; char in=(char)(bb.get() & 0xFF); if (in >= 26) { int index=(int)in - 26; cb.put(arr[index]); } else { cb.put((char)(in & 0xFF)); } cbRemaining--; } } return CoderResult.UNDERFLOW; }
Example 62
From project dcm4che, under directory /dcm4che-core/src/main/java/org/dcm4che/data/.
Source file: SpecificCharacterSet.java

public boolean encode(CharBuffer cb,ByteBuffer bb,boolean escSeq,CodingErrorAction errorAction){ encoder.onMalformedInput(errorAction).onUnmappableCharacter(errorAction).reset(); int cbmark=cb.position(); int bbmark=bb.position(); try { if (escSeq) escSeq(bb,codec.getEscSeq1()); CoderResult cr=encoder.encode(cb,bb,true); if (!cr.isUnderflow()) cr.throwException(); cr=encoder.flush(bb); if (!cr.isUnderflow()) cr.throwException(); } catch ( CharacterCodingException x) { cb.position(cbmark); bb.position(bbmark); return false; } return true; }
Example 63
From project Easy-Cassandra, under directory /src/main/java/org/easycassandra/util/.
Source file: EncodingUtil.java

/** * return a ByteBuffer from String * @param msg mensage * @return the ByteBuffer within String */ public static ByteBuffer stringToByte(String msg){ if (msg == null) { return null; } try { return ENCODER.encode(CharBuffer.wrap(msg)); } catch ( Exception exception) { Logger.getLogger(EncodingUtil.class.getName()).log(Level.SEVERE,null,exception); } return null; }
Example 64
public CharCollector(Charset charset,Appendable output){ buffer=CharBuffer.allocate(BUF_SIZE); this.output=output; this.decoder=charset.newDecoder(); this.decoder.reset(); }
Example 65
From project heritrix3, under directory /commons/src/main/java/org/archive/io/.
Source file: GenericReplayCharSequence.java

/** * Converts the first <code>Integer.MAX_VALUE</code> characters from the file <code>backingFilename</code> from encoding <code>encoding</code> to encoding <code>WRITE_ENCODING</code> and saves as <code>this.decodedFile</code>, which is named <code>backingFilename + "." + WRITE_ENCODING</code>. * @throws IOException */ protected void decode(InputStream inStream,int prefixMax,String backingFilename,Charset charset) throws IOException { this.charset=charset; BufferedReader reader=new BufferedReader(new InputStreamReader(inStream,charset)); logger.fine("backingFilename=" + backingFilename + " encoding="+ charset+ " decodedFile="+ decodedFile); this.prefixBuffer=CharBuffer.allocate(prefixMax); long count=0; while (count < prefixMax) { int read=reader.read(prefixBuffer); if (read < 0) { break; } count+=read; } int ch=reader.read(); if (ch >= 0) { count++; this.decodedFile=new File(backingFilename + "." + WRITE_ENCODING); FileOutputStream fos; try { fos=new FileOutputStream(this.decodedFile); } catch ( FileNotFoundException e) { System.gc(); System.runFinalization(); this.decodedFile=new File(decodedFile.getAbsolutePath() + ".win"); logger.info("Windows 'file with a user-mapped section open' " + "workaround gc/finalization/name-extension performed."); fos=new FileOutputStream(this.decodedFile); } Writer writer=new OutputStreamWriter(fos,WRITE_ENCODING); writer.write(ch); count+=IOUtils.copyLarge(reader,writer); writer.close(); reader.close(); } this.length=Ints.saturatedCast(count); if (count > Integer.MAX_VALUE) { logger.warning("input stream is longer than Integer.MAX_VALUE=" + NumberFormat.getInstance().format(Integer.MAX_VALUE) + " characters -- only first "+ NumberFormat.getInstance().format(Integer.MAX_VALUE)+ " are accessible through this GenericReplayCharSequence"); } logger.fine("decode: decoded " + count + " characters"+ ((decodedFile == null) ? "" : " (" + (count - prefixBuffer.length()) + " to "+ decodedFile+ ")")); }
Example 66
From project ideavim, under directory /src/com/maddyhome/idea/vim/regexp/.
Source file: CharPointer.java

public String substring(int len){ if (end()) { return ""; } else { int start=pointer; int end=normalize(pointer + len); int slen=seq.length(); return CharBuffer.wrap(seq,start,end).toString(); } }
Example 67
private static ByteBuffer toNative(CharsetEncoder encoder,final CharSequence value,final ByteBuffer buf){ buf.mark(); try { encoder.reset(); encoder.encode(CharBuffer.wrap(value),buf,true); encoder.flush(buf); nulTerminate(encoder,buf); } finally { buf.reset(); } return buf; }
Example 68
From project james-mime4j, under directory /core/src/main/java/org/apache/james/mime4j/util/.
Source file: ContentUtil.java

/** * Encodes the specified string into an immutable sequence of bytes using the specified charset. * @param charset Java charset to be used for the conversion. * @param string string to encode. * @return encoded string as an immutable sequence of bytes. */ public static ByteSequence encode(Charset charset,String string){ if (string == null) { return null; } if (charset == null) { charset=Charset.defaultCharset(); } ByteBuffer encoded=charset.encode(CharBuffer.wrap(string)); ByteArrayBuffer buf=new ByteArrayBuffer(encoded.remaining()); buf.append(encoded.array(),encoded.position(),encoded.remaining()); return buf; }
Example 69
From project JGit, under directory /org.spearce.jgit/src/org/spearce/jgit/treewalk/.
Source file: AbstractTreeIterator.java

/** * Create a new iterator with no parent and a prefix. <p> The prefix path supplied is inserted in front of all paths generated by this iterator. It is intended to be used when an iterator is being created for a subsection of an overall repository and needs to be combined with other iterators that are created to run over the entire repository namespace. * @param prefix position of this iterator in the repository tree. The value may be null or the empty string to indicate the prefix is the root of the repository. A trailing slash ('/') is automatically appended if the prefix does not end in '/'. */ protected AbstractTreeIterator(final String prefix){ parent=null; if (prefix != null && prefix.length() > 0) { final ByteBuffer b; b=Constants.CHARSET.encode(CharBuffer.wrap(prefix)); pathLen=b.limit(); path=new byte[Math.max(DEFAULT_PATH_SIZE,pathLen + 1)]; b.get(path,0,pathLen); if (path[pathLen - 1] != '/') path[pathLen++]='/'; pathOffset=pathLen; } else { path=new byte[DEFAULT_PATH_SIZE]; pathOffset=0; } }
Example 70
From project jmd, under directory /src/org/apache/commons/io/input/.
Source file: ProxyReader.java

/** * Invokes the delegate's <code>read(CharBuffer)</code> method. * @param target the char buffer to read the characters into * @return the number of characters read or -1 if the end of stream * @throws IOException if an I/O error occurs * @since Commons IO 2.0 */ @Override public int read(CharBuffer target) throws IOException { try { beforeRead(target != null ? target.length() : 0); int n=in.read(target); afterRead(n); return n; } catch ( IOException e) { handleIOException(e); return -1; } }
Example 71
public Simple(CharSequence sequence){ try { this.chars=sequence; this.bytes=UTF_8.newEncoder().encode(CharBuffer.wrap(chars)).array(); } catch ( CharacterCodingException e) { throw new UndeclaredThrowableException(e); } }
Example 72
From project kevoree-library, under directory /javase/org.kevoree.library.javase.logger.greg/src/main/java/org/greg/client/.
Source file: Greg.java

private static boolean writeRecordsBatchTo(OutputStream stream) throws IOException { int maxBatchSize=10000; DataOutput w=new LittleEndianDataOutputStream(stream); byte[] cidBytes=conf.clientId.getBytes("utf-8"); w.writeInt(cidBytes.length); w.write(cidBytes); int recordsWritten=0; byte[] machineBytes=hostname.getBytes("utf-8"); CharsetEncoder enc=Charset.forName("utf-8").newEncoder(); ByteBuffer maxMsg=ByteBuffer.allocate(1); for ( Record rec : records) { w.writeInt(1); w.writeLong(rec.timestamp.toUtcNanos()); w.writeInt(machineBytes.length); w.write(machineBytes); int maxLen=rec.message.length() * 2; if (maxLen > maxMsg.limit()) { maxMsg=ByteBuffer.allocate(maxLen); } enc.reset(); enc.encode(CharBuffer.wrap(rec.message),maxMsg,true); maxMsg.position(0); w.writeInt(maxMsg.limit()); w.write(maxMsg.array(),maxMsg.arrayOffset(),maxMsg.limit()); if (++recordsWritten == maxBatchSize) break; } w.writeInt(0); stream.flush(); for (int i=0; i < recordsWritten; ++i) { records.remove(); } numRecords.addAndGet(-recordsWritten); Trace.writeLine("Written batch of " + recordsWritten + " records to greg"); return recordsWritten < maxBatchSize; }
Example 73
From project milton, under directory /milton/milton-api/src/main/java/com/bradmcevoy/http/.
Source file: Utils.java

private static String _percentEncode(String s){ int n=s.length(); if (n == 0) { return s; } String ns=normalize(s); ByteBuffer bb=null; bb=Charset.forName("UTF-8").encode(CharBuffer.wrap(ns)); StringBuilder sb=new StringBuilder(); while (bb.hasRemaining()) { int b=bb.get() & 0xff; if (isUnReserved(b)) { sb.append((char)b); } else { appendEscape(sb,(byte)b); } } return sb.toString(); }
Example 74
From project milton2, under directory /milton-api/src/main/java/io/milton/common/.
Source file: Utils.java

private static String _percentEncode(String s){ int n=s.length(); if (n == 0) { return s; } String ns=normalize(s); ByteBuffer bb=null; bb=Charset.forName("UTF-8").encode(CharBuffer.wrap(ns)); StringBuilder sb=new StringBuilder(); while (bb.hasRemaining()) { int b=bb.get() & 0xff; if (isUnReserved(b)) { sb.append((char)b); } else { appendEscape(sb,(byte)b); } } return sb.toString(); }