Streaming ArrayList of POJOs using FasterXML
I'm currently developing a Napster-like application. I have two static methods that I use for sending information between the client and central server. On any client connect a thread on the server accepts the incoming connection and once connected the client application sends its user data and a list of files described in the ArrayList param passed into this function:
/**
* Send an ArrayList of FileInfo objects to a socket.
* @param fout Output BufferedWriter
* @param fileInfoArrayList ArrayList of FileInfo objects to send
*/
public static void sendFileInfoArrayList(BufferedWriter fout, ArrayList<FileInfo> fileInfoArrayList) {
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
objectMapper.writeValue(fout, fileInfoArrayList);
} catch (IOException e) {
e.printStackTrace();
}
}
The server thread uses a similar method to receive information about the recently connected client:
/**
* Receive an ArrayList of FileInfo objects from a socket.
* @param fin Input BufferedReader
* @return ArrayList of FileInfo objects received
*/
public static ArrayList<FileInfo> recvFileInfoArrayList(BufferedReader fin) {
ArrayList<FileInfo> fileInfoArrayList = new ArrayList<>();
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
CollectionType javaType = objectMapper.getTypeFactory()
.constructCollectionType(ArrayList.class, FileInfo.class);
fileInfoArrayList = objectMapper.readValue(fin, javaType);
} catch (IOException e) {
e.printStackTrace();
}
return fileInfoArrayList;
}
The next step is the central server thread enters a loop on the condition that the socket connection is maintained waiting to receive a search term from the client. I was intending on just using BufferedReader.readLine()
on the server and sending the search term java.lang.String
(with a CRLF appended) using DataOutputStream.writeBytes()
.
However, when it enters the loop (after the initial exchange of user/fileinfo (which is successful)) readLine()
just keeps reading null
over and over. I'm unsure why this is. I need the connection from client to central server to be persistent, hence objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false)
, so closing it out isn't an option. I've tried flush()
ing the writer in my static send method above, shutdownOutput()
on the out socket
. I can't identify the root of the problem I'm experiencing.
java sockets fasterxml
add a comment |
I'm currently developing a Napster-like application. I have two static methods that I use for sending information between the client and central server. On any client connect a thread on the server accepts the incoming connection and once connected the client application sends its user data and a list of files described in the ArrayList param passed into this function:
/**
* Send an ArrayList of FileInfo objects to a socket.
* @param fout Output BufferedWriter
* @param fileInfoArrayList ArrayList of FileInfo objects to send
*/
public static void sendFileInfoArrayList(BufferedWriter fout, ArrayList<FileInfo> fileInfoArrayList) {
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
objectMapper.writeValue(fout, fileInfoArrayList);
} catch (IOException e) {
e.printStackTrace();
}
}
The server thread uses a similar method to receive information about the recently connected client:
/**
* Receive an ArrayList of FileInfo objects from a socket.
* @param fin Input BufferedReader
* @return ArrayList of FileInfo objects received
*/
public static ArrayList<FileInfo> recvFileInfoArrayList(BufferedReader fin) {
ArrayList<FileInfo> fileInfoArrayList = new ArrayList<>();
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
CollectionType javaType = objectMapper.getTypeFactory()
.constructCollectionType(ArrayList.class, FileInfo.class);
fileInfoArrayList = objectMapper.readValue(fin, javaType);
} catch (IOException e) {
e.printStackTrace();
}
return fileInfoArrayList;
}
The next step is the central server thread enters a loop on the condition that the socket connection is maintained waiting to receive a search term from the client. I was intending on just using BufferedReader.readLine()
on the server and sending the search term java.lang.String
(with a CRLF appended) using DataOutputStream.writeBytes()
.
However, when it enters the loop (after the initial exchange of user/fileinfo (which is successful)) readLine()
just keeps reading null
over and over. I'm unsure why this is. I need the connection from client to central server to be persistent, hence objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false)
, so closing it out isn't an option. I've tried flush()
ing the writer in my static send method above, shutdownOutput()
on the out socket
. I can't identify the root of the problem I'm experiencing.
java sockets fasterxml
Use a single BufferedReader to read everything. Otherwise, when you tell the first buffered reader to read the JSON, it reads and buffers all the lines that are sent after the JSON.
– JB Nizet
Nov 21 '18 at 17:34
@JBNizet I've implemented these suggested changes in my code, tested, and received the same results. I've updated the post, because I agree it's probably better to pass in the BufferedReader than the Socket.
– wanderbread
Nov 21 '18 at 17:47
add a comment |
I'm currently developing a Napster-like application. I have two static methods that I use for sending information between the client and central server. On any client connect a thread on the server accepts the incoming connection and once connected the client application sends its user data and a list of files described in the ArrayList param passed into this function:
/**
* Send an ArrayList of FileInfo objects to a socket.
* @param fout Output BufferedWriter
* @param fileInfoArrayList ArrayList of FileInfo objects to send
*/
public static void sendFileInfoArrayList(BufferedWriter fout, ArrayList<FileInfo> fileInfoArrayList) {
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
objectMapper.writeValue(fout, fileInfoArrayList);
} catch (IOException e) {
e.printStackTrace();
}
}
The server thread uses a similar method to receive information about the recently connected client:
/**
* Receive an ArrayList of FileInfo objects from a socket.
* @param fin Input BufferedReader
* @return ArrayList of FileInfo objects received
*/
public static ArrayList<FileInfo> recvFileInfoArrayList(BufferedReader fin) {
ArrayList<FileInfo> fileInfoArrayList = new ArrayList<>();
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
CollectionType javaType = objectMapper.getTypeFactory()
.constructCollectionType(ArrayList.class, FileInfo.class);
fileInfoArrayList = objectMapper.readValue(fin, javaType);
} catch (IOException e) {
e.printStackTrace();
}
return fileInfoArrayList;
}
The next step is the central server thread enters a loop on the condition that the socket connection is maintained waiting to receive a search term from the client. I was intending on just using BufferedReader.readLine()
on the server and sending the search term java.lang.String
(with a CRLF appended) using DataOutputStream.writeBytes()
.
However, when it enters the loop (after the initial exchange of user/fileinfo (which is successful)) readLine()
just keeps reading null
over and over. I'm unsure why this is. I need the connection from client to central server to be persistent, hence objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false)
, so closing it out isn't an option. I've tried flush()
ing the writer in my static send method above, shutdownOutput()
on the out socket
. I can't identify the root of the problem I'm experiencing.
java sockets fasterxml
I'm currently developing a Napster-like application. I have two static methods that I use for sending information between the client and central server. On any client connect a thread on the server accepts the incoming connection and once connected the client application sends its user data and a list of files described in the ArrayList param passed into this function:
/**
* Send an ArrayList of FileInfo objects to a socket.
* @param fout Output BufferedWriter
* @param fileInfoArrayList ArrayList of FileInfo objects to send
*/
public static void sendFileInfoArrayList(BufferedWriter fout, ArrayList<FileInfo> fileInfoArrayList) {
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
objectMapper.writeValue(fout, fileInfoArrayList);
} catch (IOException e) {
e.printStackTrace();
}
}
The server thread uses a similar method to receive information about the recently connected client:
/**
* Receive an ArrayList of FileInfo objects from a socket.
* @param fin Input BufferedReader
* @return ArrayList of FileInfo objects received
*/
public static ArrayList<FileInfo> recvFileInfoArrayList(BufferedReader fin) {
ArrayList<FileInfo> fileInfoArrayList = new ArrayList<>();
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
CollectionType javaType = objectMapper.getTypeFactory()
.constructCollectionType(ArrayList.class, FileInfo.class);
fileInfoArrayList = objectMapper.readValue(fin, javaType);
} catch (IOException e) {
e.printStackTrace();
}
return fileInfoArrayList;
}
The next step is the central server thread enters a loop on the condition that the socket connection is maintained waiting to receive a search term from the client. I was intending on just using BufferedReader.readLine()
on the server and sending the search term java.lang.String
(with a CRLF appended) using DataOutputStream.writeBytes()
.
However, when it enters the loop (after the initial exchange of user/fileinfo (which is successful)) readLine()
just keeps reading null
over and over. I'm unsure why this is. I need the connection from client to central server to be persistent, hence objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false)
, so closing it out isn't an option. I've tried flush()
ing the writer in my static send method above, shutdownOutput()
on the out socket
. I can't identify the root of the problem I'm experiencing.
java sockets fasterxml
java sockets fasterxml
edited Nov 21 '18 at 18:37
wanderbread
asked Nov 21 '18 at 17:17
wanderbreadwanderbread
12
12
Use a single BufferedReader to read everything. Otherwise, when you tell the first buffered reader to read the JSON, it reads and buffers all the lines that are sent after the JSON.
– JB Nizet
Nov 21 '18 at 17:34
@JBNizet I've implemented these suggested changes in my code, tested, and received the same results. I've updated the post, because I agree it's probably better to pass in the BufferedReader than the Socket.
– wanderbread
Nov 21 '18 at 17:47
add a comment |
Use a single BufferedReader to read everything. Otherwise, when you tell the first buffered reader to read the JSON, it reads and buffers all the lines that are sent after the JSON.
– JB Nizet
Nov 21 '18 at 17:34
@JBNizet I've implemented these suggested changes in my code, tested, and received the same results. I've updated the post, because I agree it's probably better to pass in the BufferedReader than the Socket.
– wanderbread
Nov 21 '18 at 17:47
Use a single BufferedReader to read everything. Otherwise, when you tell the first buffered reader to read the JSON, it reads and buffers all the lines that are sent after the JSON.
– JB Nizet
Nov 21 '18 at 17:34
Use a single BufferedReader to read everything. Otherwise, when you tell the first buffered reader to read the JSON, it reads and buffers all the lines that are sent after the JSON.
– JB Nizet
Nov 21 '18 at 17:34
@JBNizet I've implemented these suggested changes in my code, tested, and received the same results. I've updated the post, because I agree it's probably better to pass in the BufferedReader than the Socket.
– wanderbread
Nov 21 '18 at 17:47
@JBNizet I've implemented these suggested changes in my code, tested, and received the same results. I've updated the post, because I agree it's probably better to pass in the BufferedReader than the Socket.
– wanderbread
Nov 21 '18 at 17:47
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417430%2fstreaming-arraylist-of-pojos-using-fasterxml%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417430%2fstreaming-arraylist-of-pojos-using-fasterxml%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Use a single BufferedReader to read everything. Otherwise, when you tell the first buffered reader to read the JSON, it reads and buffers all the lines that are sent after the JSON.
– JB Nizet
Nov 21 '18 at 17:34
@JBNizet I've implemented these suggested changes in my code, tested, and received the same results. I've updated the post, because I agree it's probably better to pass in the BufferedReader than the Socket.
– wanderbread
Nov 21 '18 at 17:47