use of java.nio.charset.CharsetEncoder in project wigle-wifi-wardriving by wiglenet.
the class HttpFileUploader method upload.
/**
* upload utility method.
*
* @param urlString the url to POST the file to
* @param filename the filename to use for the post
* @param fileParamName the HTML form field name for the file
* @param fileInputStream an open file stream to the file to post
* @param params form data fields (key and value)
* @param handler if non-null gets empty messages with updates on progress
* @param filesize guess at filesize for UI callbacks
*/
public static String upload(final String urlString, final String filename, final String fileParamName, final FileInputStream fileInputStream, final Map<String, String> params, final PreConnectConfigurator preConnectConfigurator, final Handler handler, final long filesize) throws IOException {
String retval = null;
HttpURLConnection conn = null;
try {
final boolean setBoundary = true;
conn = AbstractApiRequest.connect(urlString, setBoundary, preConnectConfigurator, ApiDownloader.REQUEST_POST);
if (conn == null) {
throw new IOException("No connection for: " + urlString);
}
OutputStream connOutputStream = conn.getOutputStream();
// reflect out the chunking info
for (Method meth : connOutputStream.getClass().getMethods()) {
// MainActivity.info("meth: " + meth.getName() );
try {
if ("isCached".equals(meth.getName()) || "isChunked".equals(meth.getName())) {
Boolean val = (Boolean) meth.invoke(connOutputStream, (Object[]) null);
MainActivity.info(meth.getName() + " " + val);
} else if ("size".equals(meth.getName())) {
Integer val = (Integer) meth.invoke(connOutputStream, (Object[]) null);
MainActivity.info(meth.getName() + " " + val);
}
} catch (Exception ex) {
// this block is just for logging, so don't splode if it has a problem
MainActivity.error("ex: " + ex, ex);
}
}
WritableByteChannel wbc = Channels.newChannel(connOutputStream);
// find a better guess. it was 281 for me in the field 2010/05/16 -hck
StringBuilder header = new StringBuilder(400);
for (Map.Entry<String, String> entry : params.entrySet()) {
header.append(TWO_HYPHENS).append(AbstractApiRequest.BOUNDARY).append(LINE_END);
header.append("Content-Disposition: form-data; name=\"").append(entry.getKey()).append("\"").append(LINE_END);
header.append(LINE_END);
header.append(entry.getValue());
header.append(LINE_END);
}
header.append(TWO_HYPHENS + AbstractApiRequest.BOUNDARY + LINE_END);
header.append("Content-Disposition: form-data; name=\"").append(fileParamName).append("\";filename=\"").append(filename).append("\"").append(LINE_END);
header.append("Content-Type: application/octet_stream" + LINE_END);
header.append(LINE_END);
MainActivity.info("About to write headers, length: " + header.length());
CharsetEncoder enc = Charset.forName(ENCODING).newEncoder();
CharBuffer cbuff = CharBuffer.allocate(1024);
ByteBuffer bbuff = ByteBuffer.allocate(1024);
writeString(wbc, header.toString(), enc, cbuff, bbuff);
MainActivity.info("Headers are written, length: " + header.length());
int percentTimesTenDone = (header.length() * 1000) / (int) filesize;
if (handler != null && percentTimesTenDone >= 0) {
handler.sendEmptyMessage(BackgroundGuiHandler.WRITING_PERCENT_START + percentTimesTenDone);
}
FileChannel fc = fileInputStream.getChannel();
long byteswritten = 0;
final int chunk = 16 * 1024;
while (byteswritten < filesize) {
final long bytes = fc.transferTo(byteswritten, chunk, wbc);
if (bytes <= 0) {
MainActivity.info("giving up transferring file. bytes: " + bytes);
break;
}
byteswritten += bytes;
MainActivity.info("transferred " + byteswritten + " of " + filesize);
percentTimesTenDone = ((int) byteswritten * 1000) / (int) filesize;
if (handler != null && percentTimesTenDone >= 0) {
handler.sendEmptyMessage(BackgroundGuiHandler.WRITING_PERCENT_START + percentTimesTenDone);
}
}
MainActivity.info("done. transferred " + byteswritten + " of " + filesize);
// send multipart form data necesssary after file data...
// clear()
header.setLength(0);
header.append(LINE_END);
header.append(TWO_HYPHENS + AbstractApiRequest.BOUNDARY + TWO_HYPHENS + LINE_END);
writeString(wbc, header.toString(), enc, cbuff, bbuff);
// close streams
MainActivity.info("File is written");
wbc.close();
fc.close();
fileInputStream.close();
int responseCode = conn.getResponseCode();
MainActivity.info("connection response code: " + responseCode);
// read the response
final InputStream is = getInputStream(conn);
int ch;
final StringBuilder b = new StringBuilder();
final byte[] buffer = new byte[1024];
while ((ch = is.read(buffer)) != -1) {
b.append(new String(buffer, 0, ch, ENCODING));
}
retval = b.toString();
// MainActivity.info( "Response: " + retval );
} finally {
if (conn != null) {
MainActivity.info("conn disconnect");
conn.disconnect();
}
}
return retval;
}
use of java.nio.charset.CharsetEncoder in project wigle-wifi-wardriving by wiglenet.
the class ObservationUploader method writeFileWithCursor.
/**
* (lifted directly from FileUploaderTask)
* @param fos
* @param bundle
* @param countStats
* @param cursor
* @return
* @throws IOException
* @throws PackageManager.NameNotFoundException
* @throws InterruptedException
*/
@SuppressLint("SimpleDateFormat")
private long writeFileWithCursor(final OutputStream fos, final Bundle bundle, final ObservationUploader.CountStats countStats, final Cursor cursor) throws IOException, PackageManager.NameNotFoundException, InterruptedException {
final SharedPreferences prefs = context.getSharedPreferences(ListFragment.SHARED_PREFS, 0);
long maxId = prefs.getLong(ListFragment.PREF_DB_MARKER, 0L);
final long start = System.currentTimeMillis();
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
countStats.lineCount = 0;
final int total = cursor.getCount();
long fileWriteMillis = 0;
long netMillis = 0;
sendBundledMessage(Status.WRITING.ordinal(), bundle);
final PackageManager pm = context.getPackageManager();
final PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
// name, version, header
final String header = "WigleWifi-1.4" + ",appRelease=" + pi.versionName + ",model=" + android.os.Build.MODEL + ",release=" + android.os.Build.VERSION.RELEASE + ",device=" + android.os.Build.DEVICE + ",display=" + android.os.Build.DISPLAY + ",board=" + android.os.Build.BOARD + ",brand=" + android.os.Build.BRAND + "\n" + "MAC,SSID,AuthMode,FirstSeen,Channel,RSSI,CurrentLatitude,CurrentLongitude,AltitudeMeters,AccuracyMeters,Type\n";
writeFos(fos, header);
// assume header is all byte per char
countStats.byteCount = header.length();
if (total > 0) {
CharBuffer charBuffer = CharBuffer.allocate(1024);
// this ensures hasArray() is true
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
final CharsetEncoder encoder = Charset.forName(MainActivity.ENCODING).newEncoder();
// don't stop when a goofy character is found
encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
final NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
// no commas in the comma-separated file
numberFormat.setGroupingUsed(false);
if (numberFormat instanceof DecimalFormat) {
final DecimalFormat dc = (DecimalFormat) numberFormat;
dc.setMaximumFractionDigits(16);
}
final StringBuffer stringBuffer = new StringBuffer();
final FieldPosition fp = new FieldPosition(NumberFormat.INTEGER_FIELD);
final Date date = new Date();
// loop!
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
if (wasInterrupted()) {
throw new InterruptedException("we were interrupted");
}
// _id,bssid,level,lat,lon,time
final long id = cursor.getLong(0);
if (id > maxId) {
maxId = id;
}
final String bssid = cursor.getString(1);
final long netStart = System.currentTimeMillis();
final Network network = dbHelper.getNetwork(bssid);
netMillis += System.currentTimeMillis() - netStart;
if (network == null) {
// weird condition, skipping
MainActivity.error("network not in database: " + bssid);
continue;
}
countStats.lineCount++;
String ssid = network.getSsid();
if (ssid.contains(COMMA)) {
// comma isn't a legal ssid character, but just in case
ssid = ssid.replaceAll(COMMA, "_");
}
// ListActivity.debug("writing network: " + ssid );
// reset the buffers
charBuffer.clear();
byteBuffer.clear();
// fill in the line
try {
charBuffer.append(network.getBssid());
charBuffer.append(COMMA);
// ssid can be unicode
charBuffer.append(ssid);
charBuffer.append(COMMA);
charBuffer.append(network.getCapabilities());
charBuffer.append(COMMA);
date.setTime(cursor.getLong(7));
singleCopyDateFormat(dateFormat, stringBuffer, charBuffer, fp, date);
charBuffer.append(COMMA);
Integer channel = network.getChannel();
if (channel == null) {
channel = network.getFrequency();
}
singleCopyNumberFormat(numberFormat, stringBuffer, charBuffer, fp, channel);
charBuffer.append(COMMA);
singleCopyNumberFormat(numberFormat, stringBuffer, charBuffer, fp, cursor.getInt(2));
charBuffer.append(COMMA);
singleCopyNumberFormat(numberFormat, stringBuffer, charBuffer, fp, cursor.getDouble(3));
charBuffer.append(COMMA);
singleCopyNumberFormat(numberFormat, stringBuffer, charBuffer, fp, cursor.getDouble(4));
charBuffer.append(COMMA);
singleCopyNumberFormat(numberFormat, stringBuffer, charBuffer, fp, cursor.getDouble(5));
charBuffer.append(COMMA);
singleCopyNumberFormat(numberFormat, stringBuffer, charBuffer, fp, cursor.getDouble(6));
charBuffer.append(COMMA);
charBuffer.append(network.getType().name());
charBuffer.append(NEWLINE);
} catch (BufferOverflowException ex) {
MainActivity.info("buffer overflow: " + ex, ex);
// double the buffer
charBuffer = CharBuffer.allocate(charBuffer.capacity() * 2);
byteBuffer = ByteBuffer.allocate(byteBuffer.capacity() * 2);
// try again
cursor.moveToPrevious();
continue;
}
// tell the encoder to stop here and to start at the beginning
charBuffer.flip();
// do the encoding
encoder.reset();
encoder.encode(charBuffer, byteBuffer, true);
try {
encoder.flush(byteBuffer);
} catch (IllegalStateException ex) {
MainActivity.error("exception flushing: " + ex, ex);
continue;
}
// byteBuffer = encoder.encode( charBuffer ); (old way)
// figure out where in the byteBuffer to stop
final int end = byteBuffer.position();
final int offset = byteBuffer.arrayOffset();
// if ( end == 0 ) {
// if doing the encode without giving a long-term byteBuffer (old way), the output
// byteBuffer position is zero, and the limit and capacity are how long to write for.
// end = byteBuffer.limit();
// }
// MainActivity.info("buffer: arrayOffset: " + byteBuffer.arrayOffset() + " limit: "
// + byteBuffer.limit()
// + " capacity: " + byteBuffer.capacity() + " pos: " + byteBuffer.position() +
// " end: " + end
// + " result: " + result );
final long writeStart = System.currentTimeMillis();
fos.write(byteBuffer.array(), offset, end + offset);
fileWriteMillis += System.currentTimeMillis() - writeStart;
countStats.byteCount += end;
// update UI
final int percentDone = (countStats.lineCount * 1000) / total;
sendPercentTimesTen(percentDone, bundle);
}
}
MainActivity.info("wrote file in: " + (System.currentTimeMillis() - start) + "ms. fileWriteMillis: " + fileWriteMillis + " netmillis: " + netMillis);
return maxId;
}
use of java.nio.charset.CharsetEncoder in project ant by apache.
the class NioZipEncoding method encode.
/**
* @see org.apache.tools.zip.ZipEncoding#encode(java.lang.String)
*/
public ByteBuffer encode(final String name) {
final CharsetEncoder enc = this.charset.newEncoder();
enc.onMalformedInput(CodingErrorAction.REPORT);
enc.onUnmappableCharacter(CodingErrorAction.REPORT);
final CharBuffer cb = CharBuffer.wrap(name);
ByteBuffer out = ByteBuffer.allocate(name.length() + (name.length() + 1) / 2);
while (cb.remaining() > 0) {
final CoderResult res = enc.encode(cb, out, true);
if (res.isUnmappable() || res.isMalformed()) {
// pseudo-URL encoding style to ByteBuffer.
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;
}
use of java.nio.charset.CharsetEncoder in project validator by validator.
the class TextMessageEmitter method newOutputStreamWriter.
private static Writer newOutputStreamWriter(OutputStream out) {
CharsetEncoder enc = Charset.forName("UTF-8").newEncoder();
enc.onMalformedInput(CodingErrorAction.REPLACE);
enc.onUnmappableCharacter(CodingErrorAction.REPLACE);
return new OutputStreamWriter(out, enc);
}
use of java.nio.charset.CharsetEncoder in project validator by validator.
the class Serializer method newOutputStreamWriter.
private static Writer newOutputStreamWriter(OutputStream out) {
CharsetEncoder enc = Charset.forName("UTF-8").newEncoder();
enc.onMalformedInput(CodingErrorAction.REPLACE);
enc.onUnmappableCharacter(CodingErrorAction.REPLACE);
return new OutputStreamWriter(out, enc);
}
Aggregations