json-simple not parsing complete json - json-simple

I have a very long JSON which I need to parse using Google's json-simple APIs. However, json-simple APIs is not parsing the complete JSON string. Below is my Java code to parse the JSON. The output does not have complete JSON string.
Is there any limitation with json-simple APIs on parsing JSONs? Or am I missing something in my code?
Please help! Many thanks!
Regards,
Vinayak
Java Code:
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
public class JSONParser {
private static final String JSON_DATA = "{\"order\":{\"entityErrors\":[],\"buyerInfo\":{\"id\":\"7b70f71b-6337-4726-befe-c6cf50c8f413\",\"isGuest\":false,\"associateInfo\":{\"shouldApplyAssociateDiscount\":true},\"primaryContact\":{\"name\":{\"completeName\":\"Yugesh Gundapaneni\",\"firstName\":\"Yugesh\",\"lastName\":\"Gundapaneni\"},\"phone\":{\"completeNumber\":\"408-660-5027\"},\"email\":{\"emailAddress\":\"ygundapaneni#abc.com\"}}},\"groupOrderNo\":\"1414451219356\",\"orderNo\":\"1414451219356\",\"orderType\":\"DOMESTIC\",\"orderDate\":\"2014-10-08T21:11:21Z\",\"orderLines\":[{\"seller\":{\"catalogSellerId\":0,\"partnerDisplayName\":\"abc.com\"},\"primeLineNo\":1,\"orderedQty\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":2.00},\"fulfillmentType\":\"S2S\",\"shippingMethod\":\"STORE_DELIVERY\",\"giftMessage\":{\"giftMessage\":\" GIFT\"},\"orderProduct\":{\"offerId\":{\"offerId\":\"539A698868664B2DB87ECB061D328722\",\"upc\":\"0005436107672\",\"wupc\":\"0005436107672\",\"USItemId\":10154038,\"USSellerId\":0},\"productName\":\"Stansport G-8-10 Folding Stadium Seat with Arms\",\"offerAttributes\":{\"isMailReturnable\":true,\"isPerishable\":false,\"wmDeptNo\":\"74\"}},\"unitPrice\":{\"currencyAmount\":20.00,\"currencyUnit\":\"USD\"},\"shipToAddress\":{\"address\":{\"addressLineOne\":\"600 showers drive\",\"addressLineTwo\":\"\",\"addressLineThree\":\"\",\"addressLineFour\":\"\",\"addressLineFive\":\"\",\"addressLineSix\":\"\",\"addressType\":\"OFFICE\",\"city\":\"MountainView\",\"countryCode\":\"USA\",\"postalCode\":\"94040\",\"stateOrProvinceCode\":\"CA\",\"id\":null,\"isApoFpo\":false,\"isPoBox\":false},\"storeFrontId\":{\"storeFrontId\":null,\"USStoreId\":5502},\"name\":{\"firstName\":\"\",\"middleName\":\"\",\"lastName\":\"\",\"generalSuffix\":\"\",\"titleOfRespect\":\"\"},\"phone\":{\"completeNumber\":\"\"},\"smsMobileNo\":{\"completeNumber\":\"\"},\"email\":{\"emailAddress\":\"\"}},\"orderedLineDates\":[{\"dateTypeId\":\"DELIVERY\",\"expectedDate\":\"2014-10-10T21:11:21Z\"}],\"charges\":[{\"chargeCategory\":\"DISCOUNT\",\"chargeName\":\"AssociateDiscount\",\"chargeQuantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":2.00},\"chargePerUnit\":{\"currencyAmount\":1.00,\"currencyUnit\":\"USD\"},\"chargePerLine\":{\"currencyAmount\":0.00,\"currencyUnit\":\"USD\"},\"isDiscount\":true,\"isBillable\":true},{\"chargeCategory\":\"FEE\",\"chargeName\":\"Fee1\",\"chargeQuantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":2.00},\"tax\":[{\"taxName\":\"Fee1\",\"taxPerLine\":{\"currencyAmount\":1.00,\"currencyUnit\":\"USD\"},\"estimatedTaxPerLine\":{\"currencyAmount\":1.00,\"currencyUnit\":\"USD\"}}]},{\"chargeCategory\":\"PRODUCT\",\"chargeName\":\"Product\",\"chargeQuantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":2.00},\"chargePerUnit\":{\"currencyAmount\":20.00,\"currencyUnit\":\"USD\"},\"tax\":[{\"taxName\":\"Tax1\",\"taxPerLine\":{\"currencyAmount\":1.00,\"currencyUnit\":\"USD\"},\"estimatedTaxPerLine\":{\"currencyAmount\":1.00,\"currencyUnit\":\"USD\"}}]}],\"orderLineQuantityInfo\":[{\"status\":\"PROCESSING\",\"statusCode\":\"2100.200\",\"statusDescription\":\"PO Created\",\"statusChangeDate\":\"2014-12-04T10:19:05Z\",\"statusQuantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":2.00}}],\"orderLineNote\":[],\"lineQuantitySummaries\":[{\"type\":\"Invoiced\",\"quantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":0.00}},{\"type\":\"Ordered\",\"quantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":2.00}},{\"type\":\"Received\",\"quantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":0.00}},{\"type\":\"Shipped\",\"quantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":0.00}},{\"type\":\"IntentToCancel\",\"quantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":1.0}},{\"type\":\"IntentToCancel\",\"quantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":1.0}}],\"holds\":[],\"returnInfo\":{\"openRMAQuantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":0.00},\"returnableQty\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":0.00},\"isCallTagRequired\":false}},{\"seller\":{\"catalogSellerId\":0,\"partnerDisplayName\":\"abc.com\"},\"primeLineNo\":2,\"orderedQty\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":2.00},\"fulfillmentType\":\"ELECTRONIC\",\"shippingMethod\":\"ELECTRONIC_DELIVERY\",\"orderProduct\":{\"offerId\":{\"offerId\":\"CFE4D1981AC746E89ABBCBCBAE6F024F\",\"upc\":\"0005436107673\",\"wupc\":\"0005436107673\",\"USItemId\":24074298,\"USSellerId\":0},\"productName\":\"LOYAL MK472B\",\"offerAttributes\":{\"isMailReturnable\":true,\"isPerishable\":false,\"wmDeptNo\":\"\"}},\"unitPrice\":{\"currencyAmount\":10.00,\"currencyUnit\":\"USD\"},\"shipToAddress\":{\"address\":{\"addressLineOne\":\"600 showers drive\",\"addressLineTwo\":\"\",\"addressLineThree\":\"\",\"addressLineFour\":\"\",\"addressLineFive\":\"\",\"addressLineSix\":\"\",\"addressType\":\"OFFICE\",\"city\":\"MountainView\",\"countryCode\":\"USA\",\"postalCode\":\"94040\",\"stateOrProvinceCode\":\"CA\",\"id\":null,\"isApoFpo\":false,\"isPoBox\":false},\"storeFrontId\":{\"storeFrontId\":null,\"USStoreId\":5502},\"name\":{\"firstName\":\"\",\"middleName\":\"\",\"lastName\":\"\",\"generalSuffix\":\"\",\"titleOfRespect\":\"\"},\"phone\":{\"completeNumber\":\"\"},\"smsMobileNo\":{\"completeNumber\":\"\"},\"email\":{\"emailAddress\":\"\"}},\"orderedLineDates\":[{\"dateTypeId\":\"DELIVERY\",\"expectedDate\":\"2014-09-30T19:36:22Z\"}],\"charges\":[{\"chargeCategory\":\"DISCOUNT\",\"chargeName\":\"AssociateDiscount\",\"chargeQuantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":2.00},\"chargePerUnit\":{\"currencyAmount\":1.00,\"currencyUnit\":\"USD\"},\"chargePerLine\":{\"currencyAmount\":0.00,\"currencyUnit\":\"USD\"},\"isDiscount\":true,\"isBillable\":true},{\"chargeCategory\":\"FEE\",\"chargeName\":\"Fee1\",\"chargeQuantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":2.00},\"tax\":[{\"taxName\":\"Fee1\",\"taxPerLine\":{\"currencyAmount\":1.00,\"currencyUnit\":\"USD\"},\"estimatedTaxPerLine\":{\"currencyAmount\":1.00,\"currencyUnit\":\"USD\"}}]},{\"chargeCategory\":\"PRODUCT\",\"chargeName\":\"Product\",\"chargeQuantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":2.00},\"chargePerUnit\":{\"currencyAmount\":10.00,\"currencyUnit\":\"USD\"},\"tax\":[{\"taxName\":\"Tax1\",\"taxPerLine\":{\"currencyAmount\":4.00,\"currencyUnit\":\"USD\"},\"estimatedTaxPerLine\":{\"currencyAmount\":4.00,\"currencyUnit\":\"USD\"}}]}],\"orderLineQuantityInfo\":[{\"status\":\"PROCESSING\",\"statusCode\":\"2100.200\",\"statusDescription\":\"PO Created\",\"statusChangeDate\":\"2014-10-27T16:07:22Z\",\"statusQuantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":2.00}}],\"orderLineNote\":[],\"lineQuantitySummaries\":[{\"type\":\"Invoiced\",\"quantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":0.00}},{\"type\":\"Ordered\",\"quantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":2.00}},{\"type\":\"Received\",\"quantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":0.00}},{\"type\":\"Shipped\",\"quantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":0.00}},{\"type\":\"IntentToCancel\",\"quantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":1.0}},{\"type\":\"IntentToCancel\",\"quantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":1.0}}],\"holds\":[],\"returnInfo\":{\"openRMAQuantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":0.00},\"returnableQty\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":0.00},\"isCallTagRequired\":false}}],\"paymentMethods\":[{\"paymentType\":\"CREDITCARD\",\"pmId\":\"VISA\",\"last4DigitsOfCard\":\"2725\",\"personInfoBillTo\":{\"address\":{\"addressLineOne\":\"850 Cherry Ave\",\"addressLineTwo\":\"\",\"addressLineThree\":\"\",\"addressLineFour\":\"\",\"addressLineFive\":\"\",\"addressLineSix\":\"\",\"city\":\"San Bruno\",\"countryCode\":\"USA\",\"postalCode\":\"94066\",\"stateOrProvinceCode\":\"CA\",\"id\":null,\"isApoFpo\":false,\"isPoBox\":false},\"name\":{\"completeName\":\"YUGESH GUNDAPANENI\",\"firstName\":\"YUGESH\",\"middleName\":\"\",\"lastName\":\"GUNDAPANENI\",\"generalSuffix\":\"\",\"titleOfRespect\":\"\"},\"phone\":{\"completeNumber\":\"408-660-5027\"},\"smsMobileNo\":{\"completeNumber\":\"408-660-5027\"},\"email\":{\"emailAddress\":\"ygundapaneni#abc.com\"}},\"amountpromised\":{\"currencyAmount\":100.00,\"currencyUnit\":\"USD\"},\"paymentReferenceId\":\"ca005e30-210b-4c23-b8ad-60adcdcea744\"}],\"shipments\":[],\"pickupPersons\":[{\"contactInfo\":{\"name\":{\"completeName\":\"YUGESH GUNDAPANENI\",\"firstName\":\"YUGESH\",\"middleName\":\"\",\"lastName\":\"GUNDAPANENI\",\"generalSuffix\":\"\",\"titleOfRespect\":\"\"},\"phone\":{\"completeNumber\":\"408-660-5027\"},\"smsMobileNo\":{\"completeNumber\":\"408-660-5027\"},\"email\":{\"emailAddress\":\"ygundapaneni#abc.com\"}},\"isPrimary\":false},{\"contactInfo\":{\"name\":{\"completeName\":\"YUGESH GUNDAPANENI\",\"firstName\":\"YUGESH\",\"middleName\":\"\",\"lastName\":\"GUNDAPANENI\",\"generalSuffix\":\"\",\"titleOfRespect\":\"\"},\"phone\":{\"completeNumber\":\"408-660-5027\"},\"smsMobileNo\":{\"completeNumber\":\"408-660-5027\"},\"email\":{\"emailAddress\":\"ygundapaneni#abc.com\"}},\"isPrimary\":true},{\"contactInfo\":{\"name\":{\"completeName\":\"Jane George\",\"firstName\":\"Jane\",\"middleName\":\"\",\"lastName\":\"George\",\"generalSuffix\":\"\",\"titleOfRespect\":\"\"},\"phone\":{\"completeNumber\":\"9995443333\"},\"smsMobileNo\":{\"completeNumber\":\"3436758989\"},\"email\":{\"emailAddress\":\"jane#abc.com\"}},\"isPrimary\":false}],\"lastModified\":\"2014-12-10T10:06:19Z\",\"orderNote\":[{\"contactType\":\"Account\",\"contactReference\":\"\",\"contactTime\":\"2014-10-27T16:07:00Z\",\"noteText\":\"Note dude\",\"reasonCode\":\"\",\"enteredBy\":\"Customer\"}],\"holds\":[],\"orderLineRelationships\":{\"warrantyRelationships\":[{\"productLine\":{\"productLineNo\":\"1\"},\"warrantyLine\":{\"warrantyLineNo\":\"2\"}}]},\"purchaseOrders\":[{\"purchaseOrderNo\":\"1569570013125\",\"shipNode\":\"2377\",\"shipNodeType\":\"DC\",\"scac\":\"\",\"tcNumber\":\"02237020963141852820177\",\"shipToAddress\":{\"address\":{\"addressLineOne\":\"600 showers drive\",\"addressLineTwo\":\"\",\"addressLineThree\":\"\",\"addressLineFour\":\"\",\"addressLineFive\":\"\",\"addressLineSix\":\"\",\"addressType\":\"OFFICE\",\"city\":\"MountainView\",\"countryCode\":\"USA\",\"postalCode\":\"94040\",\"stateOrProvinceCode\":\"CA\",\"id\":null,\"isApoFpo\":false,\"isPoBox\":false},\"storeFrontId\":{\"storeFrontId\":null,\"USStoreId\":5502},\"name\":{\"firstName\":\"\",\"middleName\":\"\",\"lastName\":\"\",\"generalSuffix\":\"\",\"titleOfRespect\":\"\"},\"phone\":{\"completeNumber\":\"\"},\"smsMobileNo\":{\"completeNumber\":\"\"},\"email\":{\"emailAddress\":\"\"}},\"poDate\":[],\"purchaseOrderLines\":[{\"poLineId\":\"2\",\"primeLineNo\":2,\"poLineStatusInfos\":[{\"poLineStatus\":\"1100.200\",\"poLineStatusCode\":\"1100.200\",\"poLineStatusDescription\":\"PO Created\",\"poLineStatusChangeDate\":\"2014-10-27T16:07:22Z\",\"poLineStatusQuantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":2.00}}],\"orderedQty\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":2.00},\"requestOfferId\":{\"offerId\":\"CFE4D1981AC746E89ABBCBCBAE6F024F\",\"upc\":\"0005436107673\",\"wupc\":\"0005436107673\",\"USItemId\":24074298,\"USSellerId\":0},\"notes\":[]}]},{\"purchaseOrderNo\":\"1569950018149\",\"shipNode\":\"2377\",\"shipNodeType\":\"DC\",\"scac\":\"\",\"tcNumber\":\"02237020963141852820177\",\"shipToAddress\":{\"address\":{\"addressLineOne\":\"600 showers drive\",\"addressLineTwo\":\"\",\"addressLineThree\":\"\",\"addressLineFour\":\"\",\"addressLineFive\":\"\",\"addressLineSix\":\"\",\"addressType\":\"OFFICE\",\"city\":\"MountainView\",\"countryCode\":\"USA\",\"postalCode\":\"94040\",\"stateOrProvinceCode\":\"CA\",\"id\":null,\"isApoFpo\":false,\"isPoBox\":false},\"storeFrontId\":{\"storeFrontId\":null,\"USStoreId\":5502},\"name\":{\"firstName\":\"\",\"middleName\":\"\",\"lastName\":\"\",\"generalSuffix\":\"\",\"titleOfRespect\":\"\"},\"phone\":{\"completeNumber\":\"\"},\"smsMobileNo\":{\"completeNumber\":\"\"},\"email\":{\"emailAddress\":\"\"}},\"poDate\":[],\"purchaseOrderLines\":[{\"poLineId\":\"1\",\"primeLineNo\":1,\"poLineStatusInfos\":[{\"poLineStatus\":\"1100.200\",\"poLineStatusCode\":\"1100.200\",\"poLineStatusDescription\":\"PO Created\",\"poLineStatusChangeDate\":\"2014-12-04T10:19:05Z\",\"poLineStatusQuantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":2.00}}],\"orderedQty\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":2.00},\"requestOfferId\":{\"offerId\":\"539A698868664B2DB87ECB061D328722\",\"upc\":\"0005436107672\",\"wupc\":\"0005436107672\",\"USItemId\":10154038,\"USSellerId\":0},\"notes\":[]}]}],\"orderPreferences\":{\"isSendSMSNotification\":true}}}";
public static void main(String[] args) {
JSONObject jsonObject = (JSONObject)JSONValue.parse(JSON_DATA);
System.out.println("JSON: "+jsonObject.toJSONString());
}
}

You have values such as 2.00.
These values are read as double. The String representation of such a double is 2.0 (notice the lacking 0).
That account for 1 less character for each occurrence of such a double value.
You have 43 such values.
There you have it.

Related

jose4j: how to set full serialization input?

Is there a way to set a JWE full serialization input with jose4j? For example, what goes in the TODO below?
public String decryptJWE(PrivateKey privateKey, String payload, boolean compact) throws JoseException {
JsonWebEncryption jwe = new JsonWebEncryption();
if (compact) {
jwe.setCompactSerialization(payload);
} else {
// TODO: what goes here? expecting something like jwe.setFullSerialization(payload)
}
jwe.setKey(privateKey);
return jwe.getPayload();
}
No, only the JWE compact serialization is supported. The general and flattened JWE JSON serializations aren't directly supported.

TextEncodings.Base64Url.Decode vs Convert.FromBase64String

I was working on creating a method that would generate a JWT token. Part of the method reads a value from my web.config that services as the "secret" used to generate the hash used to create the signature for the JWT token.
<add key="MySecret" value="j39djak49H893hsk297353jG73gs72HJ3tdM37Vk397" />
Initially I tried using the following to convert the "secret" value to a byte array.
byte[] key = Convert.FromBase64String(ConfigurationManager.AppSettings["MySecret"]);
However, an exception was thrown when this line was reached ...
The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
So I looked into the OAuth code and so another method being used to change a base64 string into a byte array
byte[] key = TextEncodings.Base64Url.Decode(ConfigurationManager.AppSettings["MySecret"]);
This method worked without issue. To me it looks like they are doing the same thing. Changing a Base64 text value into an array of bytes. However, I must be missing something. Why does Convert.FromBase64String fail and TextEncodings.Base64Url.Decode work?
I came across the same thing when I migrated our authentication service to .NET Core. I had a look at the source code for the libraries we used in our previous implementation, and the difference is actually in the name itself.
The TextEncodings class has two types of text encoders, Base64TextEncoder and Base64UrlEncoder. The latter one modifies the string slightly so the base64 string can be used in an url.
My understanding is that it is quite common to replace + and / with - and _. As a matter of fact we have been doing the same with our handshake tokens. Additionally the padding character(s) at the end can also be removed. This leaves us with the following implementation (this is from the source code):
public class Base64UrlTextEncoder : ITextEncoder
{
public string Encode(byte[] data)
{
if (data == null)
{
throw new ArgumentNullException("data");
}
return Convert.ToBase64String(data).TrimEnd('=').Replace('+', '-').Replace('/', '_');
}
public byte[] Decode(string text)
{
if (text == null)
{
throw new ArgumentNullException("text");
}
return Convert.FromBase64String(Pad(text.Replace('-', '+').Replace('_', '/')));
}
private static string Pad(string text)
{
var padding = 3 - ((text.Length + 3) % 4);
if (padding == 0)
{
return text;
}
return text + new string('=', padding);
}
}

ChannelHandler handling content of unknown size

I am still struggling with Camel (2.16.1) and Netty (4.0.33) to have them both receive tcp content of freely chosen length. Because of the unknown size of the tcp content received I was not yet able to create a working decoder for.
Let me describe my problem with an example. Lets say I have a file with a length of 3129 byte. When I nc that file to my route the size is not known until the last byte is read:
cat file.bin | nc localhost 10001
My route is defined like this:
from( "netty4:tcp://127.0.0.1:10001?sync=false&allowDefaultCodec=false&
decoder=#factory&receiveBufferSize=1000000")
.to("file:/temp/in");
The factory looks like this because I need to make sure that each ChannelHandler is used only once:
public class Factory implements ChannelHandlerFactory {
#Override
public ChannelHandler newChannelHandler() {
return new RawPrinterDecoder();
}
}
In my decoder I have this code:
public class RawPrinterDecoder extends ReplayingDecoder<Void> {
#Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in,
List<Object> out) throws Exception {
while (in.isReadable()) {
byte readByte = in.readByte();
job.addContent(readByte);
}
in.discardReadBytes();
}
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println("Bytes in job: " + job.getSize() );
}
}
The problem with this is that instead of 3129 byte I receive 9273. The reason for this is that the file is split into 3 segments of 1024 byte and 1 with 57 byte. Those are passed repeatedly to my decoder and although I try to invalidate the segments after they are first processed with in.discardReadBytes() they are processed again so instead of ...
segment1
segment2
segment3
segment4
... my decoder sees them like this
segment1
segment1+segment2
segment1+segment2+segment3
segment1+segment2+segment3+segment4
I tried so solve my problem by using checkpoint() but the segments were still called repeatedly.
How can I make sure that each segment is only processed once and in the correct order ? If this can be done more efficiently instead of reading single bytes recommendations are welcome (readableBytes() always return 2 GB so I can not use this to get the number of bytes).
It gets seperated into segments because of the ByteBuffAllocator that your server is using. You can change that this way:
#Bean
public ChannelInitializer<SocketChannel> channelInitializer() {
return new ChannelInitializer<SocketChannel>() {
#Override
public void initChannel(SocketChannel ch) throws Exception {
ch.config().setRecvByteBufAllocator(new FixedRecvByteBufAllocator(2048));
}
};
}
You can read all of the available bytes at once using:
ByteBuf buffer = in.readBytes(in);
or
ByteBuf buffer = in.readSlice(in.readableBytes());

Google App Engine MapReduceSpecification exception

Rewritten to simplify. Why does this compile:
MapReduceSpecification.of(
"Something, anything",
input,
mapper, // (extends Mapper<Job, Long, String>)
Marshallers.getLongMarshaller(),
Marshallers.getStringMarshaller(),
NoReducer.<Long, String, String>create(),
NoOutput.<String, String>create((int)1L)
);
But this does not. Note the different "mapper" extension in comments:
MapReduceSpecification.of(
"Something, anything",
input,
mapper, // (extends Mapper<Job, Long, JobSummary>)
Marshallers.getLongMarshaller(),
Marshallers.getSerializationMarshaller(),
NoReducer.<Long, JobSummary, JobSummary>create(),
NoOutput.<JobSummary, JobSummary>create((int)1L)
);
Throws this compilation exception:
The method
of(String,
Input<I>,
Mapper<I,K,V>,
Marshaller<K>,
Marshaller<V>,
Reducer<K,V,O>, Output<O,R>)
in the type MapReduceSpecification is not applicable for the arguments
(String,
JobInput,
JobMapper,
Marshaller<Long>,
Marshaller<Serializable>,
NoReducer<Long,JobSummary,JobSummary>,
NoOutput<JobSummary,JobSummary>)
JobSummary is simple (for purposes of this example):
public class JobSummary implements Serializable {
public String Text;
}
Any insight into which bit of the mystery combination of I, K, V, O, R I've missed?
Duplicating the SerializationMarshaller with a class-specific implementation seems to have solved it. It's a very unsatisfying solution, but it works.

How to use Java nio to write an uploaded image from ServletInputStream?

I've done the upload using ByteArrayOutputStream and now I want to use nio to write an image to a file in the hard disk from a ServletInputStream, I've tried a couple of ways but with no luck so far, now I have :
#Override
public void doPost(final HttpServletRequest request, final HttpServletResponse response)
throws IOException, ServletException {
final String fileName = "img_" + UUID.randomUUID().toString() + ".jpg";
final String filePathName = "E:\\tmp\\" + fileName;
final FileChannel outChannel = new FileOutputStream(filePathName).getChannel();
final ReadableByteChannel inChannel = Channels.newChannel(request.getInputStream());
outChannel.transferFrom(inChannel, 0, request.getContentLength());
inChannel.close();
outChannel.close();
}
The specified file is generated with the same size as original, but cannot be opened. What have I done wrong here please? what is the proper way?
Thanks.
I don't see why the '--' is being put in the file, unless it is being sent to you, but you need to call transferFrom() in a loop. You can't assume the entire file is transferred in one call. It returns the number of bytes it transferred each call, so you can track the total number transferred: if it's complete, break, otherwise add that to the offset, subtract it from the length, and repeat.

Resources