Java Random unicode character is appearing when using socket requesting - java

public final static String api = "api.hypixel.net";
public final static String toGet = "/skyblock/auctions";
public static void main(String[] args) {
try {
SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().createSocket(api, 443);
socket.setSoTimeout(500);
PrintWriter pw = new PrintWriter(socket.getOutputStream());
pw.println("GET " + toGet + "?page=" + 1 + " HTTP/1.1");
pw.println("Host: " + api);
pw.println("");
pw.flush();
System.out.println("Wrote Socket: " + 1);
BufferedReader bufRead = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
String e;
try {
while ((e = bufRead.readLine()) != null) {
sb.append(e);
}
} catch (SocketTimeoutException easports) {
//
}
System.out.println("finished write");
bufRead.close();
pw.close();
String[] splitted = sb.toString().split("\\{");
String copyofArr = String.join("{", Arrays.copyOfRange(splitted, 1, splitted.length));
String formatted = "{" + copyofArr.substring(0, copyofArr.length() - 1);
FileOutputStream fos = new FileOutputStream("gg1ay.txt");
fos.write(formatted.getBytes());
fos.flush();
fos.close();
JsonObject array = JsonParser.parseString(formatted).getAsJsonObject();
array.entrySet().forEach(entry -> System.out.println(entry.getKey() + " : " + entry.getValue()));
System.out.println("Got result: " + 1);
} catch (IOException e) {
System.err.println("an Exception");
e.printStackTrace();
}
}
When I request with this code, an weird alphabet comes with output (so i failed to parse json). it looks like unicode char, So I attached \u, now it shows weird chinese alphabet (like 耀, 翸, 磛). How can I fix this code?

I don't know why its working, but i fixed this problem with downgrading http version to 1.0.

Related

The content of the file when re-accessing the database is incorrect

Faced such a problem. I wrote data to a file and on the first attempt everything is written well, but subsequent ones output Null. With what it can be connected ?
public static String ExportTofile (boolean archive) throws Exception {
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
throw new Exception("Нет доступа к SDCard");}
File sdFile;
File sdPath = Environment.getExternalStorageDirectory();
sdPath = new File(sdPath.getAbsolutePath() + "/" + GlobalVars.DirectoryPath);
if (archive) {
sdFile = new File(sdPath +".txt");
} else
sdFile = new File(GlobalVars.AppPathData(), GlobalVars.DirectoryFileName);
Cursor cDeliveryTable;
try {
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(sdFile),
"windows-1251"));
Log.d(GlobalVars.LOG_TAG, "ExpotToFile: " + sdFile.getAbsolutePath());
cDeliveryTable = GlobalVars.db.rawQuery("select * from CustDeliveryTable order by _id", null);
cDeliveryTable.moveToFirst();
while (!cDeliveryTable.isAfterLast()) {
CustDeliveryTable st = new CustDeliveryTable(cDeliveryTable.getLong(cDeliveryTable.getColumnIndex("_id")));
br.write(st.CargoStr() + "\r\n");
cDeliveryTable.moveToNext();}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
throw e;
} catch (IOException e) {
e.printStackTrace();
throw e;}
return sdFile.getAbsolutePath();}
public String CargoStr() {
String sLine;
sLine = this.Name
+ ";"
+ this.Address
+ ";"
+ this.Number
+ ";";
return sLine.substring(0, sLine.length() - 1);}
First write to file
Subsequent entries

How to convert from org.apache.commons.net.ftp.FTPFile to String?

I'm retrieving files list from FTPClient using ftpClient.listFiles(); and it returns an array with correct file names and appropriate quantity.
I'm trying to read content from files, but only ftpClient.retrieveFileStream(fileName) is available for that purpose. And it somehow breaks after reading the first file and returns null.
Is there a way to convert FTPFile directly into String?
ftp.connect();
ftp.changeWorkingDirectoryToFrom();
List<String> idsList = new ArrayList<String>();
List<String> names = ftp.listFileNames();
for (String fileName : names) {
String content = fromFTPFileToString(fileName);
Matcher matcher = FILES_PATTERN.matcher(content);
String id = extractId(content);
if (matcher.find()) {
boolean duplicate = idsList.contains(id);
LOG.info("MATCHED: " + fileName);
if (!duplicate) {
ftp.moveFileFromTo(fileName);
idsList.add(id);
} else {
LOG.info("DUPLICATE: " + fileName);
duplicated++;
ftp.deleteFileOnFromFtp(fileName);
}
}
processed++;
}
ftp.disconnect();
private String fromFTPFileToString(String fileName) {
String content = "";
try {
InputStream is = ftp.readContentFromFTPFile(fileName);
StringWriter writer = new StringWriter();
IOUtils.copy(is, writer, ENCODING);
content = writer.toString();
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(writer);
} catch (Exception ex) {
LOG.error(ex);
}
return content;
}
void deleteFileOnFromFtp(String fileName) {
changeWorkingDirectory(properties.getProperty(PropertiesType.FOLDER_FROM.toString()));
deleteFile(fileName);
}
InputStream readContentFromFTPFile(String fileName) {
changeWorkingDirectory(properties.getProperty(PropertiesType.FOLDER_FROM.toString()));
InputStream inputStream = null;
try {
inputStream = ftpClient.retrieveFileStream(fileName);
} catch (IOException ex) {
LOG.error("Unable to extract content from file:" + O_Q + fileName + C_Q);
}
return inputStream;
}
void moveFileFromTo(String fileName) {
String from = FORWARD_SLASH + properties.getProperty(PropertiesType.FOLDER_FROM.toString()) + FORWARD_SLASH + fileName;
String to = FORWARD_SLASH + properties.getProperty(PropertiesType.FOLDER_TO.toString()) + FORWARD_SLASH + fileName;
try {
ftpClient.rename(from, to);
} catch (IOException ex) {
LOG.error("Unable to move file file from:" + O_Q + from + C_Q + " to: " + O_Q + to + C_Q);
throw new RuntimeException(ex.getCause());
}
}
3 Hints
1) You can use retrieveFile
And use a ByteArrayOutputStream as the 2nd parameter.
To get a String from it simply "new String(baos.toByteArray(),[The Charset you used]);"
2) Check for ftpClient.completePendingCommand();
If some commands are still pending
3) I had once a similar issue setting ftp.enterLocalPassiveMode() helped

Java GBK enconding and PHP

The following code is a PHP one I need to convert to Java to write it to a socket connection
$info = chr(6).chr(0).chr(255).chr(255).'info';
On PHP it works perfectly with this code
$Socket = fsockopen($this->Host, $this->Port, $errno, $errstr, 5);
fwrite($Socket, chr(6).chr(0).chr(255).chr(255).'info');
I tried doing this
public static void main(String[] args) {
char a = 6;
char b = 0;
char c = 255;
try {
Socket test = new Socket("shadowcores.twifysoft.net", 7171);
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(test.getOutputStream()));
write.write(Character.toString(a) + "." + Character.toString(b) + "." + Character.toString(c) + "." + Character.toString(c) + "." + "info");
BufferedReader read = new BufferedReader(new InputStreamReader(test.getInputStream()));
System.out.println(read.read());
test.close();
} catch (IOException e) {
e.printStackTrace();
}
}
With no success because I should be getting some information from the server after writing that message to it
How can I convert that PHP string to Java one?

losing data while writing through asynchronousFileChannel in java

I am trying to use asynchronousFileChannel to write the date into a text file. I made 3 jar file of the program with the AsynchronousFileChannel and compiled all 3 jars simultaneously through command prompt to read 3 different text files and output to one common temporary file
I have 2000 records in my test files(3) to be read,but the output in the common temporary file is missing some of the records,the output should have 6000 records but it shows only 5366 or 5666 or sometimes less than that.
I am not able to figure out why some data is lost as it is the functionality of a asynchronousFileChannel.
Here is the code for the java program using asynchronousfilechannel.
class Writer(){
public void writeOut(ReadableData fileData)
throws InterruptedException {
Path file = null;
AsynchronousFileChannel asynchFileChannel = null;
String filePath = tempFileName;
try {
file = Paths.get(filePath);
asynchFileChannel = AsynchronousFileChannel.open(file,
StandardOpenOption.WRITE, StandardOpenOption.CREATE);
CompletionHandler<Integer, Object> handler = new CompletionHandler<Integer, Object>() {
#Override
public void completed(Integer result, Object attachment) {
if (result == Integer.MAX_VALUE) {
log.debug("Attachment: " + attachment + " " + result
+ " bytes written");
log.debug("CompletionHandler Thread ID: "
+ Thread.currentThread().getId());
}
result++;
}
#Override
public void failed(Throwable e, Object attachment) {
try {
throw e;
} catch (Throwable e1) {
e1.printStackTrace();
}
log.debug("File Write Failed Exception:");
e.printStackTrace();
}
};
String printData = fileData.getId() + "|"
+ fileData.getName() + "|" + fileData.getEmpId()
+ "|" + fileData.getServieId() + "|" + "\n";
asynchFileChannel.write(ByteBuffer.wrap(printData.getBytes()),
asynchFileChannel.size(), "file write", handler);
log.debug(printData);
}
}
catch (IOException e) {
e.printStackTrace();
log.error(e.getMessage());
} finally {
}
}
}
}
and this is my class to read data from 3 files:
public class FileReader1 {
static Logger log = Logger.getLogger(FileHandlerNorthBoundMain.class
.getName());
Writer wrO=new Writer();
public static void main(String[] args) throws IOException,
IllegalFileFormatException, InterruptedException {
String filePath = "C:\\Users\\Public\\testdata1.csv"; //"C:\\Users\\Public\\testdata2.csv"; "C:\\Users\\Public\\testdata3.csv";
File file = new File(filePath);
log.info("Fetching data.... from: " + filePath);
ArrayList<ReadableData> list = new ArrayList<ReadableData>();
FileInputStream fs = null;
BufferedReader reader = null;
String Name;
int Id, EmpId, ServiceId;
ReadableData readableData = null;
int count = 0;
fs = new FileInputStream(file);
reader = new BufferedReader(new InputStreamReader(fs));
String line = reader.readLine();
while (line != null) {
StringTokenizer st = new StringTokenizer(line, "\\|");
while (st.hasMoreTokens()) {
try {
Id = Integer.parseInt(st.nextToken());
Name = st.nextToken();
EmpId = Integer.parseInt(st.nextToken());
ServiceId = Integer.parseInt(st.nextToken());
readableData = new ReadableData(Id,
, Name, EmpId,ServiceId);
wrO.writeOut(readableData);
list.add(count, readableData);
count = count++;
} catch (Exception ex) {
log.error("Illegal File Format");
throw new IllegalFileFormatException("Illegal File Format");
}
}
line = reader.readLine();
}
reader.close();
}
Modify your Writer class with the following code part with asynchronousFileChannel lock()
byte[] test = printData.getBytes();
Future<FileLock> featureLock = asynchFileChannel.lock();
log.info("Waiting for the file to be locked ...");
FileLock lock = featureLock.get();
if (lock.isValid()) {
log.debug(printData);
Future<Integer> featureWrite = asynchFileChannel.write(
ByteBuffer.wrap(test), asynchFileChannel.size());
log.info("Waiting for the bytes to be written ...");
int written = featureWrite.get();
log.info("I’ve written " + written + " bytes into "
+ file.getFileName() + " locked file!");
lock.release();
}
This might be because asynchronousFileChannel is thread safe but Bytebuffer is not,care should be taken to ensure that the buffer is not accessed until after the operation has completed.
check the documentation http://openjdk.java.net/projects/nio/javadoc/java/nio/channels/AsynchronousFileChannel.html

Stream closed showing up in play framework 1.2.5

i have an application that want to write a file using fileoutputstream
here's the code, method patch
public static Response patch() {
try {
System.out.println("PATCH");
System.out.println(request.contentType);
String file = params.get("filename");
System.out.println("patch file: " + file);
Map<String, Header> MapOffset = request.headers;
for (Entry<String, Header> entry : MapOffset.entrySet()) {
System.out.println("Header['" + entry.getKey() + "]: "
+ entry.getValue().value());
}
Header offsetParam = MapOffset.get("offset");
Long offset = 0L;
if (offsetParam != null) {
offset = Long.parseLong(offsetParam.value());
}
InputStream input = request.body;
File f = new File(UPLOAD_DIR + System.getProperty("file.separator")
+ file);
System.out.println("address: " + f.getAbsolutePath());
System.out.println("offset: " + offset);
System.out.println("length: " + f.length());
fileBasicUpload(f, offset, input);
Response respon = new Response();
respon.status = OK;
return respon;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
and this is where i write a file
private static void fileBasicUpload(File f, Long offset, InputStream input)
throws IOException {
FileOutputStream output = null;
try {
int c = -1;
byte[] b = new byte[1024];
try {
output = new FileOutputStream(f, true);
while ((c = input.read(b)) != -1) {
output.write(b, 0, c);
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
} finally {
output.close();
}
}
but when my application called, then stream closed error is show up at while ((c = input.read(b)) != -1) that line.
i don't know how that error is called. sorry for my poor english and thanks
i found the answer. in my application i found like this
public static Response upload(File file){
System.out.println("Appliaction.upload");
response = ResumableUpload.post();
return response;
// render(response);
}
the parameter file, it must be delete, then it work!

Categories

Resources