use of java.io.CharArrayReader in project lucene-solr by apache.
the class XSLTResponseWriter method write.
@Override
public void write(Writer writer, SolrQueryRequest request, SolrQueryResponse response) throws IOException {
final Transformer t = getTransformer(request);
// capture the output of the XMLWriter
final CharArrayWriter w = new CharArrayWriter();
XMLWriter.writeResponse(w, request, response);
// and write transformed result to our writer
final Reader r = new BufferedReader(new CharArrayReader(w.toCharArray()));
final StreamSource source = new StreamSource(r);
final StreamResult result = new StreamResult(writer);
try {
t.transform(source, result);
} catch (TransformerException te) {
throw new IOException("XSLT transformation error", te);
}
}
use of java.io.CharArrayReader in project android_frameworks_base by crdroidandroid.
the class SettingsBackupAgent method restoreWifiSupplicant.
private void restoreWifiSupplicant(String filename, byte[] bytes, int size) {
try {
WifiNetworkSettings supplicantImage = new WifiNetworkSettings();
File supplicantFile = new File(FILE_WIFI_SUPPLICANT);
if (supplicantFile.exists()) {
// Retain the existing APs; we'll append the restored ones to them
BufferedReader in = new BufferedReader(new FileReader(FILE_WIFI_SUPPLICANT));
supplicantImage.readNetworks(in, null, true);
in.close();
supplicantFile.delete();
}
// Incorporate the restore AP information
if (size > 0) {
char[] restoredAsBytes = new char[size];
for (int i = 0; i < size; i++) restoredAsBytes[i] = (char) bytes[i];
BufferedReader in = new BufferedReader(new CharArrayReader(restoredAsBytes));
supplicantImage.readNetworks(in, null, false);
if (DEBUG_BACKUP) {
Log.v(TAG, "Final AP list:");
supplicantImage.dump();
}
}
// Install the correct default template
BufferedWriter bw = new BufferedWriter(new FileWriter(FILE_WIFI_SUPPLICANT));
copyWifiSupplicantTemplate(bw);
// Write the restored supplicant config and we're done
supplicantImage.write(bw);
bw.close();
} catch (IOException ioe) {
Log.w(TAG, "Couldn't restore " + filename);
}
}
use of java.io.CharArrayReader in project nifi by apache.
the class TestJdbcCommon method testClob.
@Test
public void testClob() throws Exception {
try (final Statement stmt = con.createStatement()) {
stmt.executeUpdate("CREATE TABLE clobtest (id INT, text CLOB(64 K))");
stmt.execute("INSERT INTO clobtest VALUES (41, NULL)");
PreparedStatement ps = con.prepareStatement("INSERT INTO clobtest VALUES (?, ?)");
ps.setInt(1, 42);
final char[] buffer = new char[4002];
IntStream.range(0, 4002).forEach((i) -> buffer[i] = String.valueOf(i % 10).charAt(0));
// Put a zero-byte in to test the buffer building logic
buffer[1] = 0;
ReaderInputStream isr = new ReaderInputStream(new CharArrayReader(buffer), Charset.defaultCharset());
// - set the value of the input parameter to the input stream
ps.setAsciiStream(2, isr, 4002);
ps.execute();
isr.close();
final ResultSet resultSet = stmt.executeQuery("select * from clobtest");
final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
JdbcCommon.convertToAvroStream(resultSet, outStream, false);
final byte[] serializedBytes = outStream.toByteArray();
assertNotNull(serializedBytes);
// Deserialize bytes to records
final InputStream instream = new ByteArrayInputStream(serializedBytes);
final DatumReader<GenericRecord> datumReader = new GenericDatumReader<>();
try (final DataFileStream<GenericRecord> dataFileReader = new DataFileStream<>(instream, datumReader)) {
GenericRecord record = null;
while (dataFileReader.hasNext()) {
// Reuse record object by passing it to next(). This saves us from
// allocating and garbage collecting many objects for files with
// many items.
record = dataFileReader.next(record);
Integer id = (Integer) record.get("ID");
Object o = record.get("TEXT");
if (id == 41) {
assertNull(o);
} else {
assertNotNull(o);
final String text = o.toString();
assertEquals(4002, text.length());
// Third character should be '2'
assertEquals('2', text.charAt(2));
}
}
}
}
}
use of java.io.CharArrayReader in project derby by apache.
the class StreamingColumnTest method testDerby500.
/**
* Streams are not re-used. This test tests the fix for DERBY-500. If an
* update statement has multiple rows that is affected, and one of the
* parameter values is a stream, the update will fail because streams are
* not re-used.
*/
public void testDerby500() throws Exception {
Statement stmt;
println("======================================");
println("START DERBY-500 TEST ");
stmt = createStatement();
setAutoCommit(false);
PreparedStatement ps = prepareStatement("insert into test500 " + "values (?,?,?,?,?)");
// insert 10 rows.
int rowCount = 0;
// use blob and clob values
int len = 10000;
byte[] buf = new byte[len];
char[] cbuf = new char[len];
char orig = 'c';
for (int i = 0; i < len; i++) {
buf[i] = (byte) orig;
cbuf[i] = orig;
}
int randomOffset = 9998;
buf[randomOffset] = (byte) 'e';
cbuf[randomOffset] = 'e';
println("Inserting rows ");
for (int i = 0; i < 10; i++) {
ps.setInt(1, i);
ps.setString(2, "mname" + i);
ps.setInt(3, 0);
ps.setBinaryStream(4, new ByteArrayInputStream(buf), len);
ps.setAsciiStream(5, new ByteArrayInputStream(buf), len);
rowCount += ps.executeUpdate();
}
commit();
println("Rows inserted =" + rowCount);
PreparedStatement pss = prepareStatement(" select chardata,bytedata from test500 where id = ?");
verifyDerby500Test(pss, buf, cbuf, 0, 10, true);
// do the update, update must qualify more than 1 row and update
// will fail as currently we don't allow stream values to be re-used
PreparedStatement psu = prepareStatement("update test500 set bytedata = ? " + ", chardata = ? where mvalue = ? ");
buf[randomOffset + 1] = (byte) 'u';
cbuf[randomOffset + 1] = 'u';
rowCount = 0;
println("Update qualifies many rows + streams");
try {
psu.setBinaryStream(1, new ByteArrayInputStream(buf), len);
psu.setCharacterStream(2, new CharArrayReader(cbuf), len);
psu.setInt(3, 0);
rowCount += psu.executeUpdate();
println("DERBY500 #1 Rows updated =" + rowCount);
fail("Attempting to reuse stream should have thrown an exception!");
} catch (SQLException sqle) {
assertSQLState("XJ001", sqle);
println("EXPECTED EXCEPTION - streams cannot be re-used");
rollback();
}
// verify data
// set back buffer value to what was inserted.
buf[randomOffset + 1] = (byte) orig;
cbuf[randomOffset + 1] = orig;
verifyDerby500Test(pss, buf, cbuf, 0, 10, true);
PreparedStatement psu2 = prepareStatement("update test500 set " + "bytedata = ? , chardata = ? where id = ? ");
buf[randomOffset + 1] = (byte) 'u';
cbuf[randomOffset + 1] = 'u';
rowCount = 0;
psu2.setBinaryStream(1, new ByteArrayInputStream(buf), len);
psu2.setAsciiStream(2, new ByteArrayInputStream(buf), len);
psu2.setInt(3, 0);
rowCount += psu2.executeUpdate();
println("DERBY500 #2 Rows updated =" + rowCount);
commit();
verifyDerby500Test(pss, buf, cbuf, 0, 1, true);
// delete, as currently we dont allow stream values to be re-used
PreparedStatement psd = prepareStatement("delete from test500 where " + "mvalue = ?");
rowCount = 0;
psd.setInt(1, 0);
rowCount += psd.executeUpdate();
rowCount += psd.executeUpdate();
println("DERBY500 #3 Rows deleted =" + rowCount);
commit();
// verify data
verifyDerby500Test(pss, buf, cbuf, 0, 10, true);
PreparedStatement psd2 = prepareStatement("delete from test500 " + "where id = ?");
rowCount = 0;
psd2.setInt(1, 0);
rowCount += psd2.executeUpdate();
println("DERBY500 #4 Rows deleted =" + rowCount);
commit();
verifyDerby500Test(pss, buf, cbuf, 1, 2, true);
try {
ps.setInt(1, 11);
rowCount += ps.executeUpdate();
fail("Attempting to reuse stream should have thrown an exception!");
} catch (SQLException sqle) {
if (usingDerbyNetClient()) {
// DERBY-4315. This SQLState is wrong for client.
// It should throw XJ001 like embedded.
// Also client inserts bad data.
// Remove special case when DERBY-4315
// is fixed.
assertSQLState("XN017", sqle);
} else {
assertSQLState("XJ001", sqle);
println("EXPECTED EXCEPTION - streams cannot be re-used");
}
rollback();
}
commit();
stmt.close();
pss.close();
psu2.close();
psu.close();
psd.close();
psd2.close();
println("END DERBY-500 TEST ");
println("======================================");
}
use of java.io.CharArrayReader in project derby by apache.
the class ParameterMappingTest method helperTestDerby6237.
// numberOfRowsToUpdate - value 1 or 2
// testVariation - if 1 then update CLOB/VARCHAR with short data
// if 2 then update CLOB/VARCHAR with large data
// testCLOB - true means test setCharacterStream on CLOB
// - false means test setCharacterStream on VARCHAR
private void helperTestDerby6237(int numberOfRowsToUpdate, int testVariation, boolean testCLOB) throws Exception {
CharAlphabet a1 = CharAlphabet.singleChar('a');
// Following will update one or 2 rows depending on the 1st param
// Following will update CLOB column or VARCHAR column with short
// or large data depending on param 2
// Following will update CLOB column or VARCHAR column depending
// on 3rd param
PreparedStatement ps = prepareStatement("UPDATE TestUpdateCharStream SET " + (testCLOB == true ? "c3" : "c4") + " = ?, " + "c2 = c2 + 1 WHERE c1 IN (?, ?)");
switch(testVariation) {
case 1:
// test short data
ps.setCharacterStream(1, new LoopingAlphabetReader(50, a1), 50);
break;
case 2:
// test large data
if (testCLOB) {
// for CLOB column, use 50K data
ps.setCharacterStream(1, new LoopingAlphabetReader(50000, a1), 50000);
} else {
// for VARCHAR column, use 32K data
ps.setCharacterStream(1, new LoopingAlphabetReader(32000, a1), 32000);
}
break;
}
// First value in IN clause is getting set to 'AAAAA'
// Using setCharacterStream on VARCHAR to set the value
ps.setCharacterStream(2, new CharArrayReader("AAAAA".toCharArray()), 5);
if (numberOfRowsToUpdate == 1) {
// Second value in IN clause is also getting set to 'AAAAA', which
// means prepared statement will update only one row
ps.setObject(3, "AAAAA", Types.VARCHAR);
} else {
// Second value in IN clause is also getting set to 'EEEEE', which
// means prepared statement will update two rows
ps.setObject(3, "EEEEE", Types.VARCHAR);
}
ps.execute();
// verify updated data. Update happened to either CLOB column or VARCHAR
// column. It is decided by param 3
ResultSet rs;
ps = prepareStatement("select " + (testCLOB == true ? "c3 " : "c4 ") + "from TestUpdateCharStream " + "WHERE c1 IN (?, ?)");
ps.setCharacterStream(1, new CharArrayReader("AAAAA".toCharArray()), 5);
if (numberOfRowsToUpdate == 1) {
ps.setObject(2, "AAAAA", Types.VARCHAR);
} else {
ps.setObject(2, "EEEEE", Types.VARCHAR);
}
rs = ps.executeQuery();
char[] c;
if (testVariation == 1) {
// we are here to test short data
c = new char[50];
} else {
// we are here to test large data
if (testCLOB)
c = new char[50000];
else
c = new char[32000];
}
Arrays.fill(c, 'a');
for (int i = 0; i < numberOfRowsToUpdate; i++) {
rs.next();
if (!compareClobReader2CharArray(c, rs.getCharacterStream(1))) {
System.out.println("FAIL: " + (testCLOB ? "CLOB " : "VARCHAR ") + "data should have matched");
rs.close();
ps.close();
return;
}
}
rs.close();
ps.close();
}
Aggregations