java - Convert a byte array from Encoding A to Encoding B

文章推薦指數: 80 %
投票人數:10人

1) raw: ByteArrayOutputStream containing bytes of a BINARY object sent to us from clients. The bytes usually come in UTF-8 as a part of a HTTP ... Home Public Questions Tags Users Companies Collectives ExploreCollectives Teams StackOverflowforTeams –Startcollaboratingandsharingorganizationalknowledge. CreateafreeTeam WhyTeams? Teams CreatefreeTeam Collectives™onStackOverflow Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost. LearnmoreaboutCollectives Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. LearnmoreaboutTeams ConvertabytearrayfromEncodingAtoEncodingB AskQuestion Asked 6years,9monthsago Modified 6years,9monthsago Viewed 23ktimes 6 Ihaveaprettyinterestingtopic-atleastforme.GivenaByteArrayOutputStreamwithbytesforexampleinUTF-8,Ineedafunctionthatcan"translate"thosebytesintoanother-new-ByteArrayOutputStreaminforexampleUTF-16,orASCIIoryounameit.MynaiveapproachwouldhavebeentouseaanInputStreamReaderandgiveinthethedesiredencoding,butthatdidn'tworkbecausethat'llreadintoachar[]andIcanonlywritebyte[]tothenewBAOS. publicbyte[]convertStream(Charsetencoding){ ByteArrayInputStreamoriginal=newByteArrayInputStream(raw.toByteArray()); InputStreamReadercontentReader=newInputStreamReader(original,encoding); ByteArrayOutputStreamconverted=newByteArrayOutputStream(); intreadCount; char[]buffer=newchar[4096]; while((readCount=contentReader.read(buffer,0,buffer.length))!=-1) converted.write(buffer,0,readCount); returnconverted.toByteArray(); } Now,thisobviouslydoesn'tworkandI'mlookingforawaytomakethisscenariopossible,withoutbuildingaStringoutofthebyte[]. @Edit: Sinceitseemsratherhardtoreadtheobviousthings. 1)raw:ByteArrayOutputStreamcontainingbytesofaBINARYobjectsenttousfromclients.ThebytesusuallycomeinUTF-8asapartofaHTTPMessage. 2)ThegoalhereistosendthisBINARYdataforwardtoaninternalSystemthat'snotflexible-wellthisisaninternalSystem-anditacceptssuchattachmentsinUTF-16.Idon'tknowwhydon'tevenask,itdoesso. Sotojustifymyquestion:IsthereawaytoconvertabytearrayfromCharsetAtoCharsetBorencodingofyourchoise.OnceagainBuildingaStringisNOTwhatI'mafter. Thankyouandhopethatclearsupquestionableparts:). javaencoding Share Improvethisquestion Follow editedDec22,2015at10:45 Displayname askedDec22,2015at10:30 DisplaynameDisplayname 61511goldbadge77silverbadges1414bronzebadges 9 Whatisraw?You'veonlygivenuspartoftheinformation.I'dexpecttojustconvertthebytestoastring,andthenconvertbackfromastringtoabytearray.Noneedtousestreamsatall. – JonSkeet Dec22,2015at10:32 Well,rawisobviouslyaByteArrayOutputStreamcontainingthebytesinwhateverencodingthatwasusedbyourclientofabinarydata.WehavetotransferthisdatatoourSysteminutf-8formátsoweneedtoconvertthewhatevertoutf-8orwhatever.Ihopethatclearsitup.Buildingastringisoutofquestionrightnow. – Displayname Dec22,2015at10:35 2 Whyisbuildingastringoutofthequestion?Ifthemostobviousapproachisinappropriate,youneedtoexplainwhythat'sthecase.Andthebenefitofashortbutcompleteexampleisthatwhatyouconsider"obvious"isspelledoutinthecode.FartoooftenI'vemadeassumptionsthatseem"obvious"tome,butturnoutnottobe...andwhenyou'renowaddingrestrictionsastowhatisfeasibleandwhatisn't,thataddstotheconfusion. – JonSkeet Dec22,2015at10:38 ifitconcernscharset,seethis:stackoverflow.com/questions/229015/encoding-conversion-in-java – guillaumegirod-vitouchkina Dec22,2015at10:41 2 Buttheanswerbuildingastringupdoesansweryouroriginalquestion.Therewasnothinginthatoriginalquestiontoexplainwhyyouwouldn'twanttodothat.Youstillhaven'tsaidwhyyourefusetocreateastring.Andbeingrudetopeopletryingtohelpyouisareally,reallybadidea. – JonSkeet Dec22,2015at10:47  |  Show4morecomments 1Answer 1 Sortedby: Resettodefault Highestscore(default) Trending(recentvotescountmore) Datemodified(newestfirst) Datecreated(oldestfirst) 16 Asmentionedincomments,I'djustconverttoastring: Stringtext=newString(raw.toByteArray(),encoding); byte[]utf8=text.getBytes(StandardCharsets.UTF_8); However,ifthat'snotfeasible(forsomeunspecifiedreason...)whatyou'vegotnowisnearlythere-youjustneedtoaddanOutputStreamWriterintothemix: //NothinghereshouldthrowIOExceptioninreality-workoutwhatyouwanttodo. publicbyte[]convertStream(Charsetencoding)throwsIOException{ ByteArrayInputStreamoriginal=newByteArrayInputStream(raw.toByteArray()); InputStreamReadercontentReader=newInputStreamReader(original,encoding); intreadCount; char[]buffer=newchar[4096]; try(ByteArrayOutputStreamconverted=newByteArrayOutputStream()){ try(Writerwriter=newOutputStreamWriter(converted,StandardCharsets.UTF_8)){ while((readCount=contentReader.read(buffer,0,buffer.length))!=-1){ writer.write(buffer,0,readCount); } } returnconverted.toByteArray(); } } Notethatyou'restillcreatinganextratemporarycopyofthedatainmemory,admittedlyinUTF-8ratherthanUTF-16...butfundamentallythisishardlyanymoreefficientthancreatingastring. Ifmemoryefficiencyisaparticularconcern,youcouldperformmultiplepassesinordertoworkouthowmanybyteswillberequired,createabytearrayofthewritelength,andthenadjustthecodetowritestraightintothatbytearray. Share Improvethisanswer Follow editedDec22,2015at10:47 answeredDec22,2015at10:41 JonSkeetJonSkeet 1.4m836836goldbadges89868986silverbadges90949094bronzebadges 1 PerfectOutputStreamWriterwastheanswer!Thatwouldhavebeenenoughforme! – Displayname Dec22,2015at10:50 Addacomment  |  YourAnswer ThanksforcontributingananswertoStackOverflow!Pleasebesuretoanswerthequestion.Providedetailsandshareyourresearch!Butavoid…Askingforhelp,clarification,orrespondingtootheranswers.Makingstatementsbasedonopinion;backthemupwithreferencesorpersonalexperience.Tolearnmore,seeourtipsonwritinggreatanswers. Draftsaved Draftdiscarded Signuporlogin SignupusingGoogle SignupusingFacebook SignupusingEmailandPassword Submit Postasaguest Name Email Required,butnevershown PostYourAnswer Discard Byclicking“PostYourAnswer”,youagreetoourtermsofservice,privacypolicyandcookiepolicy Nottheansweryou'relookingfor?Browseotherquestionstaggedjavaencodingoraskyourownquestion. TheOverflowBlog HowtoearnamillionreputationonStackOverflow:beofservicetoothers Therightwaytojobhop(Ep.495) FeaturedonMeta BookmarkshaveevolvedintoSaves Inboximprovements:markingnotificationsasread/unread,andafiltered... Revieweroverboard!Orarequesttoimprovetheonboardingguidancefornew... CollectivesUpdate:RecognizedMembers,Articles,andGitLab Shouldweburninatethe[script]tag? Linked 32 Encodingconversioninjava Related 4000 CreateArrayListfromarray 4567 HowdoIread/convertanInputStreamintoaStringinJava? 2599 HowdoIdeterminewhetheranarraycontainsaparticularvalueinJava? 2436 HowdoIdeclareandinitializeanarrayinJava? 966 ConvertInputStreamtobytearrayinJava 3409 HowdoIconvertaStringtoanintinJava? 1131 HowcanIdoBase64encodinginNode.js? 26660 Whyisprocessingasortedarrayfasterthanprocessinganunsortedarray? 889 ConvertingstringtobytearrayinC# HotNetworkQuestions ShouldIreapplytoajobafterashorttimewithanimprovedresume? WhatdothecolorsindicateonthisKC135tankerboom? HowtoruntheGUIofWindowsFeaturesOn/OffusingPowershell ConvertanintegertoIEEE754float Whois"Lord"in2Cor3:18 Isitcorrecttochangetheverbto"being"in"Despitenoonewashurtinthisincident…"? Botchingcrosswindlandings Areyougettingtiredofregularcrosswords? WillIgetdeniedentryafterIremovedavisasticker?Ismypassportdamaged? HowcanIkeepmyampfromtemperingthetoneofmyprocessor?(rockandhardmetalmusic) Movingframesmethod Vivadoconstraintswizardsuggestsalotofnonsensegeneratedclocks Whydostringhashcodeschangeforeachexecutionin.NET? Whattranslation/versionoftheBiblewouldChaucerhaveread? UsingLaTeX/TikZforfractalflower Justifyingdefinitionsofagroupaction. WhytheneedforaScienceOfficeronacargovessel? Howdocucumbershappen?Whatdoes"verypoorlypollinatedcucumber"meanexactly?Howcanpollinationbe"uneven"? HowdoIsignafileusingSSHandverifyitusingacertificateauthority? Wordsforrestaurant Sortbycolumngroupandignoreothercolumnsfailingforthisexample,why? CanyourunD&D5einapost-modernsettingwithoutmagic? Wouldextractinghydrogenfromthesunlessenitslifespan? HowtofindthebordercrossingtimeofatraininEurope?(Czechbureaucracyedition) morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. lang-java Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings  



請為這篇文章評分?